/**
* POST获取网络数据
* @param httpUrl
* @param params
* @return
*/
public String httpPost(String httpUrl, RequestParams params) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
OutputStream out = null;
HttpURLConnection connection = null;
byte[] buff;
try {
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(TIME_OUT);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
out = connection.getOutputStream();
buff = params.toString().getBytes("UTF8");
out.write(buff);
out.flush();
out.close();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
}
reader.close();
int code = connection.getResponseCode();
if (code == 200)
result = sbf.toString();
else
return "{\"success\":false,\"status\":0,\"code\":\"0\",\"msg\":\"服务器连接失败!\",\"url\":null,\"tag\":null}";
} catch (Exception e) {
return "{\"success\":false,\"status\":0,\"code\":\"0\",\"msg\":\"" + e.getMessage()+ "\",\"url\":null,\"tag\":null}";
} finally {
connection.disconnect();
try {
if (out != null) {
out.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
}
}
return result;
}