Android中基于Http协议的网络通信的两种方法Get和Post

废话就不多说了直接上代码,很多的解释就直接写在代码中了!

第一种方法:Get方法:

public class Get {


private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private String url;
Handler handler;
//构造函数 用来 给UI界面传递参数,例如这个就是传递了URL和handler,当在IU界面进行操作 接收数据的时候,就会用到这些参数。
public Get(String url, Handler handler) {
// TODO Auto-generated constructor stub
this.url = url;
this.handler = handler;
}
//这个网络通信基于android的Http协议之上的。
public void Httpget() {
//为什么 要开辟一个新的线程呢,其实这个呢,我也不太清楚,只知道资料上说:android2.3还是哪个版本之后,说IU界面不能和这个使用同一个线程,否则会报错,程序停止运行
new Thread() {
@Override
public void run() {
// 先生成一个请求对象,String url = baseURL + "?" + "UserOID=" + str;地址中包含了参数。
HttpGet httpget = new HttpGet(url);
// 生成一个客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 使用Http客户端发送请求对象
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpget);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
// 当我的请求发送成功时,将接受的数据传进来
if (httpResponse.getStatusLine().getStatusCode() == 200) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
//定义的一个字符串对象,用来存储从服务器上接受到的字符!
String result = "";
//line是一个或者一行一行的读取,最后存储在result中!
String line = "";
while ((line = reader.readLine()) != null) {
result = result + line;
Message msg = new Message();
msg.obj = result;
handler.sendMessage(msg);
}


}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();


}


}
第二种方法:post方法:

//post方式向服务器发送数据
public class Post {
protected static final String DataID = null;
protected static final String DeviceEntityAID = null;
protected static final String Value = null;
// HttpResponse/HttpEntity 是接口
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private String str;
Handler handler;
//创建构造函数传入参数 str,handler
public Post(String str,Handler handler) {
this.str = str;
this.handler=handler;
}


public void SendMssage() {
new Thread() {


@Override
public void run() {
try {
// 生成一个客户端对象httpClient
HttpClient httpClient = new DefaultHttpClient();
// 生成一个请求对象,"http://192.168.11.5:3021/serviceUp/cmd/sendCMDDispatcher"就是你的服务器的地址,添加你自己的服务器的地址就ok了
HttpPost httpPost = new HttpPost(
"http://192.168.11.5:3021/serviceUp/cmd/sendCMDDispatcher");
// 建立一个NameValuePair数组,用于存储欲传送的参数,参数放在了数组中。
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

//list.add(new BasicNameValuePair("name", name));

                 //list.add(new BasicNameValuePair("pwd", pwd));  
 
// 添加参数,这些参数根据自己服务器上的数据格式设置,主要是看你的的服务器上的数据格式的
str = "[";
str += "{";
str += "\"DeviceEntityAID\":" + "\"" + DeviceEntityAID
+ "\"";
str += ",";
str += "\"DataID\"" + ":" + "\"" + DataID + "\"";
str += ",";
str += "\"Value\"" + ":" + "\"" + Value + "\"";
str += "}";
str += "]";


// String
// abc="[{\"DeviceEntityAID\":\"02000200\",\"DataID\":\"02060000\",\"Value\":\"0003\"}]";
//设置参数 cmdContent是服务器端的key值,str为自己要输入的参数,其字符格式由服务器端后决定,
//将传送的参数通过add的方法添加到BasicNameValuePair类的对象中。
postParameters
.add(new BasicNameValuePair("cmdContent", str));
// 设置编码,这个一般情况下都是默认的UTF-8编码格式
httpPost.setEntity(new UrlEncodedFormEntity(postParameters,
"UTF-8"));
// 发送Post,并返回一个httpResponse对象
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
//HttpPost链接解析xml,response.getStatusLine().getStatusCode()==200链接成功,==500失败。
if (httpResponse.getStatusLine().getStatusCode() == 200) {
//读取字符串
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpEntity.getContent()));
String result = "";
String line = "";
while ((line = reader.readLine()) != null) {
result = line;
//通过Message的类创建一个msg对象
  Message msg = new Message();
  //使用其对象msg调用message中的obj方法
msg.obj = result;
handler.sendMessage(msg);
   
}


}


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpEntity.getContent().close();
} catch (Exception e) {
e.printStackTrace();
}


}
}


}.start();
}
}

两者之间的不同: 由此我们可知,HttpGet和HttpPost的区别在于前者是将参数在地址中传递,后者是将参数用List传递。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值