Request---Post Android
public static String postRequest(String url, String info) throws Exception {
// 创建HttpPost对象
HttpPost post = new HttpPost(url);
ILog.v(TAG, "传递给服务器的信息: " + info);
StringEntity entity = new StringEntity(info, HTTP.UTF_8);
post.setEntity(entity);
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取服务器响应字符串
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}else{
ILog.v("服务器响应代码", Integer.valueOf(httpResponse.getStatusLine().getStatusCode()).toString());
return null;
}
}
Request---Get
public static String getRequest(String url) throws Exception {
// 创建HttpGet对象
HttpGet get = new HttpGet(url);
// 发送GET请求
HttpResponse httpResponse = httpClient.execute(get);
// 如果服务器成功地返回响应
ILog.v(TAG, "getRequest...");
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取服务器响应字符串
String result = EntityUtils.toString(httpResponse.getEntity());
ILog.v(TAG, "getRequest200");
return result;
}else{
ILog.v("服务器响应代码", Integer.valueOf(httpResponse.getStatusLine().getStatusCode()).toString());
return null;
}
}
Service
String bool = null;
// InputStream is = ServletActionContext.getRequest().getInputStream();
//
// byte[] bytes = new byte[1024 * 1024];
//
// int nRead = 1;
// int nTotalRead = 0;
// while (nRead > 0) {
// nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
// if (nRead > 0)
// nTotalRead = nTotalRead + nRead;
// }
// String str = new String(bytes, 0, nTotalRead, "utf-8");
// System.out.println("Str:" + str);
BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) ServletActionContext.getRequest().getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(br);
}
System.out.println(sb);
// if (str.equals("123")) {
// bool = "true";
// }
// System.out.println(str);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(bool);// 返回的字符串数据
out.close();
return null;