我的Android代码:
HttpClient client=new DefaultHttpClient();
HttpPost request=null;
for(StudentInfo stdInfo : stdInfoList) {
JSONObject reqObject=dao.createjsonreq(stdInfo);
StringEntity requestSet = new StringEntity(reqObject.toString());
//appUrl = http://192.168.1.1/StudentInfo/std?
request=new HttpPost(appUrl+"Request="+URLEncoder.encode(reqObject.toString()));
//now URl is = http://192.168.1.1/StudentInfo/std?Request={"RequestData":{"StringImge":"/9A/.."}}
requestSet.setContentType("application/json;charset=UTF-8");
requestSet.setContentEncoding(new BasicHeader
(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setEntity(requestSet);
HttpResponse response=client.execute(request);
.....
}
此处为createjsonrequest
创建方法
JSONObject sentRequestJson = new JSONObject();
JSONObject sentRequestJson = new JSONObject();
Bitmap image=BitmapFactory.decodeFile(stdInfo.getPicFile());
ByteArrayOutputStream baosimg = new ByteArrayOutputStream();
if (image != null) {
image.compress(Bitmap.CompressFormat.PNG,60,baos);
byte[] byteimg=baosimg.toByteArray();
String strImage = Base64.encodeToString(byteimg, Base64.DEFAULT);
requestJsonObj.put("StringImage",strImage );
}
sentRequestJson.put("RequestData":requestJsonObj);
return sentRequestJson;
我的Servlet代码:
log.debug("I got request");
req.setCharacterEncoding("UTF-8");
Enumeration<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()) {
String jsonObject = parameterNames.nextElement();
log.debug(jsonObject + "names");
}
当我从Android应用程序发送POST请求到java服务器时,它不执行java服务器中的任何代码(我给出了调试)(当我在android中调试时看到base64图像字符串),
如果我使用android代码requestJsonObj.put("StringImage","");
,则请求调用服务器并调试执行。
请帮忙。 base64字符串图像的内容类型的任何更改。