0

My Android Code :

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);
.....
}

Here in createjsonrequest creation method

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;

My Servlet Code :

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");
 }

When I send POST request from android app to java server it doesn't executes any code in java server (I am given debugs) (when I debug in android I see a base64 image string),

If I use android code requestJsonObj.put("StringImage",""); then request calling to server and debugs executing. Any help please. Any change in content type for base64 string image.

VMai
  • 10,156
  • 9
  • 25
  • 34
kks
  • 342
  • 5
  • 25
  • is the image bigger than some 4k?, maybe you should post multipart encoding?. BTW what´s the complete url u´re using? – eduyayo Sep 26 '14 at 11:05
  • thanks for replay, here complete URL **http://192.168.1.1:8080/StudentInfo/std?Request={"RequestData":{"StringImge":"/9A/.."}}** and image size is more then 350KB. – kks Sep 26 '14 at 11:14
  • looks like that is not a POST query! data should be IN the body – eduyayo Sep 26 '14 at 11:15
  • its worked for me when i sent image is null like : URL **192.168.1.1:8080/StudentInfo/std?Request={"RequestData":{"StringImge":""}}** – kks Sep 26 '14 at 11:17
  • That does not mean it´s a POST, if the data is not in the body, it´s still a GET query with a POST method. Create an empty html file with a form and a couple of inputs, change the method from get to post and see how it behaves. And please, read this: http://en.wikipedia.org/wiki/POST_(HTTP) – eduyayo Sep 26 '14 at 11:21
  • PLEASE CAN YOU SHARE SOME SAMPLE CODE, – kks Sep 26 '14 at 14:07
  • f course, maybe your google is not working, who knows!! http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient – eduyayo Sep 26 '14 at 17:35
  • and here´s anothe one with multipart encoding http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pr because it seems your stackoverflow is down, also – eduyayo Sep 26 '14 at 17:37

0 Answers0