尝试连接时出现HttpURLConnection错误

时间:2018-07-04 14:55:35

标签: android

尝试连接到URL时出现错误。

相同的代码对我来说就像Java控制台应用程序一样工作,但是当尝试在AsyncTask中使用Android应用程序执行此操作时,出现错误。这是我的代码:

public class XMLService extends AsyncTask<Void, Void, Boolean> {

public static String xml;
public static String responseMessage;
public static int responseCode;
static String urlGet = "http://192.168.178.38/data/static.xml";
static String urlPost = "http://192.168.178.38/data/changes.xml";

public static boolean getXML() {
    xml = "";
    try {
        URL url = new URL(urlGet);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.connect();

        BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String content = "", line;
        while ((line = rd.readLine()) != null) {
            content += line + "\n";
        }

        responseCode = connection.getResponseCode();
        responseMessage =  connection.getResponseMessage();

        if (content != "") {
            xml = content;
        }

        Log.d("XML", content);

        return true;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

@Override
protected Boolean doInBackground(Void... voids) {
    Log.d("Starting", "Starting");
    return getXML();
}

@Override
protected void onPostExecute(Boolean aBoolean) {
    super.onPostExecute(aBoolean);
    Log.d("DONE", "DONE");
    Log.d("xml: ", xml);
}
}

这是我的例外:

07-04 16:47:14.495 9580-9683/com.example.jonas.heatingcontrol W/System.err: java.net.SocketTimeoutException: timeout
07-04 16:47:14.497 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:218)
07-04 16:47:14.498 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:268)
    at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:222)
07-04 16:47:14.499 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:312)
    at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
07-04 16:47:14.500 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:202)
07-04 16:47:14.501 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:241)
    at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:147)
07-04 16:47:14.502 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:786)
07-04 16:47:14.503 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:658)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:488)
07-04 16:47:14.504 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:424)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:257)
07-04 16:47:14.505 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.example.jonas.heatingcontrol.Services.XMLService.getXML(XMLService.java:32)
07-04 16:47:14.506 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.example.jonas.heatingcontrol.Services.XMLService.doInBackground(XMLService.java:109)
    at com.example.jonas.heatingcontrol.Services.XMLService.doInBackground(XMLService.java:13)
07-04 16:47:14.507 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
07-04 16:47:14.508 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
07-04 16:47:14.509 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
07-04 16:47:14.510 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)
07-04 16:47:14.513 9580-9683/com.example.jonas.heatingcontrol W/System.err: Caused by: java.net.SocketException: Socket closed
    at java.net.SocketInputStream.read(SocketInputStream.java:203)
07-04 16:47:14.514 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:139)
07-04 16:47:14.515 9580-9683/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.Okio$2.read(Okio.java:142)
    at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:218)
07-04 16:47:14.516 9580-9683/com.example.jonas.heatingcontrol W/System.err:      ... 19 more
07-04 16:47:14.517 9580-9580/com.example.jonas.heatingcontrol D/DONE: DONE
07-04 16:49:09.565 9580-9684/com.example.jonas.heatingcontrol W/DpmTcmClient: Couldn't find 'tcm' socket after 120times. quit trying

我使用手机来运行此应用,并且可以使用它和我的电脑连接到该网址。我已设置互联网许可。知道有什么问题吗?

编辑:这是我删除超时时遇到的错误:

07-04 18:10:45.144 24043-24074/com.example.jonas.heatingcontrol W/System.err: java.io.IOException: unexpected end of stream on com.android.okhttp.Address@a6b09d89
07-04 18:10:45.146 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:256)
    at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:147)
07-04 18:10:45.147 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:786)
07-04 18:10:45.149 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:658)
07-04 18:10:45.150 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:488)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:424)
07-04 18:10:45.151 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:257)
07-04 18:10:45.152 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.example.jonas.heatingcontrol.Services.XMLService.getXML(XMLService.java:33)
    at com.example.jonas.heatingcontrol.Services.XMLService.doInBackground(XMLService.java:110)
07-04 18:10:45.153 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.example.jonas.heatingcontrol.Services.XMLService.doInBackground(XMLService.java:13)
    at android.os.AsyncTask$2.call(AsyncTask.java:333)
07-04 18:10:45.154 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
07-04 18:10:45.155 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
07-04 18:10:45.156 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)
07-04 18:10:45.159 24043-24074/com.example.jonas.heatingcontrol W/System.err: Caused by: java.io.EOFException: \n not found: size=0 content=...
    at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:206)
07-04 18:10:45.160 24043-24074/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:241)
    ... 15 more
07-04 18:10:45.161 24043-24043/com.example.jonas.heatingcontrol D/DONE: DONE

0 个答案:

没有答案