title: Malformed JSON while making a POST call
body: <p>When I send a JSON object to the server via a POST request, the server returns an error message.</p>
  
  <p>Code:</p>
  
  <pre><code>public String sendStuff(String reqUrl,String arg1, String arg2){
      String response;
      try{
          URL url = new URL(reqUrl);
          HttpURLConnection conn = (HttpURLConnection)url.openConnection();
          conn.setDoOutput(true);
          conn.setDoInput(true);
          conn.setRequestMethod("POST");
          JSONObject jsonObject = new JSONObject();
          jsonObject.accumulate("argument1",arg1);
          jsonObject.accumulate("argument2",arg2);
          String json = jsonObject.toString();
          DataOutputStream out = new DataOutputStream(conn.getOutputStream());
          out.writeBytes(URLEncoder.encode(json,"UTF-8"));
          out.flush();
          out.close();
  
          int HttpResult = conn.getResponseCode();
          if(HttpResult == HttpURLConnection.HTTP_OK){
              response = convertStreamToString(conn.getInputStream());
              return response;
          }
      }catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }
  
  private String convertStreamToString(InputStream is) {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb = new StringBuilder();
  
      String line;
      try {
          while ((line = reader.readLine()) != null) {
              sb.append(line).append('\n');
          }
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              is.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      return sb.toString();
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>{"ERROR":"A JSONObject text must begin with '{' at 1 [character 2 line 1]"}</p>
  </blockquote>
  
  <p>According to the RESTful service, this error message would only be returned if the JSON is malformed. I've tested the service manually by a Chrome extension and it works fine with that.</p>
  
  <p>I think there shouldn't be an error since I'm directly converting the JSON to a string by a method from the <code>org.json</code> package.</p>
  
  <p>I searched for a solution, but couldn't find one.</p>