onResponse(String response)try {JSONObject jsonObject = new JSONObject(response); java.lang.String无法转换为JSONObject

时间:2019-09-06 14:14:11

标签: java android json phpmyadmin localhost

我找不到答案。尝试了许多包括此在内的网站。我想知道问题出在哪里。

错误是:org.json.JSONException:类型为java.lang.String的值连接无法转换为JSONObject

这是我的Andorid代码:-

    StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    try {

                        JSONObject jsonObject=new JSONObject(response);

                        String success=jsonObject.getString("success");

                        JSONArray jsonArray=jsonObject.getJSONArray("login");

                        if (success.equals("1")){
                            for (int i=0;i<jsonArray.length();i++) {
                                JSONObject object=jsonArray.getJSONObject(i);

                                String uname=object.getString("username").trim();
                                String email=object.getString("email");

                                Toast.makeText(MainActivity.this, "Login Successful!!! \n Your Name: "+uname+"\nYour Email: "+email, Toast.LENGTH_SHORT).show();
                                loading.setVisibility(View.GONE);
                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this," Error!!"+e.toString(),Toast.LENGTH_LONG).show();
                        loading.setVisibility(View.GONE);
                        signin_btn.setVisibility(View.VISIBLE);
                    }

                }
            },

Logcat

  

2019-09-06 19:40:37.264 7944-7944 / com.example.project W / System.err:   org.json.JSONException:类型为java.lang.String的值连接   无法转换为JSONObject 2019-09-06 19:40:37.264   7944-7944 / com.example.project W / System.err:at   org.json.JSON.typeMismatch(JSON.java:111)2019-09-06 19:40:37.264   7944-7944 / com.example.project W / System.err:at   org.json.JSONObject。(JSONObject.java:160)2019-09-06   19:40:37.264 7944-7944 / com.example.project W / System.err:at   org.json.JSONObject。(JSONObject.java:173)

3 个答案:

答案 0 :(得分:1)

作为一个例外,response字符串不能转换为JSONObject

  

public JSONObject(java.lang.String source) throws JSONException

     

抛出:JSONException-如果源字符串或键重复存在语法错误。

来自:http://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-

确保您的response字符串(从POST请求到URL_LOGIN获得)确实是有效的JSON字符串。编写包含系统托管URL_LOGIN的端到端测试,或与诸如Postman之类的客户端进行手动测试,以服务URL_LOGIN的系统按给定应用程序的请求工作。

答案 1 :(得分:0)

您的Retrofit回调响应侦听器将是Retrofit的Response类型。 使您的代码像这样,它将正常工作。

StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN,
        new Response.Listener<Response>() {

            @Override
            public void onResponse(Response response) {

                try {

                    JSONObject jsonObject=new JSONObject(response.body());

                    String success=jsonObject.getString("success");

                    JSONArray jsonArray=jsonObject.getJSONArray("login");

                    if (success.equals("1")){
                        for (int i=0;i<jsonArray.length();i++) {
                            JSONObject object=jsonArray.getJSONObject(i);

                            String uname=object.getString("username").trim();
                            String email=object.getString("email");

                            Toast.makeText(MainActivity.this, "Login Successful!!! \n Your Name: "+uname+"\nYour Email: "+email, Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.this," Error!!"+e.toString(),Toast.LENGTH_LONG).show();
                    loading.setVisibility(View.GONE);
                    signin_btn.setVisibility(View.VISIBLE);
                }

            }
        },

L

答案 2 :(得分:0)

当我使用这些语句时,我的代码没有显示问题
JSONObject js =新的JSONObject(response); 最终String res = js.getString(“ result”);

但是当我不正确使用它时:

尝试{

var options = {
    type: 'line',
    data: chardata,
    options: {
        scales: {
            xAxes: [{
                display: false,
                ticks: {
                    display: false
                }
            }]
        }
    }
};