简体   繁体   中英

Android app can not connect to localhost server

I've created Database in my localhost, and want parse data from it with android app.I Create server with NodeJS and it works i can see the Database in browser, but my android app can't connect with this localhost server. Here is my Java code.

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

I thnk something wrong with this line

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

Thanks

请使用您指的是localhost的计算机的IP地址,这应该可以解决您的问题,例如:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM