1.HttpClient使用流程
①.創建HttpClient對象: HttpClient httpClient = new DefaultHttpClient();
②發送 GET 請求,創建 HttpGet 對象;發送 POST 請求,創建 HttpPost 對象。
③設置請求參數:兩者都可以用setParams(HttpParams);post 請求還可以用setEntity(HttpEntity)。
④調用httpClient對象的execute()方法發送請求,會返回一個 HttpResponse。
⑤對返回的HttpResponse調用,getEntity()方法可以獲得HttpEntity對象,該對象包含了服務器響應內容。
1.GET 請求
public class MainActivity extends Activity implements OnClickListener {
private Button btnGet;
private WebView wView;
public static final int SHOW_DATA = 0X123;
private String detail = "";
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == SHOW_DATA)
{
wView.loadDataWithBaseURL("",detail, "text/html","UTF-8","");
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setView();
}
private void initView() {
btnGet = (Button) findViewById(R.id.btnGet);
wView = (WebView) findViewById(R.id.wView);
}
private void setView() {
btnGet.setOnClickListener(this);
wView.getSettings().setDomStorageEnabled(true);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnGet) {
GetByHttpClient();
}
}
private void GetByHttpClient() {
new Thread()
{
public void run()
{
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.w3cschool.cc/python/python-tutorial.html");
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
detail = EntityUtils.toString(entity, "utf-8");
handler.sendEmptyMessage(SHOW_DATA);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
}
另外,如果是帶有參數的GET請求的話,我們可以將參數放到一個List集合中,再對參數進行URL編碼, 最後和URL拼接下就好了:
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("user", "豬小弟"));
params.add(new BasicNameValuePair("pawd", "123"));
String param = URLEncodedUtils.format(params, "UTF-8");
HttpGet httpGet = new HttpGet("http://www.baidu.com"+"?"+param);
2.POST 請求
POST請求比GET稍微複雜一點,創建完HttpPost對象後,通過NameValuePair集合來存儲等待提交 的參數,並將參數傳遞到UrlEncodedFormEntity中,最後調用setEntity(entity)完成, HttpClient.execute(HttpPost)即可;
private void PostByHttpClient(final String url)
{
new Thread()
{
public void run()
{
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "豬大哥"));
params.add(new BasicNameValuePair("pawd", "123"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity2 = httpResponse.getEntity();
detail = EntityUtils.toString(entity2, "utf-8");
handler.sendEmptyMessage(SHOW_DATA);
}
}catch(Exception e){e.printStackTrace();}
};
}.start();
}
轉自:http://www.runoob.com/w3cnote/android-tutorial-httpclient.html