title: Passing data between android and server using JSON parsing
body: <p>I'm trying to send and receive data between <strong>android</strong> app and <strong>server</strong> using <strong>JSON</strong> but unfortunately i am unable to do it i searched a lot but it didn't help. My code is as follows:   </p>
  
  <p>In android </p>
  
  <pre><code>try {
          JSONObject jsonObject = new JSONObject();
          jsonObject.put("fromDate", from);
          jsonObject.put("toDate", to);
          jsonObject.put("reason", reas);
          new SendData().execute(jsonObject);
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  </code></pre>
  
  <p>and then my <strong>asynctask</strong>'s doInBackground method</p>
  
  <pre><code>try {
                  HttpParams httpParams = new BasicHttpParams();
                  HttpConnectionParams.setConnectionTimeout(httpParams,
                          CmsInter.TIMEOUT);
                  HttpConnectionParams.setSoTimeout(httpParams, CmsInter.TIMEOUT);
                  HttpClient client = new DefaultHttpClient(httpParams);
                  HttpPost httpPost = new HttpPost(getResources().getString(
                          R.string.URL));
                  StringEntity se = new StringEntity(object.toString());
                  se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                          "application/json"));
                  httpPost.setEntity(se);
                  HttpResponse response = client.execute(httpPost);
                  message = Sync.convertResponseToString(response);
              } catch (Exception e) {
                  e.printStackTrace();
                  message = e.toString();
              }
  </code></pre>
  
  <p>and then on server side within the <code>doPost()</code> method of servlet code is as follows :</p>
  
  <pre><code>protected void doPost(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
              StringBuffer sb = new StringBuffer();
              String line = null;
              try {
                  BufferedReader reader = request.getReader();
                  while ((line = reader.readLine()) != null)
                      sb.append(line);
              } catch (Exception e) { /* report an error */
                  e.printStackTrace();
              }
              String str = sb.toString();
              try {
                  org.json.JSONObject json = new org.json.JSONObject(str);
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  }
  </code></pre>
  
  <p>and I think this is not the right way to read <code>JSONObject</code> on the server side because when I run this I get <code>org.json.JSONException: A JSONObject text must begin with '{' at character 1</code> exception , Can anyone please help me out in this. Any help will be appreciated. Thanks. </p>