我们通过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 实体机需要使用真实ip
Get处理方式:
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); } }
Android之HttpClient的GET和POST区别
最新推荐文章于 2021-05-27 08:30:38 发布