android 客户端访问服务器端

时间:2022-02-06 18:22:07
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 {


// 基础URL


Public static final StringBASE_URL="http://10.10.120.147:8080/WirelessOrder_Service/";


// 获得Get请求对象request


public static HttpGet getHttpGet(String url){


HttpGet request = new HttpGet(url);



return request;


}


// 获得Post请求对象request


public static HttpPost getHttpPost(String url){



HttpPost request = new HttpPost(url);



return request;


}


// 根据请求获得响应对象response


public static HttpResponsegetHttpResponse(HttpGet request) throws ClientProtocolException, IOException{


HttpResponse response = newDefaultHttpClient().execute(request);


return response;


}


// 根据请求获得响应对象response


public static HttpResponse getHttpResponse(HttpPostrequest) throws ClientProtocolException, IOException{


HttpResponse response = newDefaultHttpClient().execute(request);


return response;


}



// 发送Post请求,获得响应查询结果


public static String queryStringForPost(Stringurl){


// 根据url获得HttpPost对象


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(HttpPost request){


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;


}


// 发送Get请求,获得响应查询结果


public static
String queryStringForGet(String url){


// 获得HttpGet对象


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();


}

}

下面的代码是客户端请求,插入用户信息

ArrayList<NameValuePair>list=new ArrayList<NameValuePair>();


list.add(
new BasicNameValuePair("username",us));


list.add(
new BasicNameValuePair("password",pw));


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


list.add(
new BasicNameValuePair("weight",we));


list.add(
new BasicNameValuePair("sex",se));


UrlEncodedFormEntity entity1=
null;


try {



entity1=
new UrlEncodedFormEntity(list,HTTP.UTF_8);


}
catch (UnsupportedEncodingException e) {


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 JSONArray(body);


for(int i=0;i<array.length();i++)


{


JSONObject obj=array.getJSONObject(i);


HashMap m=
new HashMap<String,Object>();


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);


}


}
catch (JSONException e) {


//TODO Auto-generatedcatch block


e.printStackTrace();


}