2016-05-06 4 views
0

Ich habe erstellt Database in meinem localhost, und möchte Daten von ihm mit Android app.I Create Server mit NodeJS und es funktioniert, ich kann die Datenbank im Browser sehen, aber meine Android-App kann keine Verbindung mit diesem Localhost-Server herstellen. Hier ist mein Java-Code.Android App kann keine Verbindung zu localhost Server

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); 
    } 
} 
} 

ich thnk etwas falsch mit dieser Linie

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

Dank

+0

können Sie das Protokoll anhängen – manolodewiner

+0

Verwenden Sie Emulator? –

+0

einfach post logcat –

Antwort

0

Verwenden Sie die IP-Adresse des Computers, die Sie sich beziehen, als stattdessen localhost, das Ihr Problem zB lösen sollte:

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

Ich versuche es auch, aber nichts ist geändert –

+0

Haben Sie die INTERNET-Berechtigung in Ihrer AndroidManifest.xml-Datei hinzugefügt? '' – jaolstad

+0

ja ich habe es getan –

Verwandte Themen