//用到的方法
package interfaceTest_1; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; /** * Created by Ethan on 2015/5/12. */ public class HTTPUtil { public static String postJson(String uri, String params) { HttpClient httpclient = new DefaultHttpClient(); String backstr = null; try { HttpPost postMethod = new HttpPost(uri); postMethod.setEntity(new StringEntity(params, "utf-8")); // 灏嗗弬鏁板~鍏�OST postMethod.setHeader("Content-Type", "application/json; charset=utf-8"); HttpResponse response = httpclient.execute(postMethod); // 鎵ц�POST鏂规硶 backstr = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return backstr; } public static String dopost(String uri, List<BasicNameValuePair> params) { HttpClient httpclient = new DefaultHttpClient(); String backstr = null; try { HttpPost postMethod = new HttpPost(uri); postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 灏嗗弬鏁板~鍏�OST // Entity涓� postMethod.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); HttpResponse response = httpclient.execute(postMethod); // 鎵ц�POST鏂规硶 backstr = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return backstr; } }
//回包处理
package Vo; public class ResVo { private String data; private Integer result; private String msg; public String getData() { return data; } public void setData(String data) { this.data = data; } public Integer getResult() { return result; } public void setResult(Integer result) { this.result = result; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
//JSON格式上传测试
package junitTest; import java.util.HashMap; import java.util.Map; import org.junit.Test; import com.alibaba.fastjson.JSON; import Vo.ResVo; import interfaceTest_1.HTTPUtil; public class XXX { //........ @Test public void saveUserRatio(){ int groupId=7; int babyId = 5; String website = "http://192.168.1.100/iftest/xxx.htm"; Map<String,Object> m = new HashMap<String,Object>(); Map<String,Object> a = new HashMap<String,Object>(); m.put("groupId",groupId); a.put("babyId",babyId); m.put("a", a); String res = HTTPUtil.postJson(website,JSON.toJSONString(m)); System.out.println(JSON.toJSONString(m)); ResVo resVo = JSON.parseObject(res,ResVo.class); System.out.println(res); org.junit.Assert.assertSame(0, resVo.getResult()); }
//接口需要上传图片的测试
package junitTest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.message.BasicNameValuePair; import org.junit.Test; import com.alibaba.fastjson.JSON; import Vo.ResVo; import interfaceTest_1.HTTPUtil; public class NotesTest { @Test public void notesSave(){ String groupId="7"; String pictureStatus = "0"; String pictures = "1"; String website = "http://192.168.1.100/if.htm"; List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("groupId",groupId)); params.add(new BasicNameValuePair("note.pictureStatus", pictureStatus)); params.add(new BasicNameValuePair("note.pictures", pictures)); System.out.println(JSON.toJSONString(params)); String res = HTTPUtil.dopost(website,params); ResVo resVo = JSON.parseObject(res,ResVo.class); System.out.println(res); org.junit.Assert.assertSame(0, resVo.getResult()); } }