Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JSON to PHP Server and back

Tags:

json

android

php

Can anybody offer a solution to the above?

For now, all i want to do is send a JSON request to my server (for example: {picture:jpg, color:green}), have the PHP access the database and then return a filename from the server's database (then get android to download the file - not an issue)

Can anybody suggest firstly an Android Framework that will help me with this. - POST JSON to a php file on my server

And secondly a php script that will turn the JSON into php readable format (accessing the database is not an issue either but i cannot get the JSON into an object to then match with the database)

thanks

EDIT

Thanks for the links below but i'm not asking this out of laziness, it's out of annoyance at my seeming inability to send a JSON string and get out the correct answer.

So let me show some code and find out why what i think should happen isn't happening:

GET URL (Using GET so i can show the working)

http://example.com/process/json.php?service=GOOGLE

<?php

// decode JSON string to PHP object
$decoded = json_decode($_GET['json']);

// I'm not sure of which of the below should work but i've tried both.
$myService = $decoded->service; //$service should equal: GOOGLE
$myService = $decoded->{'service'}; //$service should equal: GOOGLE

// but the result is always $myService = null - why?


?>
like image 452
Matt Avatar asked Dec 05 '22 23:12

Matt


1 Answers

OK, i've got the PHP. The below retrieves POST ed data and returns the service

<?php

$data = file_get_contents('php://input');
$json = json_decode($data);
$service = $json->{'service'};

print $service;

?>

and the Android Code:

in onCreate()

path = "http://example.com/process/json.php";

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
                                                                            // Limit
    HttpResponse response;
    JSONObject json = new JSONObject();
    try {
        HttpPost post = new HttpPost(path);
        json.put("service", "GOOGLE");
        Log.i("jason Object", json.toString());
        post.setHeader("json", json.toString());
        StringEntity se = new StringEntity(json.toString());
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        post.setEntity(se);
        response = client.execute(post);
        /* Checking response */
        if (response != null) {
            InputStream in = response.getEntity().getContent(); // Get the
                                                                // data in
                                                                    // the
                                                                    // entity
            String a = convertStreamToString(in);
            Log.i("Read from Server", a);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

and where ever you want

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
like image 94
Matt Avatar answered Dec 07 '22 14:12

Matt