时间:2016-03-02 11:04:27

标签: android httpurlconnection

我正在尝试使用以下代码发布json。

它已经工作了一次,但现在它抛出以下异常消息:"在5000ms&#34之后无法连接到/xx.xx.xx.xx(端口XXXXX);

这里有什么问题?

public void sendToDp(PayloadEntity pEntity) {

    URL url = null;
    HttpURLConnection connection = null;
    try {
        url = new URL(PreferenceHelper.getInstance().getBaseUrl());
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestMethod("POST");
        connection.setConnectTimeout(5000);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        OutputStream os = new BufferedOutputStream(connection.getOutputStream());
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(pEntity.getJsonString());
        writer.flush();
        writer.close();
        os.close();
        int responseCode = connection.getResponseCode();
        listener.onReceive(responseCode == HttpsURLConnection.HTTP_OK, pEntity);
    } catch (MalformedURLException mue) {
        listener.onReceive(false, pEntity);
    } catch (Exception e) {
        listener.onReceive(false, pEntity);
    } finally {
        if (connection != null)
            connection.disconnect();
    }
}

编辑:添加堆栈跟踪:

03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:java.net.SocketTimeoutException:5000ms后无法连接到/10.41.66.248(端口11890) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at libcore.io.IoBridge.connectErrno(IoBridge.java:169) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at libcore.io.IoBridge.connect(IoBridge.java:122) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at java.net.Socket.connect(Socket.java:882) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at com.android.okhttp.Connection.connect(Connection.java:152) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:56) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.onReceive(QueueManager.java:97) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:71) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.onReceive(QueueManager.java:97) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:71) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.onReceive(QueueManager.java:97) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:71) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.onReceive(QueueManager.java:97) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:71) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.onReceive(QueueManager.java:97) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:71) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.onReceive(QueueManager.java:97) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:71) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.onReceive(QueueManager.java:97) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.NetworkHelper.sendToDp(NetworkHelper.java:71) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager $ LooperThread $ 1.handleMessage(QueueManager.java:129) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.QueueManager.startUpload(QueueManager.java:62) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at network.Logger.logUpdates(Logger.java:59) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at service.PullHelper.init(PullHelper.java:61) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at service.SyncService.onHandleIntent(SyncService.java:26) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.Looper.loop(Looper.java:135) 03-02 17:22:34.793 24578-24758 / com.vkc.main W / System.err:at android.os.HandlerThread.run(HandlerThread.java:61)

4 个答案:

答案 0 :(得分:0)

问题是您的应用无法通过网络访问该网址。问题可能是:

  • 您的网络连接已脱机。
  • 您尝试访问的网站已关闭。
  • 您正面临防火墙或代理问题。

答案 1 :(得分:0)

它抛出java.net.SocketTimeoutException,因为您已将连接超时设置为某个值         connection.setConnectTimeout(5000);

系统尝试建立与您的Url的连接,直到超时值。如果在您设置的时间内未能建立连接,则会抛出" SocketTimeoutException"

答案 2 :(得分:0)

尝试使用更好的http库,例如 Volley ,它比HttpUrlConnection运行更快,更容易调试

您的代码将如下所示:


JsonObjectRequest jor = new JsonObjectRequest(Request.Method.POST, urlLink, new JsonObject(pEntity.getJsonString()), new Response.Listener() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    //RESPONSE
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                   //ERROR
                }
            });
Volley

About Volley be faster

答案 3 :(得分:0)

我认为这个问题会出现在您的情况下(因为我正面临),因为您的服务在localhost上运行(在5000ms后无法连接到/10.41.66.248(端口11890))。我的意思是在本地网络上,你正在尝试从外部网络连接(如移动数据)。但是由于一些安全原因,本地网络不允许访问其他网络。 将您的设备连接到本地Wi-Fi网络(相同的localhost互联网连接),然后运行您的应用。

希望这能解决你的问题......