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