网络通讯之间一般采用JSON发送数据。这里只是简单介绍一下HttpClient的GET、POST两种网络请求方式。
1.HttpClient访问网络步骤:
-创建HttpClient对象:
HttpClient httpclient = new DefaultHttpClient();
-创建HttpGet对象(或者HttpPost对象):
HttpGet httprequest = new HttpGet("http://www.baidu.com");
HttpPost httprequest = new HttpPost("http://www.baidu.com");
-如果需要发送请求参数,可以直接将要发送的参数接到URL地址中,也可以调用HttpGet的SetParams()方法添加请求参数;(HttpPost则采用SetParams()或者setEntity()方法)
-利用HttpClient对象的execute()发送请求,并返回一个HttpResp对象:
HttpResponse httpresponse = httpclient.execute(httprequest);
-调用HttpResponse的getEntity()方法,返回一个HttpEntity对象,通过此对象获取服务器的响应内容。
2、判断响应成功的语句:
httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK
3、HttpClient发送GET请求代码如下:
`
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.impl.conn.DefaultClientConnection;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button;
private TextView tv;
private Handler handler;
private String result = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.result);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
send();
Message m = handler.obtainMessage();
handler.sendMessage(m);
}
}).start();
}
});
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(result != null){
tv.setText(result);
}
super.handleMessage(msg);
}
};
}
public void send(){
String target = "http://www.baidu.com";
HttpClient httpclient = new DefaultHttpClient();//1. 创建httpclient对象
HttpGet httpget = new HttpGet(target);//2.创建httpget对象
HttpResponse httpresponse;
try {
httpresponse = httpclient.execute(httpget);//3.发请求
if(httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
result = EntityUtils.toString(httpresponse.getEntity()) ;//4.获取返回的字符串
}else{
result = "请求失败";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5、HttpClient发送POST请求:一般发送复杂数据时都利用POST
public void send(){
// String target = "http://192.168.1.101:8080/blog/deal_httpclient.jsp";
String target = "http://www.baidu.com";
HttpClient httpclient = new DefaultHttpClient();//1.创建httpclient 对象
HttpPost httprequest = new HttpPost(target);//2.创建httppost对象
//将要传递的参数保存到List集合中去
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("param", "POST"));//标记参数
params.add(new BasicNameValuePair("name", "lili"));//添加昵称
params.add(new BasicNameValuePair("content", "睡觉了"));//添加内容
try {
httprequest.setEntity(new UrlEncodedFormEntity(params,"utf-8"));//设置编码
HttpResponse httpresponse = httpclient.execute(httprequest);//3.发送请求
if(httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
result += EntityUtils.toString(httpresponse.getEntity());//4.4.获取返回的字符串
}else{
result = "请求失败";
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
6、activity_main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送GET请求" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
`