JSONException:没有值

时间:2013-12-09 15:29:13

标签: java android arrays json parsing

在我的代码中出现以下错误,我认为它没有正确读取我的JSON而且我不明白为什么,它在我上周做了一些更改之前工作正常但我忘记了我做了哪些更改。

  

W / System.err(1551):org.json.JSONException:uid没有值

基本上,该程序使用用户名/密码登录,并使用JSON解析将数据从MySQL数据库传递到下一个活动。在按下登录按钮时,它向PHP页面发送HTTP请求,该页面在MySQL中验证并将用户数据的JSON字符串(数组)返回到下一个活动。

此活动显示,但它是空白的,它应该在不同的TextView中显示所有人员信息,但事实并非如此。

这是一个JSON输出示例:

  

{ “用户”:[{ “全名”: “亚当”, “显示名”: “adamator”, “电子邮件”: “123”, “密码”: “123”, “created_at”:“2013-11 -30 11:20:31“,”updated_at“:”2013-11-30 11:34:08“}]}

所以我想要做的是在logcat或弹出消息中显示JSON对象中的内容,以便我可以看到我的JSON解析器是否正常工作。我怎样才能做到这一点?或者如果有人能够在我的代码中看到我遗漏的问题,那就更好了。

这是我的Homepage.java

public class Homepage extends Activity {

    // URL to get JSON Arrays
    public static String url = "http://10.0.2.2/android/SQL.php?username='";
    public static String usernamefromlogin;
    public static TextView errorchecking;

    // JSON Node Names
    private static final String TAG_USER = "users";
    private static final String TAG_ID = "uid";
    private static final String TAG_NAME = "fullname";
    private static final String TAG_DISPLAY = "displayname";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_PW = "password";
    private static final String TAG_CREATED = "created_at";
    private static final String TAG_UPDATED = "updated_at";

    JSONArray user = null;

    @Override
    public void onBackPressed() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reshomepage);

        // Get login name from EditText in login screen and concatenate it to
        // PHP user-name for _GET command in 3 steps.
        // Step 1: Get intent from previous activity
        Intent intent = getIntent();
        getIntent().getExtras();

        // Step 2: convert intent (intent) to string called "usernamefromlogin"
        // //error checking in log cat to see value of "usernamefromlogin"
        usernamefromlogin = intent.getExtras().getString("username2");
        Log.d("log of usernamefromlogin", usernamefromlogin);

        // Step 3: take the string "url" and add string "usernamefromlogin"
        // after it
        String url5 = url.concat(usernamefromlogin);
        String url6 = url5.concat("'");
        // find TextView "errorchecking" and send the string "url6" to it so it
        // can display in log cat
        Log.d("log of URL6 in it's final state", url6);

        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL from the final string "url6"
        JSONObject json = jParser.getJSONFromUrl(url6);
        // Logcat check value for TAG_USER

        try {
            // Getting JSON Array

            user = json.getJSONArray(TAG_USER);
            JSONObject c = user.getJSONObject(0);

            // Storing JSON item in a String Variable
            String uid = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String display = c.getString(TAG_DISPLAY);
            String email = c.getString(TAG_EMAIL);
            String pw = c.getString(TAG_PW);
            String created = c.getString(TAG_CREATED);
            String updated = c.getString(TAG_UPDATED);

            // Importing TextView
            final TextView uid1 = (TextView) findViewById(R.id.tvuid);
            final TextView name1 = (TextView) findViewById(R.id.tvfullname);
            final TextView display1 = (TextView) findViewById(R.id.tvdisplayname);
            final TextView email1 = (TextView) findViewById(R.id.tvemail);
            final TextView pw1 = (TextView) findViewById(R.id.tvpassword);
            final TextView created1 = (TextView) findViewById(R.id.tvcreated_at);
            final TextView updated1 = (TextView) findViewById(R.id.tvupdated_at);

            ImageView imgView;
            imgView = (ImageView) findViewById(R.id.image);
            imgView.setImageResource(R.drawable.icon);

            // Set JSON Data in its respectable TextView
            uid1.setText(uid);
            name1.setText(name);
            display1.setText(display);
            email1.setText(email);
            pw1.setText(pw);
            created1.setText(created);
            updated1.setText(updated);

            // print error if applicable.
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

JSONParser.java

package com.sencide;

import learn2crack.jsonparsing.library.JSONParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class Homepage extends Activity {

    // URL to get JSON Arrays
    public static String url = "http://10.0.2.2/android/SQL.php?username='";
    public static String usernamefromlogin;
    public static TextView errorchecking;

    // JSON Node Names
    private static final String TAG_USER = "users";
    private static final String TAG_ID = "uid";
    private static final String TAG_NAME = "fullname";
    private static final String TAG_DISPLAY = "displayname";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_PW = "password";
    private static final String TAG_CREATED = "created_at";
    private static final String TAG_UPDATED = "updated_at";

    JSONArray user = null;

    @Override
    public void onBackPressed() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reshomepage);

        // Get login name from EditText in login screen and concatenate it to
        // PHP user-name for _GET command in 3 steps.
        // Step 1: Get intent from previous activity
        Intent intent = getIntent();
        getIntent().getExtras();

        // Step 2: convert intent (intent) to string called "usernamefromlogin"
        // //error checking in log cat to see value of "usernamefromlogin"
        usernamefromlogin = intent.getExtras().getString("username2");
        Log.d("log of usernamefromlogin", usernamefromlogin);

        // Step 3: take the string "url" and add string "usernamefromlogin"
        // after it
        String url5 = url.concat(usernamefromlogin);
        String url6 = url5.concat("'");
        // find TextView "errorchecking" and send the string "url6" to it so it
        // can display in log cat
        Log.d("log of URL6 in it's final state", url6);

        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL from the final string "url6"
        JSONObject json = jParser.getJSONFromUrl(url6);
        // Logcat check value for TAG_USER

        try {
            // Getting JSON Array

            user = json.getJSONArray(TAG_USER);
            JSONObject c = user.getJSONObject(0);

            // Storing JSON item in a String Variable
            String uid = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String display = c.getString(TAG_DISPLAY);
            String email = c.getString(TAG_EMAIL);
            String pw = c.getString(TAG_PW);
            String created = c.getString(TAG_CREATED);
            String updated = c.getString(TAG_UPDATED);

            // Importing TextView
            final TextView uid1 = (TextView) findViewById(R.id.tvuid);
            final TextView name1 = (TextView) findViewById(R.id.tvfullname);
            final TextView display1 = (TextView) findViewById(R.id.tvdisplayname);
            final TextView email1 = (TextView) findViewById(R.id.tvemail);
            final TextView pw1 = (TextView) findViewById(R.id.tvpassword);
            final TextView created1 = (TextView) findViewById(R.id.tvcreated_at);
            final TextView updated1 = (TextView) findViewById(R.id.tvupdated_at);

            ImageView imgView;
            imgView = (ImageView) findViewById(R.id.image);
            imgView.setImageResource(R.drawable.icon);

            // Set JSON Data in its respectable TextView
            uid1.setText(uid);
            name1.setText(name);
            display1.setText(display);
            email1.setText(email);
            pw1.setText(pw);
            created1.setText(created);
            updated1.setText(updated);

            // print error if applicable.
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

1 个答案:

答案 0 :(得分:3)

试试这个..

您的回复中的JSONObject中没有uid,您必须与c.has(TAG_ID)核对是否存在TAG名称。

        String uid = "";
        String name = "";
        String display = "";
        String email = "";
        String pw = "";
        String created = "";
        String updated = "";

        if(c.has(TAG_ID))
           uid = c.getString(TAG_ID);

        if(c.has(TAG_NAME))
           name = c.getString(TAG_NAME);

        if(c.has(TAG_DISPLAY))
           display = c.getString(TAG_DISPLAY);

        if(c.has(TAG_EMAIL))
           email = c.getString(TAG_EMAIL);

        if(c.has(TAG_PW))
           pw = c.getString(TAG_PW);

        if(c.has(TAG_CREATED))
           created = c.getString(TAG_CREATED);

        if(c.has(TAG_UPDATED))
           updated = c.getString(TAG_UPDATED);