//设置需要请求服务器的url
String url = yxpFixture.ystoreWebAppURL+"/profile/index"; List<NameValuePair> headers = new ArrayList<NameValuePair>(); //设置heaser headers.add(new BasicNameValuePair("Accept", "application/json, text/plain, */*")); headers.add(new BasicNameValuePair("Content-Type", "application/json;charset=UTF-8"));
//发起get请求获取返回HttpResponse String responseSrc = HttpRequest.get(client, url, headers);
在设计保存个人资料代码时,我知道这是一个post请求,所有参照了主席的删除购物车的post请求,设置url和post的参数,发起请求。String url = yxpFixture.ystoreWebAppURL+"/cart/delete.json"; List<NameValuePair> entity = new ArrayList<NameValuePair>(); // 设置entity entity.add(new BasicNameValuePair("genMyCartParamJson", "{\"cartIdAndPromoIdMap\":null,\"ordPromoId\":null,\"couponCode\":null,\"cartIdSetOfSelected\":[]}")); entity.add(new BasicNameValuePair("ids", itemId)); // 提交删除请求 String response = HttpRequest.post((DefaultHttpClient) client, url, entity, headers, HTTP.UTF_8);
String url = yxpFixture.ystoreWebAppURL+"/profile/save.json"; List<NameValuePair> headers = new ArrayList<NameValuePair>(); List<NameValuePair> entity = new ArrayList<NameValuePair>(); // 设置entity entity.add(new BasicNameValuePair("realName", "小强")); entity.add(new BasicNameValuePair("mobile", “13575xxxx64”));
entity.add(new BasicNameValuePair("email", “test@126.com”);
...... // 提交保存请求 String response = HttpRequest.post((DefaultHttpClient) client, url, entity, headers, HTTP.UTF_8);
执行后报错,返回了HTTP 415的错误( 不支持的媒体类型),估计是请求设置的参数不对。通过火狐的firebug插件分析请求的数据,哎!还真不一样,删除购物车的post参数是这样子的:......
List<NameValuePair> headers = new ArrayList<NameValuePair>(); List<NameValuePair> entity = new ArrayList<NameValuePair>(); entity.add(new BasicNameValuePair("profile",jo.toString())); entity.add(new BasicNameValuePair("userProperty",jo1.toString())); String retSrc =HttpRequest.post((DefaultHttpClient)client, url, entity, headers, HTTP.UTF_8);
......
......
HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(obj3.toString(), HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpResponse httpResponse = client.execute(httpPost); String retSrc = EntityUtils.toString(httpResponse.getEntity());//使用EntityUtils对返回值的实体进行处理
......
最终完成的不是很完美,都是自己摸索和学习的过程,目前开始研究购买、订单提交、组单等接口,也是慢慢摸索完成吧。