0

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;
}
11
  • for (int i = 0; i < jsonArray.length(); i++) { May 17, 2014 at 9:14
  • @user3241507 jsonArray is null . Before using jsonArray.length() print jsonArray.
    – Hariharan
    May 17, 2014 at 9:14
  • jsonArray is null and you are calling length() on null May 17, 2014 at 9:14
  • 1
    OK, localhost on an emulator refers to the emulated device's own loopback (localhost) address and not the one of the PC the emulator is running on. Change the url to String URL = "http://10.0.2.2/natter/index.php";. The IP address 10.0.2.2 on an emulator is routed through to the PC's loopback address.
    – Squonk
    May 17, 2014 at 9:57
  • 1
    @user3241507 : That warrants a separate question. Now you have the network sorted, create a new question showing your JSON being returned from your PHP code.
    – Squonk
    May 17, 2014 at 10:55

2 Answers 2

0

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.

-1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.