1、方法一
(1)、result = HttpRequest.sendGet(url+pds.getString("eId"), null); //向服务器端传递数据获取返回值 url为接口地址
JSONObject newresult = JSONObject.fromObject(result);
int errorCode = (int)newresult.getInt("errorCode");
JSONObject eventInfoData = (JSONObject)newresult.get("eventInfoData");
(2)、
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.connect();
// 获取所有响应头字段
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("----------------------发送GET请求出现异常!-------------------------");
// e.printStackTrace();
}
2、方法二
(1)、NameValuePair[] params = {
new NameValuePair("title",pd.getString("title")),
new NameValuePair("content",pd.getString("content")),
new NameValuePair("address",pd.getString("address")),
new NameValuePair("coordLat",pd.getString("latitude")),
new NameValuePair("coordLong",pd.getString("longitude")),
new NameValuePair("eventSource","5"),
new NameValuePair("fileuuid",fileuuid),
//new NameValuePair("files","http://gkq.ittun.com/"+pd.getString("imgPath"))
};
Properties pro = GetProperties.getKey("wgh.properties"); //获取配置文件wgh.properties中的地址
String host = pro.getProperty("host"); //IP(可不写)
String port = pro.getProperty("port");//端口(可不写)
String url = pro.getProperty("reportUrl"); //接口地址
result = HttpRequest.sendPost(host,port,url, params);
if(result.getString("errorCode").equals("0")){
pd.put("eId", JSONObject.fromObject(result.getString("result")).getString("id"));
pd.put("isSynchro", "1");
}
(2)、
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static JSONObject sendPost( String host,String port,String requestUrl, NameValuePair[] params){
JSONObject jsonObject = null;
String buffer = null;
HttpClient httpClient = new HttpClient();
try {
HttpMethod method = postMethod(requestUrl,params);
httpClient.executeMethod(method);
buffer = method.getResponseBodyAsString();
} catch (IOException e) {
System.out.println("-------------------发送post请求出现异常!-----------------------------------");
//e.printStackTrace();
}
jsonObject = JSONObject.fromObject(buffer);
return jsonObject;
}