W / System.err:java.io.IOException:Connection上的流意外结束

时间:2019-01-07 15:37:57

标签: android android-volley httpurlconnection

我已经开发了一个Android应用,该应用使用PHP Web API存储和从MySQL服务器中检索数据,该MySQL服务器托管在一个公共共享托管帐户中。我有大约550位固定用户,应该登录我的应用并进行数据输入。在测试阶段,我从未遇到任何连接问题。但是当使用全部用户群时,它随机显示错误“ W / System.err:java.io.IOException:Connection上流的意外结束”。在网上搜索了很多,最后得到了对我不起作用的答案。如果有人知道哪里出了问题,我将不胜感激。

首先,我使用AsyncTask和HttpURLConnection连接到Web服务器。然后部分替换为Volley。但是,当大多数用户尝试同时访问时,特别是仍然存在相同的问题。以下是我的Web请求处理程序。

public class RequestHandler {

    public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams){
        URL url;

        StringBuilder sb;
        String sbString;

    try{
        url = new URL(requestURL);

        //Creating a http url connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        //Configuring connection properties
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        //Creating an output stream
        OutputStream os = conn.getOutputStream();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));
        writer.flush();
        writer.close();
        os.close();

        int responseCode = conn.getResponseCode();

        if(responseCode == HttpURLConnection.HTTP_OK){
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String response;

            while((response = reader.readLine()) != null){
                sb.append(response);
            }
            sbString = sb.toString();
            Log.d("RequestHandler", "***** sendPostRequest() >> HTTP_OK >> "+sbString);
        }
        else{
            sbString = "{\"error\": true,\"message\":\"URL Connection Error\"}";
        }
    }
    catch (SocketTimeoutException conTimeoutEx) {
        sbString = "{\"error\": true,\"message\":\"Timeout caused by poor Internet connectivity.\"}";
    }
    catch(Exception e){
        sbString = "{\"error\": true,\"message\":\"technical error: "+e.getMessage()+"\"}";
        e.printStackTrace();
    }

    //return sb.toString();
    return sbString;
}

}

第二个catch块中出现异常。使用Volley时也会出现相同的例外情况。

0 个答案:

没有答案
相关问题