3

I am trying to read some data from the database on the server but i have this error:

09-19 21:14:55.294 8376-8376/com.example.user.firebasenot I/System.out: Try2com.android.volley.ParseError: org.json.JSONException: Value connected of type java.lang.String cannot be converted to JSONObject

I tried to try if the code on the server is working using Advanced Rest client and Postman also and it worked very well and this was the result:

{
  "students": [
    {
      "id": "1",
      "firstname": "saleh",
      "lastname": "refai",
      "age": "333"
    },
    {
      "id": "2",
      "firstname": "ali",
      "lastname": "hariri",
      "age": "22"
    }
  ]
}

but when I tried it on the android app I got the previous error response here is my code:

public class Insertion extends AppCompatActivity {

    String token1;
    EditText firstname,lastname,age;
    Button register,show;
    TextView result;
    RequestQueue requestQueue;
    String inserturl="http://saleh923.freeoda.com/insertstudent.php";
    String showurl="http://saleh923.freeoda.com/showstudent.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nsertion);
        Bundle b=getIntent().getExtras();
//        token1=b.getString("token");

        firstname=(EditText)findViewById(R.id.editText);
        lastname=(EditText)findViewById(R.id.editText2);
        age=(EditText)findViewById(R.id.editText3);
        register=(Button) findViewById(R.id.button2);
        show=(Button) findViewById(R.id.button3);
        result=(TextView) findViewById(R.id.textView2);

        requestQueue= Volley.newRequestQueue(getApplicationContext());
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, showurl, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            System.out.println("Try1"+response);
                            JSONArray students=response.getJSONArray("students");
                            for(int i=0;i<students.length();i++)
                            {
                                JSONObject student=students.getJSONObject(i);
                                String firstname=student.getString("firstname");
                                String lastname=student.getString("lastname");
                                String age=student.getString("age");
                                result.append(firstname+" "+lastname+" "+ age+ " \n");

                            }
                            result.append("====\n");
                        } catch (JSONException e) {
                            System.out.println("errrrror");
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("Try2"+error.toString());

                    }
                }
                );
                requestQueue.add(jsonObjectRequest);
            }
        });
    }
}

and here is my PHP code:

<?php
if($_SERVER["REQUEST_METHOD"]=="POST"  )
{
       include "init.php";
       showStudent();
}

function showStudent()
{
    global $con;
    $query="SELECT * FROM  student; ";
    $result=mysqli_query($con,$query);
    $num_of_rows=mysqli_num_rows($result);
    $temp_arr=array();
    if($num_of_rows>0)
    {
        while($row=mysqli_fetch_assoc($result))
        {
            $temp_arr[]=$row;


        }
    }

    header('Content-Type:application/json');
    echo json_encode(array("students"=>$temp_arr));
    mysqli_close($con);
}
?>
4
  • showurl does not return the data Commented Sep 19, 2016 at 19:05
  • I tried it on Postman and Advanced REST client and returned the student array as shown @fahad Commented Sep 19, 2016 at 19:09
  • remove the conneted word in start of json response, json returned from server is not valid json Commented Sep 19, 2016 at 19:36
  • make the post call with this utility check in start there is connected word of your json hurl.it Commented Sep 19, 2016 at 19:38

3 Answers 3

3

The response returned from your server is not valid json it has starting connected word

this is json response from server

connected{"students":[{"id":"1","firstname":"saleh","lastname":"refai","age":"333"},{"id":"2","firstname":"ali","lastname":"hariri","age":"22"}]}

remove connected and try again

Sign up to request clarification or add additional context in comments.

Comments

1
  @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("students");
                for (int x = 0; x < jsonArray.length(); x++) {
                    JSONObject JO = jsonArray.getJSONObject(x);
                    String firstname = JO.getString("firstname");
              }

            } cSONException e) {
                e.printStackTrace();
            }

2 Comments

this will not work because the Response.Listener<JSONObject> needs JSONObject as parameter in the onResponse method
Yes, my fault. AmirhosseinAbd93 answer will fix the problem.
1

in your json your object's name is "stude" but in your code you used "student"

your code :

JSONArray students=response.getJSONArray("students");

have to change to :

JSONArray students=response.getJSONArray("stude");

1 Comment

no, this is my fault i tried to change the name in PHP code to stude and tried it on Advanced REST client to be sure if it changes the header of the json response. This is not the problem solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.