JSON.getString JSONObject上的空指针异常

时间:2014-05-17 09:12:24

标签: android json jsonobject

我想在android上使用PHP从数据库中获取用户名。我把它设置在我的localhost上。 localhost上的输出是:[{"id":"1","username":"Josh","password":"pass"}]

PHP是:

<?php
$con = $con = mysql_connect("localhost", "root", "root");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("natter", $con);

$result = mysql_query("SELECT * FROM users");

while($row = mysql_fetch_assoc($result)) {
    $output[] = $row;
}

print(json_encode($output));

mysql_close($con);

java是:

public void setTextToTextView(JSONArray jsonArray) {
    String s = "";
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject json = null;

        try {
            json = jsonArray.getJSONObject(i);
            s = s + "Username : " + json.getString("username");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } 
    status.setText(s);
}

和LOGCAT:

05-17 05:09:49.616: E/AndroidRuntime(1846): FATAL EXCEPTION: main
05-17 05:09:49.616: E/AndroidRuntime(1846): Process: me.docci.natter, PID: 1846
05-17 05:09:49.616: E/AndroidRuntime(1846): java.lang.NullPointerException
05-17 05:09:49.616: E/AndroidRuntime(1846):     at me.docci.natter.Login.setTextToTextView(Login.java:75)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at me.docci.natter.Login$CheckUser.onPostExecute(Login.java:97)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at me.docci.natter.Login$CheckUser.onPostExecute(Login.java:1)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at android.os.AsyncTask.finish(AsyncTask.java:632)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at android.os.Looper.loop(Looper.java:136)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at java.lang.reflect.Method.invoke(Method.java:515)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-17 05:09:49.616: E/AndroidRuntime(1846):     at dalvik.system.NativeStart.main(Native Method)

之前我还没有处理过JSON,我用这个教程来获取这段代码:

youtube tutorial

有什么想法吗?

**更新** 显然它在jsonArray中给出了null:

这是代码

public JSONArray getUserInfo() {
    String URL = "http://localhost/natter/index.php";

    HttpEntity httpEntity = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();
    } catch (IOException e) {
        e.printStackTrace();
    }

    JSONArray jsonArray = null;

    if(httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);

            Log.e("Entity Response : ", entityResponse);

            jsonArray = new JSONArray(entityResponse);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;
}

2 个答案:

答案 0 :(得分:0)

感谢Squonk,我不知道localhost指向模拟器/设备上的localhost。所以我将URL链接更改为String URL = "http://10.0.2.2/natter/index.php";并且它有效。

答案 1 :(得分:-1)

我不确定,但我认为您的代码应该是这样的。

public void setTextToTextView(JSONArray jsonArray) {
    String s = "";
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject json = new JSONObject();

        try {
            json = jsonArray.getJSONObject(i);
            s = s + "Username : " + json.getString("username");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } 
    status.setText(s);
}

请试一试。