HTTP请求可用作Java控制台应用程序,但不能用作Android应用程序

时间:2018-07-05 20:16:10

标签: java android http httpurlconnection

我正在尝试从本地网络中的服务器获取XML文件。我有一个正常工作的Java控制台应用程序和一个Android应用程序。这是我的代码:

public static void getXML() {
    try {
        URL url = new URL(urlGet);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(3000);
        conn.setReadTimeout(3000);
        conn.setDoInput(true);
        conn.setDoOutput(true);

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

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

        Log.d("response: ", xml);
        listener.onSuccess();


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

对于Java应用程序和Android应用程序,“ getXML()”方法相同。唯一的区别是,我直接在Java应用程序中调用TestXML.getXML(),在Android应用程序中调用new TestXML.execute()

它对于Java应用程序正常工作,但是在Android应用程序中我收到错误消息:

07-05 22:11:21.827 26440-26652/com.example.jonas.heatingcontrol W/System.err: java.net.SocketTimeoutException: timeout
07-05 22:11:21.843 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:218)
07-05 22:11:21.844 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:268)
07-05 22:11:21.845 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:222)
07-05 22:11:21.846 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:312)
07-05 22:11:21.847 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
07-05 22:11:21.848 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:202)
    at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:241)
07-05 22:11:21.849 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:147)
07-05 22:11:21.850 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:786)
07-05 22:11:21.851 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:658)
07-05 22:11:21.852 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:488)
07-05 22:11:21.853 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:424)
07-05 22:11:21.854 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:257)
07-05 22:11:21.855 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.getInputStream(TrackedHttpURLConnection.java:356)
07-05 22:11:21.856 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.tools.profiler.support.network.httpurl.HttpURLConnection$.getInputStream(HttpURLConnection$.java:207)
07-05 22:11:21.857 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.example.jonas.heatingcontrol.GetXMLService.getXML(GetXMLService.java:31)
07-05 22:11:21.858 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.example.jonas.heatingcontrol.GetXMLService.doInBackground(GetXMLService.java:61)
    at com.example.jonas.heatingcontrol.GetXMLService.doInBackground(GetXMLService.java:15)
07-05 22:11:21.859 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
07-05 22:11:21.861 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
07-05 22:11:21.862 26440-26652/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-05 22:11:21.863 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
07-05 22:11:21.864 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at java.lang.Thread.run(Thread.java:764)
07-05 22:11:21.871 26440-26652/com.example.jonas.heatingcontrol W/System.err: Caused by: java.net.SocketException: Socket closed
07-05 22:11:21.872 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:203)
07-05 22:11:21.873 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:139)
    at com.android.okhttp.okio.Okio$2.read(Okio.java:142)
07-05 22:11:21.874 26440-26652/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:218)
07-05 22:11:21.875 26440-26652/com.example.jonas.heatingcontrol W/System.err:   ... 21 more

我不明白为什么它可以在我的PC上运行而不能在我的手机上运行。它们都在同一网络中。我可以在手机和PC上打开urlGet!此外,该应用程序还可以通过HTTP POST到另一个URL,但在同一网络中使用相同的API。删除超时也不起作用,但是会出现此错误:

07-05 20:26:06.325 9427-9481/com.example.jonas.heatingcontrol W/System.err: java.io.IOException: unexpected end of stream on com.android.okhttp.Address@d7b518d6
07-05 20:26:06.326 9427-9481/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:201)
    at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
07-05 20:26:06.327 9427-9481/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
07-05 20:26:06.328 9427-9481/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
07-05 20:26:06.329 9427-9481/com.example.jonas.heatingcontrol W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
    at com.example.jonas.heatingcontrol.GetXMLService.getXML(GetXMLService.java:31)
07-05 20:26:06.330 9427-9481/com.example.jonas.heatingcontrol W/System.err:     at com.example.jonas.heatingcontrol.GetXMLService.doInBackground(GetXMLService.java:61)
    at com.example.jonas.heatingcontrol.GetXMLService.doInBackground(GetXMLService.java:15)
07-05 20:26:06.331 9427-9481/com.example.jonas.heatingcontrol W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
07-05 20:26:06.332 9427-9481/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:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
07-05 20:26:06.333 9427-9481/com.example.jonas.heatingcontrol W/System.err:     at java.lang.Thread.run(Thread.java:764)
07-05 20:26:06.334 9427-9481/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:201)
    at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
    ... 15 more

0 个答案:

没有答案