Search code examples
phpandroidjsonhttpurlconnectionjsonreader

get string from php json encode to android target api23+


I have a php file that generates data that must take to Android.

This is the file output .php

[{"item":"1","title":"Title of one","link":"qwerty1234"},{"item":"2","title":"Title of two","link":"qwerty1234"},{"item":"3","title":"Title of three","link":"qwerty1234"}]

Now there have been changes with the class apache: Link here

Looking around I find several guides on the old method but I wanted to use the new one since my app is the targetSdkVersion 23.
I think this is the new method with JsonReader and HttpURLConnection.

I tried but I can not make it work or understand how to handle it.

So I ask, how do I take the strings from php page that creates? (example: title and link)


Solution

  • please try this.

    it's working for me.

        String url = "someurl.php";
        HttpPost httppost = new HttpPost(url);
    
        // Creating HTTP client
        HttpClient httpClient = new DefaultHttpClient();
    
        try {
            HttpResponse response = httpClient.execute(httpPost);
            String responseString = EntityUtils.toString(response.getEntity());
    
            JSONObject jsonObject = new JSONObject(responseString);
    
            String error = jsonObject.getString("item1");
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    in this case i was using GSON to later parse the response but you can parse it as you wish.

    this is a snippet of code that i can provide, if you need more help or if some dependencies are missing please tell me so i can point you in the right direction.

    have a look at this tutorial for more help

    http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php

    if you need a webservice PHP framework, take a look at this

    http://www.slimframework.com/

    it's very easy to use and ligthweight framework so you can use it as a service to send and recieve info from your server

    hope it helps, feel free to ask if you need any more help

    cheers