HttpClient的使用方法
实例解析:HttpClient的get,post的数据传输
package com.example.httpclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URLEncoder; 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.app.Activity; import android.os.AsyncTask; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AsyncTask.execute(new Runnable() { @Override public void run() { try { callByGet(); } catch (Exception e) { } } }); } public void callByGet() throws Exception { HttpClient client = new DefaultHttpClient(); HttpGet get= new HttpGet( "http://192.168.48.176:8081/myweb/CheckGet?name=aaa&pwd="+URLEncoder.encode("你好", "utf-8")); //重新编码,解决中文乱码问题 HttpResponse response = client.execute(get);//向服务器发送请求,获取响应数据 HttpEntity entity = response.getEntity();//把相应数据包装成HttpEntity对象,方便打印 System.out.println(EntityUtils.toString(entity)); //获得内容转换成页面存储起来: BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent())); } public void callByPost(){ } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-13 15:10:08