为什么我连接失败了:ETIMEDOUT?

时间:2016-11-08 10:19:38

标签: java android android-asynctask android-emulator

我编写了以下Asynctask以获取json数据。我收到以下异常。我试图在模拟器中运行它。

class FetchData extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String[] values) {
        try {

            java.net.URL url = new URL("http://www.json-generator.com/api/json/get/ckxAGAPILC?indent=2");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = connection.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(reader);
            StringBuilder stringBuilder = new StringBuilder();
            String tempString;
            while ((tempString = bufferedReader.readLine()) != null) {
                stringBuilder.append(tempString);
                stringBuilder.append("\n");
            }
            Log.i("TAG","string builder"+stringBuilder.toString());
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

这是我得到的例外

java.net.ConnectException: failed to connect to www.json-generator.com/216.58.197.51 (port 80): connect failed: ETIMEDOUT (Connection timed out)
11-08 02:50:59.197 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
11-08 02:50:59.197 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
11-08 02:50:59.198 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
11-08 02:50:59.200 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at java.net.Socket.connect(Socket.java:884)
11-08 02:50:59.202 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
11-08 02:50:59.203 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
11-08 02:50:59.204 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
11-08 02:50:59.204 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:152)
11-08 02:50:59.204 15857-26127/com.mindtree.a03_m6_assign_v21 W/System.err:     at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)

0 个答案:

没有答案