【问题标题】:Couldn't Fetch JSON data because of connection timeout由于连接超时,无法获取 JSON 数据
【发布时间】:2016-09-23 10:00:47
【问题描述】:

我有一个生成 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);



    }
}

【问题讨论】:

  • 你的 api 工作了吗??
  • 您是否连接到您的电脑所连接的同一个 wifi?
  • 检查你的服务器是否开启!
  • 您使用的是 Genymotion 还是任何真实设备?
  • 除了user6556461提出的观点,也可以关注这里的答案——stackoverflow.com/questions/25886578/…

标签: android connection-timeout android-json


【解决方案1】:

请检查您的 api 或 url 是否有效。如果你将你的 Json 文件上传到这个http://myjson.com/ 并从这个在线服务器获取一个 url,那就更好了。

【讨论】:

  • 您也可以在此链接中检查您的 Json 是否正常工作
  • 奇怪的是,这行得通,因为 LogCat 没有显示出问题所在。 @GouthamPalani 你能在这里解释一下吗? :)
  • @Sufian 我刚刚使用 myjson.com 来获取数据。我在那里手动上传了我的 JSON 文件
  • JSON 从来都不是问题。这是访问API。您的设备与问题下 cmets 中指出的不在同一个网络上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 2012-03-06
  • 2014-01-09
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
相关资源
最近更新 更多