转载自:http://blog.sina.com.cn/s/blog_a364999b01019u37.html
android如何实现服务器和客户端的数据传递
我就以我做的一个无线点菜系统为例,服务器端放在pc上,使用tomcat服务器,用servlet写的服务器端,客户端用android。
1.
首先看客户端在客户端我们要发送请求我要得到什么,那么服务器端就看你出的条件了,所谓条件就是参数,你传参数是不传参数,如果不传参数很好办,传参数也好办,哈哈
现在我们看不传参数怎么办,我们把请求的方法全部写在HttpUitl这个类里边
代码如下:
packagecom.Mobile.util;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.params.HttpConnectionParams;
importorg.apache.http.params.HttpParams;
importorg.apache.http.util.EntityUtils;
publicclass HttpUtil {
//
Public static final StringBASE_URL="http://10.10.120.147:8080/WirelessOrder_Service/";
//
public static HttpGet getHttpGet(String url){
HttpGet request = new HttpGet(url);
return request;
}
//
public static HttpPost getHttpPost(String url){
HttpPost request = new HttpPost(url);
return request;
}
//
public static HttpResponsegetHttpRespo
HttpResponse response = newDefaultHttpClient().execute(request);
return response;
}
//
public static HttpResponse getHttpResponse(HttpPostrequest) throws ClientProtocolException, IOException{
HttpResponse response = newDefaultHttpClient().execute(request);
return response;
}
//
public static String queryStringForPost(Stringurl){
//
HttpPost request =HttpUtil.getHttpPost(url);
String result = null;
try {
//
HttpResponse response =HttpUtil.getHttpResponse(request);
//
if(response.getStatusLine().getStatusCode()==200){
//
result =EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
//
public static StringqueryStringForPost
String result = null;
try {
//
HttpResponse response =HttpUtil.getHttpResponse(request);
//
if(response.getStatusLine().getStatusCode()==200){
//
result =EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
//
public static
String queryStringForGet(String url){
//
HttpGet request =HttpUtil.getHttpGet(url);
String result = null;
try {
//
HttpResponse response =HttpUtil.getHttpResponse(request);
//
if(response.getStatusLine().getStatusCode()==200){
//
result =EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
//这个方法是用jSON请求获得JSON
public static String getContent(String url)
{
StringBuilder sb=new StringBuilder();
HttpClient client=newDefaultHttpClient();
HttpParamshttpparams=client.getParams();
HttpConnectionParams.setConnectionTimeout(httpparams,3000);
HttpConnectionParams.setSoTimeout(httpparams,5000);
try {
HttpResponseresponse=client.execute(new HttpGet(url));
HttpEntityentity=response.getEntity();
if(entity!=null)
{
BufferedReaderreader=new BufferedReader(new InputStreamReader(entity.getContent(),"utf8"),8192);
String line=null;
while((line=reader.readLine())!=null)
{
sb.append(line+"\n");
}
reader.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
}
return sb.toString();
}
}
下面的代码是客户端请求,插入用户信息
ArrayListlist=new
list.add(new
list.add(new
list.add(new
list.add(new
list.add(new
UrlEncodedFormEntity entity1=null;
try
entity1=
new
}
e.printStackTrace();
}
String url=HttpUtil.BASE_URL+"/InsertServlet";
HttpPostrequest = HttpUtil.getHttpPost(url);
下面是用JSON获得订单的例子
String url=HttpUtil.BASE_URL+"/SelectOrderListServlet?"+"orderid="+orde;
String body=HttpUtil.getContent(url);//这个方法在HttpUtil类里边
try
JSONArray array=new
for(int
{
JSONObject obj=array.getJSONObject(i);
HashMap
m.put("name", obj.getString("name"));
m.put("price", obj.getString("price"));
m.put("num",obj.getString("num"));
m.put("menuid",obj.getString("menuid"));
orderlist.add(m);
}
}
//
e.printStackTrace();
}