2017-04-01 68 views
1

我一直在关注tutorial(老实说,我是复制粘贴,因此我不知道为什么它不工作作为我没有改变任何东西),我有这个字符串响应,我需要转换成Json对象。试图创建一个JSONObject结束与无法评估org.json.JSONObject.toString()

响应=

CONFIG.PHP \ r \ N { “错误”:假​​, “用户”:{ “名称”: “测试”, “姓”: “测试”, “地址” : “测试”, “电子邮件”: “测试”}}

右后tryJSONObject jObj = new JSONObject(response);我的程序跳转到一条消息,异常:

方法抛出“显示java.lang.NullPointerException '例外。不能 评估org.json.JSONObject.toString()

是它的response什么结构?

此处,我使用它:

@Override 
      public void onResponse(String response) { 
       Log.d(TAG, "Login Response: " + response.toString()); 
       hideDialog(); 

       try { 
        JSONObject jObj = new JSONObject(response); 
        boolean error = jObj.getBoolean("error"); 

        // Check for error node in json 
        if (!error) { 
         // user successfully logged in 
         // Create login session 
         session.setLogin(true); 

         // Now store the user in SQLite 

         JSONObject user = jObj.getJSONObject("user"); 
         String name = user.getString("name"); 
         String surname = user.getString("surname"); 
         String address = user.getString("address"); 
         String email = user.getString("email"); 

         // Inserting row in users table 
         db.addUser(name, surname, address, email); 

         // Launch main activity 
         Intent intent = new Intent(LoginScreen.this, 
           MainMenu.class); 
         startActivity(intent); 
         finish(); 
        } else { 
         // Error in login. Get the error message 
         String errorMsg = jObj.getString("error_msg"); 
         Toast.makeText(getApplicationContext(), 
           errorMsg, Toast.LENGTH_LONG).show(); 
        } 
       } catch (JSONException e) { 
        // JSON error 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 

      } 
+0

把它变成一个JSON对象之前,从响应删除'config.php文件\ r \ N'前缀。 –

+0

谢谢,那是做的。奇怪他们为什么不在教程中提到这一点,因为代码是相同的 – giedriusx3

回答

0

取出config.php文件\ r \ n的把它变成一个JSON对象之前的回应前缀。

0

您的response不是有效的json字符串。

请参阅RFC 7159。你

也可以验证您的JSON在线json formatter