Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android http post请求获取SocketTimeoutException_Android - Fatal编程技术网

Android http post请求获取SocketTimeoutException

Android http post请求获取SocketTimeoutException,android,Android,我试图配置一个HTTP POST请求,但我总是得到SocketTimeoutException。 有人知道我的代码有什么问题吗 int TIMEOUT_MILLISEC = 60000; // = 60 seconds HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParam

我试图配置一个HTTP POST请求,但我总是得到
SocketTimeoutException
。 有人知道我的代码有什么问题吗

int TIMEOUT_MILLISEC = 60000;  // = 60 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

client = new DefaultHttpClient(httpParams);

StringBuilder builder = new StringBuilder(URL_STRING); 

HttpPost request = new HttpPost(builder.toString());

request.setEntity(new ByteArrayEntity(jobj.toString().getBytes("UTF8")));

HttpResponse response = client.execute(request);

请参阅此代码。它将帮助您了解如何处理几乎所有基本的HttpClient异常。 试一试{

    // check for request method
    if (method == "POST") {
        // request method is POST
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        // new
        HttpParams httpParameters = httpPost.getParams();
        // Set the timeout in milliseconds until a connection is
        // established.
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 10000;
        HttpConnectionParams
                .setSoTimeout(httpParameters, timeoutSocket);
        // new
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } else if (method == "GET") {
        // request method is GET
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += "?" + paramString;
        HttpGet httpGet = new HttpGet(url);
        // new
        HttpParams httpParameters = httpGet.getParams();
        // Set the timeout in milliseconds until a connection is
        // established.
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 10000;
        HttpConnectionParams
                .setSoTimeout(httpParameters, timeoutSocket);
        // new
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    }

} catch (UnsupportedEncodingException e) {
    throw new Exception("Unsupported encoding error.");
} catch (ClientProtocolException e) {
    throw new Exception("Client protocol error.");
} catch (SocketTimeoutException e) {
    throw new Exception("Sorry, socket timeout.");
} catch (ConnectTimeoutException e) {
    throw new Exception("Sorry, connection timeout.");
} catch (IOException e) {
    throw new Exception("I/O error(May be server down).");
}

无需设置套接字超时。 删除此行:

HttpConnectionParams.setSoTimeout(httpParams,超时_毫秒);

可能是因为您的服务器没有应答?您的服务器是否正在侦听默认端口?如果没有,您是否指定了端口?您的服务器是否在web浏览器上正确应答请求?您希望得到什么样的响应?