Value Connected of type java.lang.String cannot be converted to JSONObject

Ibrahim Mahmoud Emara

I have a PHP file that gets all employees from a database when I run it get to mee all employee in JSON format.

This code in Android Studio that gets all employee from database by the php file

When I run the app I don't get the data in Android Studio.

It returns this error:

Value Connected of type java.lang.String cannot be converted to JSONObject

in the image

enter image description here

This is the Java code in Android Studio:

package com.example.ibrahim.samplecrud;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class ViewAllEmployee extends AppCompatActivity implements ListView.OnItemClickListener {

    private ListView listView;

    private String JSON_STRING;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_employee);
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        getJSON();
    }


    private void showEmployee(){
        JSONObject jsonObject = null;
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String id = jo.getString(Config.TAG_ID);
                String name = jo.getString(Config.TAG_NAME);

                HashMap<String,String> employees = new HashMap<>();
                employees.put(Config.TAG_ID,id);
                employees.put(Config.TAG_NAME,name);
                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                ViewAllEmployee.this, list, R.layout.list_item,
                new String[]{Config.TAG_ID,Config.TAG_NAME},
                new int[]{R.id.id, R.id.name});

        listView.setAdapter(adapter);
    }

    private void getJSON(){
        class GetJSON extends AsyncTask<Void,Void,String>{

            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(ViewAllEmployee.this,"Fetching Data","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                JSON_STRING = s;
                showEmployee();
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest(Config.URL_GET_ALL);
                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, ViewEmployee.class);
        HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
        String empId = map.get(Config.TAG_ID).toString();
        intent.putExtra(Config.EMP_ID,empId);
        startActivity(intent);
    }
} 
Phan Van Linh

Currently, your String that receive from server is

Connection sucessful{"result":[{"id":2,"name":A"},{"id","pop"},...]}

and it is not a valid JSON format

So you should edit the server code for return correct JSON format like

{"result":[{"id":2,"name":A"},{"id","pop"},...]}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JSONException: Value of type java.lang.String cannot be converted to JSONObject

From Dev

Value Database of type java.lang.String cannot be converted to JSONObject

From Dev

Value Database of type java.lang.String cannot be converted to JSONObject

From Dev

java.lang.String cannot be converted to JSONObject. mismatch type

From Dev

JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

From Dev

org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject during the debugging

From Dev

How to solve error: Value UPDATE of type java.lang.String cannot be converted to JSONObject

From Dev

org.json.JSONException: Value < of type java.lang.String cannot be converted to JSONObject

From Dev

Value <br of type java.lang.String cannot be converted to JSONObject on android

From Dev

error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

From Dev

Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject

From Dev

Value Connection of type java.lang.String cannot be converted to JSONObject in getting objects from the database

From Dev

org.json.JSONException: Value Data of type java.lang.String cannot be converted to JSONObject

From Dev

java.lang.String cannot be converted to JSONObject

From Dev

Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

From Dev

ERROR: com.android.volley.ParseError: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

From Dev

org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject in Android Studio with phpserver

From Dev

Value Bundle of type java.lang.String cannot be converted to JSONArray

From Dev

Value Bundle of type java.lang.String cannot be converted to JSONArray

From Dev

JSONException: Value 'cityId':4,cityName:'Ahmedabad 'java.lang.String cannot be converted to JSONObject

From Dev

Get Org.json.JSONException:Value array(2) of type java.lang.String can not be converted to JSONObject

From Dev

JSONArray java.lang.String cannot be converted to JSONObject

From Dev

JSON Exception: java.lang.String cannot be converted to JSONObject

From Dev

Why java.lang.String cannot converted into JSONObject in android

From Dev

json error value of type string cannot be converted to jsonobject

From Dev

json error value of type string cannot be converted to jsonobject

From Dev

Value [string] <br> [string] <br> of type java.lang.String cannot be converted to JSONArray

From Dev

Value of type java.lang.String cannot be converted to JSONArray in Android(repost)

From Dev

Value of type java.lang.String cannot be converted to JSONArray in Android(repost)

Related Related

  1. 1

    JSONException: Value of type java.lang.String cannot be converted to JSONObject

  2. 2

    Value Database of type java.lang.String cannot be converted to JSONObject

  3. 3

    Value Database of type java.lang.String cannot be converted to JSONObject

  4. 4

    java.lang.String cannot be converted to JSONObject. mismatch type

  5. 5

    JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

  6. 6

    org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject during the debugging

  7. 7

    How to solve error: Value UPDATE of type java.lang.String cannot be converted to JSONObject

  8. 8

    org.json.JSONException: Value < of type java.lang.String cannot be converted to JSONObject

  9. 9

    Value <br of type java.lang.String cannot be converted to JSONObject on android

  10. 10

    error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

  11. 11

    Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject

  12. 12

    Value Connection of type java.lang.String cannot be converted to JSONObject in getting objects from the database

  13. 13

    org.json.JSONException: Value Data of type java.lang.String cannot be converted to JSONObject

  14. 14

    java.lang.String cannot be converted to JSONObject

  15. 15

    Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

  16. 16

    ERROR: com.android.volley.ParseError: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

  17. 17

    org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject in Android Studio with phpserver

  18. 18

    Value Bundle of type java.lang.String cannot be converted to JSONArray

  19. 19

    Value Bundle of type java.lang.String cannot be converted to JSONArray

  20. 20

    JSONException: Value 'cityId':4,cityName:'Ahmedabad 'java.lang.String cannot be converted to JSONObject

  21. 21

    Get Org.json.JSONException:Value array(2) of type java.lang.String can not be converted to JSONObject

  22. 22

    JSONArray java.lang.String cannot be converted to JSONObject

  23. 23

    JSON Exception: java.lang.String cannot be converted to JSONObject

  24. 24

    Why java.lang.String cannot converted into JSONObject in android

  25. 25

    json error value of type string cannot be converted to jsonobject

  26. 26

    json error value of type string cannot be converted to jsonobject

  27. 27

    Value [string] <br> [string] <br> of type java.lang.String cannot be converted to JSONArray

  28. 28

    Value of type java.lang.String cannot be converted to JSONArray in Android(repost)

  29. 29

    Value of type java.lang.String cannot be converted to JSONArray in Android(repost)

HotTag

Archive