2016-05-06 13 views
0

ローカルホストにDatabaseを作成し、アンドロイドapp.Iでデータを解析したい場合NodeJSでサーバーを作成してもブラウザでデータベースが表示されますが、アンドロイドアプリこのローカルホストサーバーに接続できません。 ここに私のJavaコードがあります。Androidアプリがlocalhostサーバーに接続できません

public class MainActivity extends AppCompatActivity { 

protected TextView tvData; 

public static ArrayList<String> titleList = new ArrayList<String>(); 
private static ArrayAdapter<String> adapter; 
private static ListView lv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tvData = (TextView)findViewById(R.id.tvJsonItem); 

    // adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.listView1, titleList); 
    // lv = (ListView) findViewById(R.id.listView1); 

    new JSONTask().execute("http://10.0.2.2:3000/api/people"); 

} 

public class JSONTask extends AsyncTask<String,String,String>{ 
    @Override 
    protected String doInBackground(String... params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 
     int statusCode = 0; 

     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestProperty("Content-Type", "application/json"); 
      connection.setRequestMethod("GET"); 
      connection.connect(); //connect to server 

      statusCode = connection.getResponseCode(); 


      if (statusCode == 200) { 
       System.out.println("Server responded with code: " + statusCode); 

       InputStream stream = connection.getInputStream(); 

       reader = new BufferedReader(new InputStreamReader(stream)); 

       StringBuffer buffer = new StringBuffer(); 
       String line = ""; 

       while ((line = reader.readLine()) != null) { 
        buffer.append(line); 
       } 

       String finalJSON = buffer.toString(); 
       JSONObject parentObject = new JSONObject(finalJSON); 
       JSONArray parentArray = parentObject.getJSONArray("people"); 

       StringBuffer finalBufferdData = new StringBuffer(); 

       for (int i = 0; i < parentArray.length(); i++) { 

        JSONObject finalObj = parentArray.getJSONObject(i); 

        String peopleName = finalObj.getString("name"); 
        Integer age = finalObj.getInt("age"); 
        finalBufferdData.append(peopleName + "-->" + age + "\n"); 

        Log.i("Hakob_log",peopleName + "-->" + age + "\n"); 
       } 


       return finalBufferdData.toString(); //pases result to POstExecute 
      }else{ 
       Log.i("Hakob_log","Error"); 
      } 

     }catch(MalformedURLException e){ 
      e.printStackTrace(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     }catch(JSONException e){ 
      e.printStackTrace(); 
     } 
     finally { 
      if(connection !=null) { 
       connection.disconnect(); 
      } 
      try { 
       if(reader !=null) { 
        reader.close(); 
       } 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 


    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     tvData.setText(result); 
    } 
} 
} 

私は

new JSONTask().execute("http://localhost:3000/api/people"); 

おかげ

+0

ログを添付することができます – manolodewiner

+0

エミュレータを使用していますか? –

+0

ちょうどポストlogcat –

答えて

0

があなたの代わりにlocalhostのように言及されているコンピュータのIPアドレスを使用して、このラインに異常をthnk、これは例えば、あなたの問題を解決する必要があります。

new JSONTask().execute("http://10.0.0.100:3000/api/people"); 
+0

私もそれも試してみませんか?変更されました –

+0

AndroidManifest.xmlファイルにインターネットアクセス許可を追加しましたか? '使用許可android:name =" android.permission.INTERNET "/>' – jaolstad

+0

はい私はそれを実行しました –

関連する問題