1

I am returning a JSON string from PHP:

<?php
$results = array(
    "result"   => "success",
    "username" => "some username",
    "projects" => "some other value"
);

echo json_encode($results);
?>

I found a java example online that works. It uses StringBuilder and outputs the response using Toast. I want to actually parse it as a JSON object so I can reference each key=>value, but not sure how to do it. This is the example I am using:

private void tryLogin(String usernameInput, String passwordInput)
{
    HttpURLConnection connection;
    OutputStreamWriter request = null;
    URL url = null;
    String response = null;
    String parameters = "username=" + usernameInput + "&password=" + passwordInput;

    try
    {
        url = new URL(getString(R.string.loginLocation));
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestMethod("POST");

        request = new OutputStreamWriter(connection.getOutputStream());
        request.write(parameters);
        request.flush();
        request.close();
        String line = "";

        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        response = sb.toString();

        Toast.makeText(this, "Message from server: \n" + response, 0).show();
        isr.close();
        reader.close(); 
    }
    catch(IOException e)
    {
        Log.i("NetworkTest","Network Error: " + e);
    }
}

This is what the code currently returns:

05-04 19:19:54.724: INFO/NetworkTest(1061): {"result":"success","username":"rondog","projects":"1,2"}

Just to be clear, I am pretty sure I know how to parse the string. What I am confused on is getting the response back from the server and pushing that to the JSONObject (or is 'response' the object that I pass?). Any help is appreciated, thanks!

Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • im not sure what your issue is. – Naftali May 04 '11 at 19:23
  • I guess I am confused as to what to pass to the JSONObject. Do I just pass 'response' ? I just started java a couple days ago so bare with me :) – Ronnie May 04 '11 at 19:26
  • if `05-04 19:19:54.724: INFO/NetworkTest(1061): {"result":"success","username":"rondog","projects":"1,2"}` is what you are getting back, then the parser won't work because you have `05-04 19:19:54.724:... in the string. you only what the json string nothing else – Ibu May 04 '11 at 19:26
  • 1
    if I do: JSONObject obj = new JSONObject(response); I get a "JSONObject cannot be resolved to a variable" error – Ronnie May 04 '11 at 19:27
  • @ Ibrahim Diallo thats just from the LogCat. It is actually just the bracketed stuff – Ronnie May 04 '11 at 19:28
  • Why are you not using [jQuery Ajax](http://api.jquery.com/jQuery.ajax/)? – Dutchie432 May 04 '11 at 19:32
  • @Ronnie: Please add the code where you acutally parse the response as well. It might not match what the [JSONObject](http://www.json.org/javadoc/org/json/JSONObject.html) is about, but that's just an assumption of me and I might be wrong. – hakre May 04 '11 at 19:37
  • Well it wont let me answer my own question, but here is how I did it: [http://pastie.org/1865381](http://pastie.org/1865381) @hakre: Is the way I posted in the pastie link the correct way to do? It seems to be working ok – Ronnie May 04 '11 at 19:37
  • What library are you using for JSON handling in Java? – no.good.at.coding May 04 '11 at 19:40
  • It looks like you're using the library from JSON.org. As for the code, yes, that's the way you would do it. You might also want to check out [GSON](https://code.google.com/p/google-gson/), it's a much less painful way of dealing with JSON. – no.good.at.coding May 04 '11 at 19:41
  • @Ronnie: I have no clue. From what I read from the docs you might want to try `get` instead of `getString`. Maybe that works? – hakre May 04 '11 at 19:43
  • @hakre: If I use `get`, I have to end up doing `toString()` at the end. Both methods work. @no.good.at.coding: Yes I am using the JSON.org library. I'll check out GSON, thanks. – Ronnie May 04 '11 at 19:47
  • @Ronnie: If both methods work, then response with a valid json string should make it. The json documentation page has a description which formattings the class is able to parse and what you showed here looks valid as far as I can say. – hakre May 04 '11 at 19:50

1 Answers1

0

(or is 'response' the object that I pass?)

Yes, it is. It expects a string object in it's constructor to parse it.

hakre
  • 193,403
  • 52
  • 435
  • 836