title: JSON: Value of type java.lang.String cannot be converted to JSONObject
body: <p>I'm trying to program an app to send a String to a service. A friend of mine has a service to receive the data.</p>
  
  <p>Logcat shows this error: "org.json.JSONException: Value FIRST of type java.lang.String cannot be converted to JSONObject"</p>
  
  <p>Here is my code:</p>
  
  <blockquote>
    <p>Main Activity</p>
  </blockquote>
  
  <pre><code>public class MainActivity extends Activity {
  
  private String URL = "String with my friend's url";
  private Button btnAddValue;
  
  String num = "1";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      RadioGroup answer = (RadioGroup) findViewById(R.id.answer);
  
      answer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  
          public void onCheckedChanged(RadioGroup group, int checkedId) {
              // TODO Auto-generated method stub
  
              switch (checkedId) {
                  case R.id.answerA:
                      num = "1";
                      break;
                  case R.id.answerB:
                      num = "2";
                      break;
                  case R.id.answerC:
                      num = "3";
                      break;
  
              }
  
          }
      });
  
      btnAddValue = (Button) findViewById(R.id.submit);
  
      btnAddValue.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
              new AddNewValue().execute(num);
          }
      });
   }
  
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  private class AddNewValue extends AsyncTask&lt;String, Void, Void&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
  
      }
  
      @Override
      protected Void doInBackground(String... arg) {
          // TODO Auto-generated method stub
          String number = arg[0];
  
          // Preparing post params
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          params.add(new BasicNameValuePair("number", number));
  
          ServiceHandler serviceClient = new ServiceHandler();
  
          String json = serviceClient.makeServiceCall(URL,
                  ServiceHandler.POST, params);
  
          Log.d("Create Request: ", "&gt; " + json);
  
          if (json != null) {
              try {
                  JSONObject jsonObj = new JSONObject(json);
                  boolean error = jsonObj.getBoolean("error");
                  // checking for error node in json
                  if (!error) {
                      // new category created successfully
                      Log.e("Value added successfully ",
                              "&gt; " + jsonObj.getString("message"));
                  } else {
                      Log.e("Add Error: ",
                              "&gt; " + jsonObj.getString("message"));
                  }
  
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
          } else {
              Log.e("JSON Data", "JSON data error!");
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(Void result) {
          super.onPostExecute(result);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Service Handler</p>
  </blockquote>
  
  <pre><code>public class ServiceHandler {
  static InputStream is = null;
  static String response = null;
  public final static int GET = 1;
  public final static int POST = 2;
  
  public ServiceHandler() {
  
  }
  
  public String makeServiceCall(String url, int method) {
      return this.makeServiceCall(url, method, null);
  }
  
  
  public String makeServiceCall(String url, int method,
                                List&lt;NameValuePair&gt; params) {
      try {
  
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpEntity httpEntity = null;
          HttpResponse httpResponse = null;
  
  
          if (method == POST) {
              HttpPost httpPost = new HttpPost(url);
  
              if (params != null) {
                  httpPost.setEntity(new UrlEncodedFormEntity(params));
              }
  
              httpResponse = httpClient.execute(httpPost);
  
          } else if (method == GET) {
  
              if (params != null) {
                  String paramString = URLEncodedUtils
                          .format(params, "utf-8");
                  url += "?" + paramString;
              }
              HttpGet httpGet = new HttpGet(url);
  
              httpResponse = httpClient.execute(httpGet);
  
          }
          httpEntity = httpResponse.getEntity();
          is = httpEntity.getContent();
  
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "UTF-8"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          response = sb.toString();
      } catch (Exception e) {
          Log.e("Buffer Error", "Error: " + e.toString());
      }
  
      return response;
  }
  }
  </code></pre>
  
  <p>I read questions to other people with the same problem. The solution seemed to be to add a "{" at the beginning of the json String and a "}" at the end, but it didn't work to me. I tried changing this:</p>
  
  <pre><code>String json = serviceClient.makeServiceCall(URL_NEW_PREDICTION,
                  ServiceHandler.POST, params);
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>String json = "{" + serviceClient.makeServiceCall(URL_NEW_PREDICTION,
                  ServiceHandler.POST, params) + "}";
  </code></pre>
  
  <p>but the I got this error:</p>
  
  <pre><code>"org.json.JSONException: Expected ':' after FIRST at character 9 of {FIRST DATA New record created successfully}"
  </code></pre>