你的dopost根本就没有封装json格式。 response.setContentType("application/json"); // Get the printwriter object from response to write the required json object to the output stream PrintWriter out = response.getWriter(); // Assuming your json object is **jsonObject**, perform the following, it will return your json object out.print(jsonObject); out.flush();
要么 定义webservice android 直接请求也很爽。
@RequestMapping("/restproxy")
public class HttpController {
/**
* 默认post方法
* @param systemClient 调用系统编号
* @param url 调用地址 中间用!符号分割 如:index!get.thm
* @param charset 编码格式 默认UTF-8
* @param request
* @param response
* @param args json格式的参数
*/
@RequestMapping(value="/url/{url}")
public void execute(@PathVariable("url") String actionMethod,HttpServletRequest request,HttpServletResponse response,String argsjson) {
String action = actionMethod.split("_")[0];
String method = actionMethod.split("_")[1];
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(StringUtil.isNotBlank(argsjson)){
JSONObject json = JSONObject.fromObject(argsjson);
Iterator<?> it = json.keys();
while (it.hasNext()) {
String key = (String) it.next();
nvps.add(new BasicNameValuePair(key, json.getString(key)));
}
}
String result = null;
try {
String url = "http://localhost:8080/PudongTravelWeb/page/"+action+"_"+method+".action";
result = doPost(url, nvps, "UTF-8",0,0,false,request,response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
}
public static String doPost(String url, List<NameValuePair> nvps, String charset, int connectTimeout,
int readTimeout, boolean isGzip,HttpServletRequest request,HttpServletResponse response) throws ClientProtocolException, IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, connectTimeout);// 连接超时
httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, readTimeout); // 读取超时
if (isGzip) {
httpclient.addRequestInterceptor(new RequestAcceptEncoding());
httpclient.addResponseInterceptor(new ResponseContentEncoding());
}
try { HttpPost httpPost = new HttpPost(url);
if(null==nvps){
nvps=new ArrayList<NameValuePair>();
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, charset.toUpperCase()));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String restr=httpclient.execute(httpPost, responseHandler);
return restr;
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object
out.print(jsonObject);
out.flush();