1. Http 访问方式
1.1 HttpPost
1) setEntity() //设置请求参数
1.2 HttpGet
2. Http 客户端代理
2.1 HttpClient
1) execute( HttpUriRequest request ) //request 为访问方式,执行客户端发送请求
2.2 HttpResponse // HttpClient execute 执行的访问结果实例
1) getStatusLine() //可以获得返回状态码、协议信息等
3. Http Post 几个重要 点
3.1 BasicNameValuePair //封装请求头字段
3.2 UrlEncodedFormEntity //对自定义请求头进行URL编码
3.3 InputStream in = response.getEntity().getContext(); //服务器返回的数据 , response 为 HttpResponse
3.4 int code = response.getStatusLine().getStatusCode(); //返回服务器状态码
package org.xiazdong.network.httpclient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText name, age;
private Button getbutton, postbutton;
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
try{
if(postbutton==v){
/*
* NameValuePair代表一个HEADER,List<NameValuePair>存储全部的头字段
* UrlEncodedFormEntity类似于URLEncoder语句进行URL编码
* HttpPost类似于HTTP的POST请求
* client.execute()类似于发出请求,并返回Response
*/
DefaultHttpClient client = new DefaultHttpClient();
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair pair1 = new BasicNameValuePair("name", name.getText().toString());
NameValuePair pair2 = new BasicNameValuePair("age", age.getText().toString());
list.add(pair1);
list.add(pair2);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
HttpPost post = new HttpPost("http://192.168.0.103:8080/Server/Servlet1");
post.setEntity(entity);
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode()==200){
InputStream in = response.getEntity().getContent();//接收服务器的数据,并在Toast上显示
String str = readString(in);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
else Toast.makeText(MainActivity.this, "POST提交失败", Toast.LENGTH_SHORT).show();
}
if(getbutton==v){
DefaultHttpClient client = new DefaultHttpClient();
StringBuilder buf = new StringBuilder("http://192.168.0.103:8080/Server/Servlet1");
buf.append("?");
buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");
buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));
HttpGet get = new HttpGet(buf.toString());
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
InputStream in = response.getEntity().getContent();
String str = readString(in);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) this.findViewById(R.id.name);
age = (EditText) this.findViewById(R.id.age);
getbutton = (Button) this.findViewById(R.id.getbutton);
postbutton = (Button) this.findViewById(R.id.postbutton);
getbutton.setOnClickListener(listener);
postbutton.setOnClickListener(listener);
}
protected String readString(InputStream in) throws Exception {
byte[]data = new byte[1024];
int length = 0;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while((length=in.read(data))!=-1){
bout.write(data,0,length);
}
return new String(bout.toByteArray(),"UTF-8");
}
}