Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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 Get和Post方法异常?_Android_Http - Fatal编程技术网

android中的Http Get和Post方法异常?

android中的Http Get和Post方法异常?,android,http,Android,Http,我正在使用Http get和post方法进行Http连接 我只是想问一下,在使用它们的过程中会出现什么异常。 我知道列表可能太长了,但有人能告诉我发生和必须处理的一般和频繁的异常情况吗? 我的代码是: public class httpconnection { HttpClient client=new DefaultHttpClient(); InputStream in=null; public InputStream httpreponse_pos

我正在使用Http get和post方法进行Http连接 我只是想问一下,在使用它们的过程中会出现什么异常。 我知道列表可能太长了,但有人能告诉我发生和必须处理的一般和频繁的异常情况吗? 我的代码是:

public class httpconnection
    {

    HttpClient client=new DefaultHttpClient();
      InputStream in=null;


    public InputStream httpreponse_post(String url,List<NameValuePair> params)
     {
      try {

    HttpPost post = new HttpPost(url); 
        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
        post.setEntity(ent);
        HttpResponse responsePOST = client.execute(post);  
        HttpEntity resEntity = responsePOST.getEntity();  
        in=resEntity.getContent();
        in.close();

} catch (ClientProtocolException e)
 {
 Log. e ("Client Protocol Exception", e.toString ());
 }
 catch (IOException e) {
 Log. e ("Input Output Exception", e.toString ());
  }
  return in;
    }



 public InputStream httpreponse_get(String url)
  {
     try 
    {


  //String getURL = "http://www.google.com";
 HttpGet get = new HttpGet(url);
 HttpResponse responseGet = client.execute(get);
 HttpEntity resEntityGet = responseGet.getEntity();
     in=resEntityGet.getContent();
     in.close();
 } 

 catch (IOException e) {
 Log. e ("Input Output Exception", e.toString ());
  }
  return in;
        }
     }
公共类httpconnection
{
HttpClient=new DefaultHttpClient();
InputStream in=null;
公共InputStream HttpReporte_post(字符串url,列表参数)
{
试一试{
HttpPost=新的HttpPost(url);
UrlEncodedFormEntity ent=新的UrlEncodedFormEntity(params,HTTP.UTF_8);
邮政实体(ent);
HttpResponse responsePOST=client.execute(post);
HttpEntity当前状态=responsePOST.getEntity();
in=resEntity.getContent();
in.close();
}捕获(客户端协议例外e)
{
Log.e(“客户端协议异常”,e.toString());
}
捕获(IOE异常){
e(“输入输出异常”,e.toString());
}
返回;
}
公共输入流httpreponse\u get(字符串url)
{
尝试
{
//字符串getURL=”http://www.google.com";
HttpGet=新的HttpGet(url);
HttpResponse responseGet=client.execute(get);
HttpEntity-resEntityGet=responseGet.getEntity();
in=resEntityGet.getContent();
in.close();
} 
捕获(IOE异常){
e(“输入输出异常”,e.toString());
}
返回;
}
}

这里有一些例外,您至少应该在代码中包含它们:

catch (UnsupportedEncodingException e)  //POST section
catch (NullPointerException e) //POST & GET section
catch (ClientProtocolException e)  //POST section
catch (IOException e) // POST & GET section
如果您需要确保捕获除上述之外可能发生的任何其他异常,只需在最后一个catch语句中添加常规异常。这应该能覆盖你的代码

catch (Exception e)

这里有一些例外情况,您至少应该在代码中包含它们:

catch (UnsupportedEncodingException e)  //POST section
catch (NullPointerException e) //POST & GET section
catch (ClientProtocolException e)  //POST section
catch (IOException e) // POST & GET section
如果您需要确保捕获除上述之外可能发生的任何其他异常,只需在最后一个catch语句中添加常规异常。这应该能覆盖你的代码

catch (Exception e)

Thanx…我确实在我的代码中添加了它们。这非常有用。但是当出现网络超时时会发生什么呢?IOException已经处理了这个问题,但是如果需要更具体的说明,请使用ConnectTimeoutException(在IOException捕获块之前)。不客气,谢琳。谢谢你的帮助……)Thanx…我确实在我的代码中添加了它们。这非常有用。但是当出现网络超时时会发生什么呢?IOException已经处理了这个问题,但是如果需要更具体的说明,请使用ConnectTimeoutException(在IOException捕获块之前)。不客气,谢琳。谢谢你的帮助……)