关于发送数据的Android AsyncTask SocketException

时间:2017-06-12 05:26:47

标签: android android-asynctask socketexception

我正在做的是从Android应用程序发送到URL上的服务的POST请求并且它一直崩溃,我无法弄清楚为什么?! 当我没有for循环但它应该像这样可行时,它有效吗?有什么想法吗?

public partial class OrderOrderData
{
    public OrderOrderDataOfferOrder[] OfferOrder{get;set}
}

public partial class OrderOrderDataOfferOrder
{
    public OrderOrderDataOfferOrderQuestion[] Question{get;set}
}

public partial class OrderOrderDataOfferOrderQuestion
{
    public OrderOrderDataOfferOrderQuestionPrice Price{get;set;}
}

堆栈跟踪:

protected String doInBackground(String... params) {
    try {
        Log.i("ASYNC", "BG ASYNC STARTED");
        for (String wifi : wifis) {
            if (wifi.length() > 10) {
                Log.i("WIFI", wifi);
                URL url = new URL("http://ip/add");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                conn.setRequestProperty("Accept", "application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
                os.writeBytes(wifi);

                os.flush();
                os.close();

                conn.disconnect();

                Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                Log.i("MSG", conn.getResponseMessage());

          }


                Log.i("RESPONSE", String.valueOf(conn.getResponseCode()));
            }
        }
    } catch (Exception e) {
        Log.e("Exception", "e",e);
        return "";
    }
    return "";
}

清单权限:

E/Exception: e
                               java.net.SocketException: Socket closed
                                   at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:120)
                                   at java.net.SocketOutputStream.write(SocketOutputStream.java:157)
                                   at com.android.okhttp.okio.Okio$1.write(Okio.java:76)
                                   at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
                                   at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
                                   at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
                                   at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
                                   at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:905)
                                   at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:783)
                                   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)
                                   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
                                   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:521)
                                   at pz.metropolitan.luka.listenwifi.Download.doInBackground(Download.java:63)
                                   at pz.metropolitan.luka.listenwifi.Download.doInBackground(Download.java:18)
                                   at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                   at java.lang.Thread.run(Thread.java:761)

0 个答案:

没有答案