问题
I am trying to get the username from a database using PHP on android. I have it set up on my localhost. the output on localhost is: [{"id":"1","username":"Josh","password":"pass"}]
The PHP is:
<?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);
The java is:
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);
}
and the 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)
I have not dealt with JSON before, I used this tutorial to get this code:
youtube tutorial
Any ideas?
** UPDATE ** Apparently it is giving null in the jsonArray:
here is the code
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;
}
回答1:
Thanks to Squonk, I did not know that localhost
was pointing to localhost
on the emulator / device. So I changed the URL link to String URL = "http://10.0.2.2/natter/index.php";
and it worked.
回答2:
I am not sure but i idea that your code should be like this.
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);
}
Please try this.
来源:https://stackoverflow.com/questions/23709592/null-pointer-exception-on-json-getstring-jsonobject