码迷,mamicode.com
首页 > 移动开发 > 详细

android与后台请求的例子

时间:2016-03-27 23:55:18      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:


public static ClientResponse SendClientRequest(List<BasicNameValuePair> params)
{
ClientResponse clientResponse = new ClientResponse();
List<ClientResponseData> clientResponseDataList = new ArrayList<>();
clientResponse.setClientResponseDataList(clientResponseDataList);
String url = Switch.url + "ReturnClientInfo";

HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5000);
HttpPost httpPost = new HttpPost(url);
try{
Log.i("66666666666666666666", "66666666666666666666");
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

HttpResponse httpResponse = httpClient.execute(httpPost);


if(httpResponse.getStatusLine().getStatusCode() == 200)
{
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
for(String s = bufferedReader.readLine();s != null; s = bufferedReader.readLine())
{
builder.append(s);
}
Log.i("builder",builder.toString());

JSONObject jsonObjectBoss = new JSONObject(builder.toString());
String jsonObjectStatus = jsonObjectBoss.getString("status");
String jsonObjectMsg = jsonObjectBoss.getString("msg");

if(jsonObjectStatus.equals("success")) {
JSONArray jsonArray = jsonObjectBoss.getJSONArray("content");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
ClientResponseData clientResponseData = new ClientResponseData();
clientResponseData.setClient_id(jsonObject.getString("client_id"));
clientResponseData.setClient_name(jsonObject.getString("client_name"));
clientResponseData.setClient_phone(jsonObject.getString("client_phone"));
clientResponseData.setClient_picture(jsonObject.getString("client_picture"));
clientResponseData.setClient_address(jsonObject.getString("client_address"));
clientResponseData.setUser_id(jsonObject.getString("user_id"));
clientResponse.getClientResponseDataList().add(clientResponseData);
}
}
clientResponse.setStatus(jsonObjectStatus);
clientResponse.setMsg(jsonObjectMsg);
}
else
{
clientResponse.setStatus("error_msg");
clientResponse.setMsg("返回错误码");
}

}catch (Exception e)
{
e.printStackTrace();
clientResponse.setStatus("request_error");
clientResponse.setMsg("请求错误");
}
return clientResponse;
}
这是ClientResponse类:
public class ClientResponse extends Response
{
List<ClientResponseData> clientResponseDataList;

public List<ClientResponseData> getClientResponseDataList() {
return clientResponseDataList;
}

public void setClientResponseDataList(List<ClientResponseData> clientResponseDataList) {
this.clientResponseDataList = clientResponseDataList;
}
}
 
这是ClientResponseData类:
public class ClientResponseData implements Serializable
{
private String client_id;
private String client_name;
private String client_phone;
private String client_address;
private String client_picture;
private String user_id;

public String getClient_id() {
return client_id;
}

public String getClient_name() {
return client_name;
}

public void setClient_name(String client_name) {
this.client_name = client_name;
}

public String getUser_id() {
return user_id;
}

public void setUser_id(String user_id) {
this.user_id = user_id;
}

public String getClient_picture() {

return client_picture;
}

public void setClient_picture(String client_picture) {
this.client_picture = client_picture;
}

public String getClient_address() {

return client_address;
}

public void setClient_address(String client_address) {
this.client_address = client_address;
}

public String getClient_phone() {

return client_phone;
}

public void setClient_phone(String client_phone) {
this.client_phone = client_phone;
}

public void setClient_id(String client_id) {
this.client_id = client_id;
}
}
这是Switch类:
public class Switch {
public static String url =http://115.29.40.90/GoodsManager/index.php/Home/Index/;
}
 
 
接下来是客户端要给服务端传送的数据:
1、返回仓库的所有物品信息,不用传参数
http://115.29.40.90/GoodsManager/index.php/Home/Index/ReturnStoreInfo
服务端接收后返回给客户端的信息:
         失败后的返回数据:
‘status‘ => ‘error‘,
‘msg‘ => ‘查询错误或者无查询结果‘,
‘content‘ => ‘null‘
         成功后的返回数据:


‘status‘ => ‘success‘,
‘msg‘ => ‘查询成功‘,
‘content‘ => $data //这是返回的商品内容
 
2、给仓库添加物品
http://115.29.40.90/GoodsManager/index.php/Home/Index/IntoStorage
客户端需要传送的数据:
goods_id:物品id,形如:’00001’
goods_num:物品数量,形如:‘2000’
goods_name:物品名称,形如:‘电脑’
goods_vender:生产厂家,形如:‘华硕电脑城’
goods_type:型号,形如:‘Xl520’
goods_norms:物品规格,形如:‘合格’
storageUnitName:生产单位名称,形如:‘广州电脑城’
operator:送货或者提货人的姓名,形如:‘小明’
in_out:出库还是入库:1代表出库,0代表入库,这是int
day:出库或者入库的日,形如:‘1日’
month:出库或者入库的月期,形如:‘5月’
year:出库或者入库的年期,形如:‘2016年’
 
服务端接收后返回给客户端的数据: 
插入成功: 
‘status‘ => ‘success‘,
‘msg‘ => ‘插入成功‘,
‘content‘=> ‘null‘
插入错误:
‘status‘ => ‘error‘,
‘msg‘ => ‘插入错误‘,
‘content‘=> ‘null‘
网络错误等原因:
‘status‘ => ‘error‘,
‘msg‘ => ‘查询错误‘,
‘content‘=> ‘null‘
商品已经存在,不能重复插入:
‘status‘ => ‘exist‘,
‘msg‘ => ‘商品已存在‘,
‘content‘=> ‘null‘
 
 
3、删除物品
http://115.29.40.90/GoodsManager/index.php/Home/Index/DeleteStorage
客户端需要传送的数据:
goods_id:物品id
商品不存在:
‘status‘ => ‘noexist‘,
‘msg‘ => ‘用户不存在‘,
‘content‘=> ‘null‘
由于网络等原因造成的失败:
‘status‘ => ‘error‘,
‘msg‘ => ‘查询错误‘,
‘content‘=> ‘null‘
删除成功:
‘status‘ => ‘success‘,
‘msg‘ => ‘删除成功‘,
‘content‘=> ‘null‘
 
 
4、按日期返回入库或者出库物品信息
http://115.29.40.90/GoodsManager/index.php/Home/Index/ReadByDay
客户端需要传送的数据:
day:日期
 
返回成功:
‘status‘ => ‘success‘,
‘msg‘ => ‘返回成功‘,
‘content‘=> $end, //这是返回的商品内容
‘num‘ => $data //这是返回的商品数量
 
仓库没有记录:
‘status‘ => ‘none‘,
‘msg‘ => ‘返回空值‘,
‘content‘=> ‘null‘,
‘num‘ => $data
 
请求错误(由于网络原因等等):
‘status‘ => ‘error‘,
‘msg‘ => ‘请求错误‘,
‘content‘=> ‘null‘,
‘num‘ => ‘0‘
 
5、按月期返回入库或者出库的物品信息
http://115.29.40.90/GoodsManager/index.php/Home/Index/ReadByMonth
客户端需要传送的数据:
month:月期
6、按年期返回入库或者出库的物品信息
http://115.29.40.90/GoodsManager/index.php/Home/Index/ReadByYear
客户端需要传送的数据:
year:年期
 
 
5和6的服务端返回值跟4的一样;
 
 
 
android调用的格式是:
List<BasicNameValuePair>  pairs = null;
ClientResponse clientResponse = null;
pairs = new ArrayList<>();
pairs.add(new BasicNameValuePair("user_id", “1881555100”));
clientResponse = FuckyApi.SendClientRequest(pairs);
这样就返回了clientResponse的实例。

android与后台请求的例子

标签:

原文地址:http://www.cnblogs.com/xulibing/p/5327232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!