1.HttpClient的学习
HttpClient使用流程:
①创建HttpClient对象:HttpClient client = new DefaultHttpClient0;
②发送Get请求的话,创建HttpGet对象;发送Post请求的话创建HttpPost对象
③设置请求参数:两者都可以用setParams(HttpParams);Post还可用setEnity(HttpEntity)方法
④调用httpClient对象的execute)方法发送请求,该方法会返回一个HttpResponse
⑤对返回的HttpResponse调用:getAllHeaders().getHeaders(name)等方法可以获得服务器的响应头调用getEntity0方法可以取得HttpEntity对象,改对象包装了服务器的响应内容
使用HttpClient发送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