0

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)

Riccardo
  • 135
  • 3
  • 11
  • 2
    Where's the code you've tried? If we can't see what's generating errors or some code, we don't know where to start to help. – Juan Bonnett Sep 23 '15 at 14:00
  • Like this (java file) http://stackoverflow.com/questions/32403892/android-data-fetch-from-database-not-working-php-json-mysql – Riccardo Sep 23 '15 at 14:31
  • try this website https://developer.android.com/reference/java/net/HttpURLConnection.html there is an updated an simple way, if it doesn't work just tell me and i'll give you a working code i have in one of my apps – marcoFSN Sep 23 '15 at 14:50
  • Thanks Marco, I had already seen the link. I'm trying with this and I do not work. http://developer.android.com/training/basics/network-ops/connecting.html – Riccardo Sep 23 '15 at 15:12
  • @marcoFSN You can post the code that works? – Riccardo Sep 23 '15 at 16:33

1 Answers1

0

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

marcoFSN
  • 125
  • 9