由于连接超时,无法获取JSON数据

时间:2016-09-23 10:00:47

标签: android connection-timeout android-json

我有一个生成JSON数据的spring-boot Web服务,该服务在我的本地计算机http://localhost:8080/demo/services中运行。

我尝试使用我的IP地址获取JSON数据,但它在LogCat中给出了以下错误:

09-23 15:18:27.897 2923-3540/com.example.palangou.foodie1 
    W/System.err: java.net.ConnectException: failed to connect to /192.168.1.9
    (port 8080): connect failed: ETIMEDOUT (Connection timed out)

相关的Java代码是:

new JSONpart().execute("http://192.168.1.9:8080/demo/services");

public class JSONpart extends AsyncTask<String, Void, List<POJO>> {


    @Override
    protected List<POJO> doInBackground(String... urls) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        String place = "", name = "";
        List<POJO> arraylist = new ArrayList<>();
        try {
            URL url = new URL(urls[0]);
            try {
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream input = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(input));

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

                Log.d("Recursive places",buffer.toString());
                String finalJSON = buffer.toString();
                try {
                    //JSONObject object = new JSONObject(finalJSON);
                    JSONArray array = new JSONArray(finalJSON);

                    for(int i=0;i< array.length();i++)
                    {//Log.d("Recursive places",     finalobject.getString("name"));
                        POJO pojo = new POJO();
                        JSONObject finalobject = array.getJSONObject(i);
                        pojo.setName(finalobject.getString("service"));
                        pojo.setPlace(finalobject.getString("servicedesc"));
                       //
                        arraylist.add(pojo);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
              //  Log.d("name", arraylist.get(1).getName().toString());

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            ;
        } finally {
            connection.disconnect();
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       // return buffer.toString();
         return  arraylist;
    }

    @Override
    protected void onPostExecute(List<POJO> s) {
        super.onPostExecute(s);
       // Log.d("OnPOST", s.get(1).getName().toString());
        FoodiesAdapater adapter = new FoodiesAdapater(getApplicationContext(),R.layout.row,s);
        listView.setAdapter(adapter);



    }
}

1 个答案:

答案 0 :(得分:1)

请检查您的api或网址是否正常工作。如果您将Json文件上传到此http://myjson.com/并从此在线服务器获取网址会更好。