Android用Apache HttpClient 实现POST和Get请求
16lz
2022-04-22
类 : org.apache.http.client.HttpClient;
1. GET实现
Java代码- packagecom.yarin.android.Examples_08_02;
- importjava.io.IOException;
- importorg.apache.http.HttpResponse;
- importorg.apache.http.HttpStatus;
- importorg.apache.http.client.ClientProtocolException;
- importorg.apache.http.client.HttpClient;
- importorg.apache.http.client.methods.HttpGet;
- importorg.apache.http.impl.client.DefaultHttpClient;
- importorg.apache.http.util.EntityUtils;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.TextView;
- publicclassActivity02extendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.http);
- TextViewmTextView=(TextView)this.findViewById(R.id.TextView_HTTP);
- //http地址
- StringhttpUrl="http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
- //HttpGet连接对象
- HttpGethttpRequest=newHttpGet(httpUrl);
- try{
- //取得HttpClient对象
- HttpClienthttpclient=newDefaultHttpClient();
- //请求HttpClient,取得HttpResponse
- HttpResponsehttpResponse=httpclient.execute(httpRequest);
- //请求成功
- if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
- //取得返回的字符串
- StringstrResult=EntityUtils.toString(httpResponse
- .getEntity());
- mTextView.setText(strResult);
- }else{
- mTextView.setText("请求错误!");
- }
- }catch(ClientProtocolExceptione){
- mTextView.setText(e.getMessage().toString());
- }catch(IOExceptione){
- mTextView.setText(e.getMessage().toString());
- }catch(Exceptione){
- mTextView.setText(e.getMessage().toString());
- }
- //设置按键事件监听
- Buttonbutton_Back=(Button)findViewById(R.id.Button_Back);
- /*监听button的事件信息*/
- button_Back.setOnClickListener(newButton.OnClickListener(){
- publicvoidonClick(Viewv){
- /*新建一个Intent对象*/
- Intentintent=newIntent();
- /*指定intent要启动的类*/
- intent.setClass(Activity02.this,Activity01.class);
- /*启动一个新的Activity*/
- startActivity(intent);
- /*关闭当前的Activity*/
- Activity02.this.finish();
- }
- });
- }
- }
2. POST
Java代码
- packagecom.yarin.android.Examples_08_02;
- importjava.io.IOException;
- importjava.util.ArrayList;
- importjava.util.List;
- importorg.apache.http.HttpEntity;
- importorg.apache.http.HttpResponse;
- importorg.apache.http.HttpStatus;
- importorg.apache.http.NameValuePair;
- importorg.apache.http.client.ClientProtocolException;
- importorg.apache.http.client.HttpClient;
- importorg.apache.http.client.entity.UrlEncodedFormEntity;
- importorg.apache.http.client.methods.HttpPost;
- importorg.apache.http.impl.client.DefaultHttpClient;
- importorg.apache.http.message.BasicNameValuePair;
- importorg.apache.http.util.EntityUtils;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.TextView;
- publicclassActivity03extendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.http);
- TextViewmTextView=(TextView)this.findViewById(R.id.TextView_HTTP);
- //http地址
- StringhttpUrl="http://192.168.1.110:8080/httpget.jsp";
- //HttpPost连接对象
- HttpPosthttpRequest=newHttpPost(httpUrl);
- //使用NameValuePair来保存要传递的Post参数
- List<NameValuePair>params=newArrayList<NameValuePair>();
- //添加要传递的参数
- params.add(newBasicNameValuePair("par","HttpClient_android_Post"));
- try{
- //设置字符集
- HttpEntityhttpentity=newUrlEncodedFormEntity(params,"gb2312");
- //请求httpRequest
- httpRequest.setEntity(httpentity);
- //取得默认的HttpClient
- HttpClienthttpclient=newDefaultHttpClient();
- //取得HttpResponse
- HttpResponsehttpResponse=httpclient.execute(httpRequest);
- //HttpStatus.SC_OK表示连接成功
- if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
- //取得返回的字符串
- StringstrResult=EntityUtils.toString(httpResponse.getEntity());
- mTextView.setText(strResult);
- }else{
- mTextView.setText("请求错误!");
- }
- }catch(ClientProtocolExceptione){
- mTextView.setText(e.getMessage().toString());
- }catch(IOExceptione){
- mTextView.setText(e.getMessage().toString());
- }catch(Exceptione){
- mTextView.setText(e.getMessage().toString());
- }
- //设置按键事件监听
- Buttonbutton_Back=(Button)findViewById(R.id.Button_Back);
- /*监听button的事件信息*/
- button_Back.setOnClickListener(newButton.OnClickListener(){
- publicvoidonClick(Viewv){
- /*新建一个Intent对象*/
- Intentintent=newIntent();
- /*指定intent要启动的类*/
- intent.setClass(Activity03.this,Activity01.class);
- /*启动一个新的Activity*/
- startActivity(intent);
- /*关闭当前的Activity*/
- Activity03.this.finish();
- }
- });
- }
- }
Apacheorg.apache.http.client.HttpClient;
更多相关文章
- 【开发工具】判断请求源是 手机 or PC
- android 打开文件
- Android(安卓)fragment中如何对listview添加监听事件
- Android(安卓)判断软键盘的状态(显示,隐藏)
- Android(安卓)Http网络开发神兵利器
- 根据请求头跳转判断Android&iOS
- android手势滑动——左右滑动效果实现
- Android(安卓)Socket网络通信
- android http请求并解析返回的xml