Android之HttpClient的GET和POST区别
来源:互联网 发布:淘宝里的金牌卖家 编辑:程序博客网 时间:2024/05/17 06:16
我们通过AsyncTask这个异步类来执行和界面的交互,然后将返回的数据给UI主线程。
你可以把HttpClient想象成一个浏览器,通过它的API我们可以很方便的发出GET,POST请求。
userName = (EditText) findViewById(R.id.editText1);passWord = (EditText) findViewById(R.id.editText2);Get传值方式:
MyAsyncTaskGet task = new MyAsyncTaskGet(result);String user = userName.getText().toString();String pass = passWord.getText().toString();task.execute("http://192.168.0.17:8080/HttpDemo/login.jsp?username="+user+"&password="+pass);
Android虚拟机ip使用10.0.2.2 实体机需要使用真实ipGet处理方式:
import java.io.IOException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import android.os.AsyncTask;import android.widget.TextView;public class MyAsyncTaskGet extends AsyncTask<String, Void, String> {private TextView textView;public MyAsyncTaskGet(TextView textView){this.textView = textView;}protected String doInBackground(String... params) {String httpUrl = params[0];//打开一个浏览器HttpClient client = new DefaultHttpClient();//输入网址HttpGet get = new HttpGet(httpUrl);//回车try {HttpResponse response = client.execute(get);//服务器响应状态int code = response.getStatusLine().getStatusCode();if(code == 200){//返回数据HttpEntity entity = response.getEntity();String result = EntityUtils.toString(entity); //EntityUtils是一个工具类,通过它可以将数据转换成想要的格式 return result;}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}protected void onPostExecute(String result) {super.onPostExecute(result);textView.setText(result);}}
POST传值方式:MyAsyncTaskPost taskPost = new MyAsyncTaskPost(result);taskPost.execute("http://192.168.0.17:8080/HttpDemo/login.jsp",userName.getText().toString(),passWord.getText().toString());POST处理方式:
由于POST方式不能在url上直接传递参数,故通过HttpPost的setEntity方法设置键值对来作为参数。
import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import android.os.AsyncTask;import android.widget.TextView;public class MyAsyncTaskPost extends AsyncTask<String, Void, String> {private TextView textView;public MyAsyncTaskPost(TextView textView){this.textView = textView;}protected String doInBackground(String... params) {String httpUrl = params[0];String username = params[1];String password = params[2];HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(httpUrl);//UrlEncodedFormEntity里面的参数是一个List<NameValuePair>集合List<NameValuePair> list = new ArrayList<NameValuePair>();list.add(new BasicNameValuePair("username", username));list.add(new BasicNameValuePair("password", password));try {//setEntity里的参数是一个HttpEntity实体,这是一个接口,不能new出对象,所以用了一个继承它的类post.setEntity(new UrlEncodedFormEntity(list));HttpResponse response = client.execute(post);int code = response.getStatusLine().getStatusCode();if(code == 200){HttpEntity entity = response.getEntity();//通过EntityUtils工具类将返回的数据转换String result = EntityUtils.toString(entity);return result;}} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}protected void onPostExecute(String result) {super.onPostExecute(result);textView.setText(result);}}
0 0
- Android之HttpClient的GET和POST区别
- httpclient中get和post的区别
- HttpClient 的Get和Post
- HttpClient的get和post
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- HttpClient应用之GET和POST申请
- httpclient的post和get,httpurlconnection的post和get
- Android HttpClient基本使用方法!!--get和post
- Android HttpClient POST GET
- http之get和post的区别
- HttpClient Get和Post
- Android POST和GET传输的区别
- httpclient的get和post请求
- Post和Get在HttpClient的使用
- HttpClient的Post和Get访问网页
- HttpClient的Post和Get方法
- HttpClient的get和post请求数据
- applicationContext.xml文件中的异常: Attribute : name The name of the property, following JavaBean naming c
- 反汇编基本原理与x86指令构造
- 用maven创建基于wink的rest服务-传送文件(字符串和字节数组)(一)
- 基于VideoInput的摄像头操作类
- backbone实例todos分析
- Android之HttpClient的GET和POST区别
- wifidog 认证 php
- 上台阶、硬币问题的算法
- nginx学习笔记1
- 翻硬币
- 一种一天成为高手的另类Sketch up速成法
- nginx学习笔记2
- kindeditor去掉图片空间
- Unity3D研究院之深入理解Unity脚本的执行顺序(六十二)