Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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端停机的情况_Android_Http Post - Fatal编程技术网

如何处理服务器在我的android端停机的情况

如何处理服务器在我的android端停机的情况,android,http-post,Android,Http Post,这是我的doInBackground()方法,在其中我调用了另一个类中的makeHttpRequest()方法 protected String doInBackground(Integer... args) { // Building Parameters String parameter1 = "tenant"; String parameter2 = "price"; List&l

这是我的doInBackground()方法,在其中我调用了另一个类中的makeHttpRequest()方法

 protected String doInBackground(Integer... args) {
            // Building Parameters

            String parameter1 = "tenant";
                String parameter2 = "price";

            List<NameValuePair> params = new ArrayList<NameValuePair>();

                params.add(new BasicNameValuePair("person",parameter1));
                        params.add(new BasicNameValuePair("price",parameter2));

            JSONObject json = jParser.makeHttpRequest(requiredurl, "POST", params);


            Log.d("Details", json.toString());



                int success = json.getInt("connected");

                if (success == 1) {

                    //blah blah
                      }
        }
受保护字符串doInBackground(整数…args){
//建筑参数
字符串参数1=“租户”;
字符串参数2=“价格”;
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“person”,参数1));
参数添加(新的BasicNameValuePair(“价格”,参数2));
JSONObject json=jParser.makeHttpRequest(requiredurl,“POST”,参数);
Log.d(“Details”,json.toString());
int success=json.getInt(“已连接”);
如果(成功==1){
//废话
}
}
makeHttpRequest()方法:

public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){

                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);

                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }       

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

       ................
       ....................... // Here the result is extracted and made it to json object
       .............................

        // return JSON 
        return jObj;  // returning the json object to the method that calls.

    }
公共JSONObject makeHttpRequest(字符串url、字符串方法、, 列表参数){ //发出HTTP请求 试一试{ //检查请求方法 如果(方法==“POST”){ //defaultHttpClient DefaultHttpClient httpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(url); setEntity(新的UrlEncodedFormEntity(参数)); HttpResponse HttpResponse=httpClient.execute(httpPost); HttpEntity HttpEntity=httpResponse.getEntity(); is=httpEntity.getContent(); } }捕获(不支持的编码异常e){ e、 printStackTrace(); }捕获(客户端协议例外e){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } ................ 这里提取结果并将其转换为json对象 ............................. //返回JSON return jObj;//将json对象返回给调用的方法。 } 因此,通过上面的代码,对服务器的实际调用是通过以下行进行的->
HttpResponse HttpResponse=httpClient.execute(httpPost)


当我的服务器启动时,一切正常,但当它关闭时,进度条会持续加载。我想处理这种情况。已经做了很多搜索,但只能找到这个。这看起来对我的情况很好,因为即使我的想法是等待10秒来获得响应,如果它超过了超时时间,我需要处理它以在catch块中显示消息。但我无法根据我的代码实现它。有人能帮我吗?我将非常感谢。

在执行url之前添加以下代码

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
在try块中编写上述代码,并捕获
ConnectTimeOutException
SocketConnectionTimeOutException
。您可以在其中显示一些自定义对话框


您还可以通过检查HttpClient响应的状态行来判断它。

您的异常没有被命中,因为HttpClient没有抛出异常。可能您的HttpResponse代码中出现了错误,例如500。在try块中,添加以下行:

response.getStatusLine().getStatusCode()

然后读取状态代码。如果您的web服务器处于运行状态,您应该得到200分,但如果服务器处于运行状态,您可能会得到500分或其他。然后,您可以在此处处理需要隐藏旋转进度条的代码,并显示错误消息。

此答案与我在问题中提供的链接中的答案完全相同。请您澄清您的答案中的HttpConnectionParams是什么,以及如何根据我的代码进行操作。这就是我被这个答案困住的地方?@Korhan抱歉,我没有注意到这个链接。然而,这个答案应该适合你。如果设置了HttpConnectionParams,那么在执行url时,如果套接字有任何问题,它将等待您提供的超时毫秒,然后抛出SocketTimeoutException。这同样适用于连接超时。如果服务器关闭,而您无法连接到它,则它将等待连接建立,如果未成功,则抛出ConnectionTimeoutException。感谢您抽出时间。如果我不能尝试接球,我会检查这个。顺便说一句,你能详细说明确切的状态码吗?