【问题标题】:onResponse(String response) try { JSONObject jsonObject=new JSONObject(response);java.lang.String cannot be converted to JSONObjectonResponse(String response) try { JSONObject jsonObject=new JSONObject(response);java.lang.String 无法转换为 JSONObject
【发布时间】:2020-01-09 10:25:02
【问题描述】:

我找不到答案。试过很多网站,包括这个。我想知道问题出在哪里。

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

这是我的安卓代码:-

    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:在 org.json.JSON.typeMismatch(JSON.java:111) 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err:在 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)

【问题讨论】:

  • 显示您的 json 响应。
  • 你能显示响应字符串吗?

标签: java android json phpmyadmin localhost


【解决方案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 字符串(从对 URL_LOGIN 的 POST 请求中获得)确实是有效的 JSON 字符串。编写包含托管 URL_LOGIN 的系统的端到端测试,或者使用 Postman 等客户端手动测试服务 URL_LOGIN 的系统是否根据您的应用程序的请求按预期工作。

【讨论】:

    【解决方案2】:

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

    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

    【讨论】:

    • StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN, new Response.Listener() { @Override public void onResponse(Response response) { try { JSONObject jsonObject=new JSONObject( response.body());
    • 能否分享您的回复
    • 哪个响应? php代码还是移动应用响应?
    • 移动应用响应
    【解决方案3】:

    当我使用这些语句时,我的代码没有显示问题

    JSONObject js=new JSONObject(response);
    final String res = js.getString("result");
    

    但是当我不使用它时它可以正常工作:

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                final String response = EntityUtils.toString(httpResponse.getEntity());
                JSONObject js=new JSONObject(response);
                final String res =  js.getString("result");
                JSONObject js=new JSONObject(response);
                final String res =  js.getString("result");
    
    
    
    
    
    
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Problem", Toast.LENGTH_SHORT).show();
                    }
                });
            }
    
    
            catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多