Php 处理具有timedout的http请求,而实际上服务器成功地处理了该请求

Php 处理具有timedout的http请求,而实际上服务器成功地处理了该请求,php,android,httpresponse,Php,Android,Httpresponse,考虑下一个案例 我正在通过我的应用程序向服务器发布一些东西。例如,我设置了连接并将响应超时设置为3000ms 在互联网连接缓慢的情况下,用户应用程序将抛出异常,该异常将被处理以通知用户请求已超时,但实际上服务器已成功处理请求和数据 当然,我会将超时时间设置为3000毫秒以上,我只是在注意到这个问题时才将其用于测试目的,但我不禁想知道如何处理这个问题?第一步是控制状态代码,它将我们的api发送到应用程序,如果此代码为'statusCode==200',我们可以知道连接的开始和结束是正确的。作为第二

考虑下一个案例

我正在通过我的应用程序向服务器发布一些东西。例如,我设置了连接并将响应超时设置为3000ms

在互联网连接缓慢的情况下,用户应用程序将抛出异常,该异常将被处理以通知用户请求已超时,但实际上服务器已成功处理请求和数据


当然,我会将超时时间设置为3000毫秒以上,我只是在注意到这个问题时才将其用于测试目的,但我不禁想知道如何处理这个问题?

第一步是控制状态代码,它将我们的api发送到应用程序,如果此代码为'statusCode==200',我们可以知道连接的开始和结束是正确的。作为第二步,我要做的是,我的服务器将向我发送一个自定义答案。例如,如果我可以读取所有内容并正确完成连接,我将从服务器发送“OK”作为响应实体。您可以在下面看到我的代码:

private String Sync3_GetRequest_NewLocalInformation(String url,String id,List<NameValuePair> ListOfValues){
    //Declaration of variables
    DefaultHttpClient httpClient;
    HttpPost Request = new HttpPost(url);
    HttpResponse Response;
    HttpParams httpParameters = new BasicHttpParams();
    httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    String Result = "Completed";

    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 60000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    httpClient = new DefaultHttpClient(httpParameters);

    try {
        HttpEntity entity = new UrlEncodedFormEntity(ListOfValues);
        Request.setHeader(entity.getContentType());
        Request.setEntity(entity);


        Response = httpClient.execute(Request);

        if (Response.getStatusLine().getStatusCode() == 200) {
            String EntityResult = EntityUtils.toString(Response.getEntity());
            Log.e("-- Sync_Class.Sync3_GetRequest_NewLocalInformation --", EntityResult);

            if(EntityResult.contains("\"message\":OK")){
                //My code
            }
            else{
                Log.e("-- Sync_Class.Sync3_GetRequest_NewLocalInformation --", "Invalid code");
            }

            return Result;
        }
        else{
            MainActivity.CanUpdate = true;
            Log.e("-- Sync_Class.Sync3_GetRequest_NewLocalInformation --", "Invalid Status Code");
            throw new RuntimeException("Invalid Status Code");
        }
    }
    catch (Exception ex){
        MainActivity.CanUpdate = true;
        Log.e("-- Sync_Class.Sync3_GetRequest_NewLocalInformation --", "Exception",ex);
        return ex.toString();
    }
}
private String Sync3\u GetRequest\u NewLocalInformation(字符串url、字符串id、值列表){
//变量声明
默认httpClient httpClient;
HttpPost请求=新的HttpPost(url);
HttpResponse响应;
HttpParams httpParameters=新的BasicHttpParams();
setParameters(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
String Result=“已完成”;
//以毫秒为单位设置超时,直到建立连接。
//默认值为零,表示不使用超时。
int timeoutConnection=3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//设置默认套接字超时(SO\U超时)
//以毫秒为单位,这是等待数据的超时。
int timeoutSocket=60000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
httpClient=新的默认httpClient(httpParameters);
试一试{
HttpEntity=新的UrlEncodedFormEntity(ListOfValues);
setHeader(entity.getContentType());
请求。设置实体(实体);
Response=httpClient.execute(请求);
if(Response.getStatusLine().getStatusCode()==200){
字符串EntityResult=EntityUtils.toString(Response.getEntity());
Log.e(“--Sync\u Class.Sync3\u GetRequest\u NewLocalInformation--”,EntityResult);
if(EntityResult.contains(“\”消息\“:确定”)){
//我的代码
}
否则{
Log.e(“--Sync_Class.Sync3_GetRequest_NewLocalInformation--”,“无效代码”);
}
返回结果;
}
否则{
MainActivity.CanUpdate=true;
Log.e(“--Sync\u Class.Sync3\u GetRequest\u NewLocalInformation--”,“无效状态代码”);
抛出新的运行时异常(“无效状态代码”);
}
}
捕获(例外情况除外){
MainActivity.CanUpdate=true;
Log.e(“--Sync\u Class.Sync3\u GetRequest\u NewLocalInformation--”,“Exception”,ex);
返回例如toString();
}
}
我使用简单的步骤来控制这些问题。如果我能帮你,请告诉我,祝你好运