Android Volley错误字符串无法转换为JSONObject

时间:2016-09-19 18:22:30

标签: php android json android-volley

我正在尝试从服务器上的数据库中读取一些数据,但我有这个错误:

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

我尝试尝试服务器上的代码是否正在使用Advanced Rest客户端和Postman,并且它运行良好,这就是结果:

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

但是当我在Android应用程序上尝试它时,我得到了之前的错误响应 这是我的代码:

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);
            }
        });
    }
}

这是我的PHP代码:

<?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);
}
?>

3 个答案:

答案 0 :(得分:2)

从您的服务器返回的响应无效json它已启动连接词

这是来自服务器的json响应

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

删除已连接并重试

答案 1 :(得分:0)

  @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 :(得分:0)

在您的json中,您的对象名称为“stude”,但在您的代码中您使用的是“student”

您的代码:

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

必须改为:

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