--------- 部分实例
try {
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent()));
for (String s = reader.readLine(); s != null; s = reader
.readLine()) {
builder.append(s);
}
JSONObject jsonObject = new JSONObject(builder.toString());
error = jsonObject.getInt("errno");
String errormsg = jsonObject.getString("errmsg");
Log.v("wgp",myget.getURI().toString());
Log.v("wgp", "error=" + error);
Log.v("wgp", "errormsg=" + errormsg);
if (error == 0) {
array = jsonObject.getJSONArray("data");
mFavoriteList = new ArrayList<FavoriteItem>();
FavoriteItem item = null;
String srcType;
for (int i = 0; i < array.length(); i++) {
item = new FavoriteItem();
item.setId(array.getJSONObject(i).getInt("id"));
item.setUserId(array.getJSONObject(i).getInt("userid"));
item.setUsername(array.getJSONObject(i).getString("username"));
item.setType(array.getJSONObject(i).getInt("type"));
item.setCid(array.getJSONObject(i).getInt("cid"));
item.setTitle(array.getJSONObject(i).getString("title"));
item.setUrl(array.getJSONObject(i).getString("url"));
item.setTag(array.getJSONObject(i).getString("tag"));
item.setSrc(array.getJSONObject(i).getString("src"));
item.setCreateDate(array.getJSONObject(i).getString("created"));
item.setModifiedDate(array.getJSONObject(i).getString("modified"));
srcType = array.getJSONObject(i).getString("src_type");
if(srcType != null && srcType.equals("cps")){
item.setSrc_type("industry");
}else{
item.setSrc_type(srcType);
}
mFavoriteList.add(item);
}
if (mFavoriteList.size() == 0) {
handler.sendEmptyMessage(-1);
} else {
handler.sendEmptyMessage(1);//更新主UI
}
}
} catch (Exception e) {
e.printStackTrace();
handler.sendEmptyMessage(-1);
} finally {
}
=============
Android SDK集成了Apache HttpClient模块。要注意的是,这里的Apache HttpClient模块是 HttpClient 4.0(org.apache.http.*),而不是常见的 Jakarta Commons HttpClient 3.x(org.apache.commons.httpclient.*)。
HttpClient常用HttpGet和HttpPost这两个类,分别对应Get方式和Post方式。
无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源。
1.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。
2.使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST请求,并返回HttpResponse对象。
3.通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理。
如果使用HttpPost方法提交HTTP POST请求,则需要使用HttpPost类的setEntity方法设置请求参数。参数则必须用NameValuePair[]数组存储。
HttpGet
- 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;
- }
HttpPost
如果使用HttpPost方法提交HTTP POST请求,则需要使用HttpPost类的setEntity方法设置请求参数。参数则必须用NameValuePair[]数组存储。
- public String doPost()
- {
- String uriAPI = "http://XXXXXX";//Post方式没有参数在这里
- String result = "";
- HttpPost httpRequst = new HttpPost(uriAPI);//创建HttpPost对象
- List <NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("str", "I am Post String"));
- try {
- httpRequst.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
- HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
- if(httpResponse.getStatusLine().getStatusCode() == 200)
- {
- HttpEntity httpEntity = httpResponse.getEntity();
- result = EntityUtils.toString(httpEntity);//取出应答字符串
- }
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- result = e.getMessage().toString();
- }
- 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;
- }
以发送连接请求时,需要设置链接超时和请求超时等参数,否则会长期停止或者崩溃。
- HttpParams httpParameters = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParameters, 10*1000);//设置请求超时10秒
- HttpConnectionParams.setSoTimeout(httpParameters, 10*1000); //设置等待数据超时10秒
- HttpConnectionParams.setSocketBufferSize(params, 8192);
- HttpClient httpclient = new DefaultHttpClient(httpParameters); //此时构造DefaultHttpClient时将参数传入
- 由于是联网,在AndroidManifest.xml中添加网络连接的权限
- <uses-permission android:name="android.permission.INTERNET"/>
相关推荐
httpClient httpPost httpGet 所需jar包 httpClient httpPost httpGet 所需jar包
HttpClient模拟http发送post和get请求
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
HttpClient实现POST GET和文件下载
http请求工具类HttpClientUtil,get,post请求(csdn)————程序
httpclient的用法,发送get请求和post请求,设置header
java HttpClient 发送GET请求和带有表单参数的POST请求教程例子
利用c# httpclient ,post json数据到web api
MFC使用H偷偷跑Client的GET和POST方法 MFC使用H偷偷跑Client的GET和POST方法
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
本代码是httpclient发送get请求和post请求demo,具体描述参考博客 HttpClient发送get请求和post请求
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解(这款工具类是我精心挑选适合广大java程序猿使用)
介绍如何通过HttpClient模块来创建Http连接,并分别以Http GET与Http POST方法来传递参数,连接之后取回Web Server的返回网页结果。重点是如何使用HttpClient的模块来完成Http的请求与应答。 分享参考自Android SDK...
分别演示了android中Http和HttpClient的Get和Post方法的使用,还有加载图片的演示
mfc http post 和get 请求
NULL 博文链接:https://javasam.iteye.com/blog/2117845
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解
httpclient get/post请求工具类(map参数封装),方便调用
Android Studio使用HttpClient请求数据(get请求)不包括post请求
小小测试项目包含了4个知识点,其中包含了http协议向服务器传输数据的post和get方法,包含了httpclient传输方法,httpclient上传文件到服务器的方法,