HTTP GET和android中Post方法异常?异常、方法、GET、HTTP

2023-09-06 22:17:04 作者:無愛ー身輕

我使用HTTP GET和POST用于HTTP连接方法我只是想问什么可以使用它们发生的所有异常。我知道名单可能太长了,但有人可以告诉我,发生,必须处理一般的和频繁的异常?我的code是:

 公共类的HttpConnection    {    HttpClient的客户端=新DefaultHttpClient();      在的InputStream = NULL;    公众的InputStream HTT preponse_post(字符串URL,列表和LT;&的NameValuePair GT; PARAMS)     {      尝试{    HttpPost后=新HttpPost(URL);        UrlEn codedFormEntity ENT =新UrlEn codedFormEntity(参数,可以HTTP.UTF_8);        post.setEntity(ENT);        HTT presponse responsePOST = client.execute(岗位);        HttpEntity resEntity = responsePOST.getEntity();        在= resEntity.getContent();        附寄();}赶上(ClientProtocolException E) { 日志。 E(客户协议异常,e.toString()); } 赶上(IOException异常五){ 日志。 E(输入输出异常,e.toString());  }  返回;    } 公众的InputStream HTT preponse_get(字符串URL)  {     尝试    {  //字符串的getURL =htt​​p://www.google.com HTTPGET GET =新HTTPGET(URL); HTT presponse responseGet = client.execute(获取); HttpEntity resEntityGet = responseGet.getEntity();     在= resEntityGet.getContent();     附寄(); } 赶上(IOException异常五){ 日志。 E(输入输出异常,e.toString());  }  返回;        }     } 

解决方案

下面是一些例外,你至少应该包括他们在您的code:

 赶上(UnsupportedEncodingException五)// POST节赶上(NullPointerException异常E)// POST和放大器; GET节赶上(ClientProtocolException E)// POST节赶上(IOException异常E)// POST和放大器; GET节 
Android操作不当遇到的异常

如果你需要确保赶除了上述可能出现的任何其他异常,只需添加上的最后一个catch语句的一般例外。这应该让你的code覆盖。

 赶上(例外五) 

I am using Http get and post methods for Http Connection I just wanted to ask that what all exception can occur in using them. I know list may be too long but can someone tell me the general and frequent exceptions that occur and must be handled? my code is :

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;
        }
     }

解决方案

Here's a few exception that you should at least include them in your code:

catch (UnsupportedEncodingException e)  //POST section
catch (NullPointerException e) //POST & GET section
catch (ClientProtocolException e)  //POST section
catch (IOException e) // POST & GET section

And if you need to make sure to catch any other exceptions that might occur besides mentioned above, just add the general exception on the last catch statement. That should get your code covered.

catch (Exception e)