JSONException String cannot be converted to JSONObject

  • Replies:2
  • OpenNot stickiedUnanswered
  • Forum posts: 6

Aug 11, 2015 8:19:11 AM via Website

I was reading some similar questions but could not find a solution to my Exception.

08-10 16:37:44.457: E/pass 1(2607): connection success
08-10 16:37:44.461: E/pass 2(2607): connection success
08-10 16:37:44.465: E/Fail 3(2607): org.json.JSONException: Value <meta of type java.lang.String cannot be converted to JSONObject

I get product_name and product_id from a listview and open on product_details activity. What I want now is to show a Toast message after clicking on a TextView to check product availability. After some tries I still get the exception above.

I´m not using an EditText to get data. I use a textView where I click to do the stuff. This could be the problem? Am I getting an empty string?


public class ProductDetails extends Activity {

TextView name;
String p_id, pname,  yes, no;
InputStream inputStream = null;
String result = null;
String line = null;
int code;

private static final String TAG_PID = "product_id";
private static final String TAG_NAME = "product_name";

protected void onCreate(Bundle savedInstanceState) {

name = (TextView) findViewById(R.id.name);

Intent i = getIntent();
p_id = i.getStringExtra(TAG_PID);
pname = i.getStringExtra(TAG_NAME);


final TextView et_yes = (TextView) findViewById(R.id.yes);
final TextView et_no = (TextView) findViewById(R.id.not_a);

    et_yes.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            yes = et_yes.getText().toString();
            new insertYes().execute();


public class insertYes extends AsyncTask<String, Boolean, Boolean>{

    protected Boolean doInBackground(String... params) {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("p_id", p_id));
        nameValuePairs.add(new BasicNameValuePair("yes", yes));

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("my website link / insert_counter.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            inputStream = entity.getContent();
            Log.e("pass 1", "connection success");
        }catch(Exception e){
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP", Toast.LENGTH_LONG).show();

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null) {                
            result = stringBuilder.toString();
            Log.e("pass 2", "connection success");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());

        try {
            JSONObject json_date = new JSONObject(result);
            code = (json_date.getInt("code"));
            if (code==1) {
                Toast.makeText(getBaseContext(), "Insert success",
            } else {
                Toast.makeText(getBaseContext(), "Sorry, try again",
        } catch (Exception e) {
            Log.e("Fail 3", e.toString());
        return null;
  • Forum posts: 6

Sep 4, 2015 2:50:28 AM via Website

Ashish Tripathi

Send your JSON response

This is old. Have already solved it. Thanks