HTTP GET和android中Post方法异常? [英] Http Get and Post method exceptions in android?

查看:137
本文介绍了HTTP GET和android中Post方法异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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节

如果你需要确保赶除了上述可能出现的任何其他异常,只需添加上的最后一个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)

这篇关于HTTP GET和android中Post方法异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆