-2

I want to ask how to pass data from php to textview in android with json. I already create this php file to retrieve data from my db, but the thing is I don't know how to pass it to android with json. I have read many tutorial, but it doesn't seem to fix my problem since they all using httpclient and httppost which is already deprecated. Can u please help me?

Here is my php file

<? php
mysql_connect("localhost","root","");

mysql_select_db("mydatabase");

$query = "SELECT * FROM tbl_user";
$result = mysql_query($query);


while($row = mysql_fetch_assoc($result))
{
array_push($result,
             array('Name'=>$record[0],
             'Address'=>$record[1],
             'address'=>$record[2]
           ));
         }

    echo json_encode(array("result"=>$result));

?>

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Fimblutterr
  • 89
  • 2
  • 10
  • 2
    I fail to see what your PHP code has to do with your question? As far as the connection on the client side is concerned: in its most basic fashion, it would [look like this](http://developer.android.com/intl/es/training/basics/network-ops/connecting.html#download). Depending on your needs, you may want to leverage one of many networking libraries out there. – MH. Nov 20 '15 at 13:32
  • 1
    You can now use HttpURLConnection instead of HttpClient. Tons of info on SO about it. – mjp66 Nov 20 '15 at 13:33

2 Answers2

1

Well there are many ways on which you can get data from a server through JSON.

Still use HTTP requests - Even though these have depreciated by Android, because of their complexity and lack of speed, you can still use this method to retrieve data from a server. I have tried it and I can assure that it still works on some older apps I myself have developed.

Use Volley - In late 2013, Android introduced Volley, which is the best for you to parse JSON data onto your app. There are several tutorials online which can help you achieve this. Personally this is my best one. Using the Volley API is very similar to the the Http request method, but it is simpler to integrate and faster.

Use REST - Alternatively you can use REST services to fetch data from the server. If you consider famous apps such as Evernote or Wunderlist, these keep the user credentials even after uninstalling the app and re-installing it. This is because the unique ID of the device is kept in the database. A good tutorial on how to implement this can be found here.

Other APIs - A quick search over the internet can provide you with other methods to parse JSON responses from online databases.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
0

The best way is to use the volley library. I find it simply awesome to work with and also easy. It takes care of a lot of work for you. Yes there are always a few limitations to this. You can go through a few links here, here and here for volley tutorial. I found this tutorial exceptionally helpful. Google will give you a lot of results. Please go through this SO post to get a good idea about your requirements. Also @codeByMI gave a really nice answer that you can follow.

Community
  • 1
  • 1
Sajib Acharya
  • 1,666
  • 5
  • 29
  • 54