0

I want to send a Json object from android to a servlet. When the Json object is converted to a String, it is 74409 characters in length. On the server side request.getContentLength(); returns 73728. This causes an error when using gson.fromJson(); These are the relevant code snippets from the Android app, and the servlet.

Android:

String jobpfb = getJsonStringWithGson(pfb);
URL url = new URL(testPath);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type",     "application/json");
httpConn.connect();
OutputStream os = httpConn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);

Log.i(info, String.valueOf(jobpfb.length()));
osw.write(jobpfb.toString());
InputStream is = httpConn.getInputStream();
String x = convertStreamToString(is);

osw.close();
osw.flush();
is.close();

httpConn.disconnect();

Servlet:

System.out.println(request.getContentLength());
response.setContentType("application/json");
Gson gson = new Gson();
InputStream is = request.getInputStream();
String x = convertStreamToString(is);
JsonReader jr = new JsonReader(new StringReader(x));
jr.setLenient(true);
ProfileBean pfb = gson.fromJson(jr, ProfileBean.class);
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
slipperypete
  • 5,358
  • 17
  • 59
  • 99
  • You have to flush your output stream before you close it. Also, I would flush the output stream before attempting to read the response. You cannot expect the server to send you a response before it received the complete request. – Ralf Mar 30 '16 at 14:10
  • http://stackoverflow.com/a/36278171/5930228 please check out this link , you are actually doint gson.fromJson() wrongly – Sumanth Jois Mar 30 '16 at 14:13
  • Ok that worked with moving the inputStream, thanks for the help. – slipperypete Mar 30 '16 at 14:16

0 Answers0