封装http远程请求web服务的client端方法

web_backend_develope
chenlw 9 years ago
parent 3f5818a414
commit cc7749b749

@ -1,20 +0,0 @@
package com.platform.http.gfs;
public class BaseForm1 {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -2,6 +2,6 @@ package com.platform.http.gfs;
public class HttpClientConstant { public class HttpClientConstant {
public static String URL_IP_PORT = "http://localhost"; public static String URL_IP_PORT = "http://192.168.0.110:8088/jfinal/";
} }

@ -1,14 +1,9 @@
package com.platform.http.gfs; package com.platform.http.gfs;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.sound.midi.SysexMessage;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
@ -21,19 +16,19 @@ import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import com.platform.entities.FolderNode;
import com.platform.utils.Bean2MapUtils; import com.platform.utils.Bean2MapUtils;
public class HttpUtils { public class HttpUtils {
public Object sendPost(String subUrl, Object data) { public String sendPost(String subUrl, Object data) {
Map<String, Object> resultMap = null;
String resultStr = null; String resultStr = null;
HttpClient client = new DefaultHttpClient(); HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(HttpClientConstant.URL_IP_PORT + subUrl); HttpPost post = new HttpPost(HttpClientConstant.URL_IP_PORT + subUrl);
try { try {
// 传参 // 传参
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); List<NameValuePair> parameters = new ArrayList<NameValuePair>();
if (null != data) {
// 转 map // 转 map
Map<String, Object> map = Bean2MapUtils.convertBean(data); Map<String, Object> map = Bean2MapUtils.convertBean(data);
// 转json // 转json
@ -47,6 +42,8 @@ public class HttpUtils {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
"UTF-8"); "UTF-8");
post.setEntity(entity); post.setEntity(entity);
}
// 发送 // 发送
HttpResponse respone = client.execute(post); HttpResponse respone = client.execute(post);
HttpEntity result = respone.getEntity(); HttpEntity result = respone.getEntity();
@ -60,29 +57,29 @@ public class HttpUtils {
return resultStr; return resultStr;
} }
public static void main(String[] args) { // public static void main(String[] args) {
Date d = new Date(); // Date d = new Date();
HttpUtils ht = new HttpUtils(); // HttpUtils ht = new HttpUtils();
BaseForm1 bas = new BaseForm1(); // BaseForm1 bas = new BaseForm1();
bas.setId("12"); // bas.setId("12");
bas.setName("n12"); // bas.setName("n12");
Object rest = ht.sendPost("/hello/gain", bas); // String rest = ht.sendPost("gfs/getAllvolume", null);
if (null != rest) { // if (null != rest) {
System.out.println(rest); // System.out.println(rest);
JSONObject jsondata = JSONObject.fromObject(rest); // JSONArray jsondata = JSONArray.fromObject(rest);
FolderNode fo = (FolderNode) JSONObject.toBean((JSONObject) jsondata, FolderNode.class); // ArrayList<VolumeEntity> fo = (ArrayList<VolumeEntity>) JSONArray.toCollection(jsondata, VolumeEntity.class);
System.err.println(fo); // System.err.println(fo);
try { // try {
System.err.println(Bean2MapUtils.convertBean(fo)); // System.err.println(Bean2MapUtils.convertBean(fo));
} catch (IllegalAccessException | InvocationTargetException // } catch (IllegalAccessException | InvocationTargetException
| IntrospectionException e) { // | IntrospectionException e) {
// TODO Auto-generated catch block // // TODO Auto-generated catch block
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
Date r = new Date(); // Date r = new Date();
System.err.println(r.getTime() - d.getTime()); // System.err.println(r.getTime() - d.getTime());
} // }
} }

Loading…
Cancel
Save