您的位置:首页 > 理论基础 > 计算机网络

httpclient请求去掉返回结果string中的多余转义字符

2017-10-03 15:57 881 查看
public String doGet()

{

String uriAPI = "http://XXXXX?str=I+am+get+String";

String result= "";

// HttpGet httpRequst = new HttpGet(URI uri);

// HttpGet httpRequst = new HttpGet(String uri);

// 创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。

HttpGet httpRequst = new HttpGet(uriAPI);

// new DefaultHttpClient().execute(HttpUriRequst requst);

try {

//使用DefaultHttpClient类的execute方法发送HTTP GET请求,并返回HttpResponse对象。

HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);//其中HttpGet是HttpUriRequst的子类

if(httpResponse.getStatusLine().getStatusCode() == 200)

{

HttpEntity httpEntity = httpResponse.getEntity();

result = EntityUtils.toString(httpEntity);//取出应答字符串

// 一般来说都要删除多余的字符

result.replaceAll("\r", "");//去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格

}

else

httpRequst.abort();

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

result = e.getMessage().toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

result = e.getMessage().toString();

}

return result;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐