/**
* 获取 接口调用结果
* @param String postData
* @param String url
* @return
* @throws ProtocolException
* @throws UnsupportedEncodingException
* @throws IOException
// 发送POST请求
URL postUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type"," application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", "" + postData.length());
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData);
out.flush();
out.close();
// 获取响应状态
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
Map<String,String> map= new HashMap<String, String>();
map.put("code", "error");
map.put("msg", "connect error");
return map;
}
// 获取响应内容体
String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = in.readLine()) != null) {
result += line + "\n";
}
in.close();
// 获取返回的状态码
return JsonUtil.fromJson(result, Map.class);
}
* 获取 接口调用结果
* @param String postData
* @param String url
* @return
* @throws ProtocolException
* @throws UnsupportedEncodingException
* @throws IOException
*/
// 发送POST请求
URL postUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type"," application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", "" + postData.length());
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData);
out.flush();
out.close();
// 获取响应状态
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
Map<String,String> map= new HashMap<String, String>();
map.put("code", "error");
map.put("msg", "connect error");
return map;
}
// 获取响应内容体
String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = in.readLine()) != null) {
result += line + "\n";
}
in.close();
// 获取返回的状态码
return JsonUtil.fromJson(result, Map.class);
}