3

ive got a php file that connect to database and fetches data, now i want this data to be sent to my java code and be stored as an array, for connecting to php and retriving results i am using AsyncHttpClient now in AsyncHttpClient their is a func onSucess that takes a string value as its parameter, soo anything coming from php is stored as string. i want it to be array.. please suggest me a way, either to get an array instead of string following is my code.

 public void func3(View view)throws Exception
{
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams rp = new RequestParams();
    rp.put("pLat", "select * from iwmp_state");
    client.post("http://10.0.2.2/conc2.php", rp, newAsyncHttpResponseHandler() {
        public final void onSuccess(Array response) {
            // handle your response here
            //tx.setText(response.toString());

        }

        @Override
        public void onFailure(Throwable e, String response) {
            // something went wrong
            tx.setText(response.toString());
        }               
    });
}

and ive got a php file which echos an array $row

<?php
 // attempt a connection
 $dbh = pg_connect("host=10.22.35.11 dbname=iwmp_dev2 user=postgres "); 
 if (!$dbh) {
     die("Error in connection: " . pg_last_error());
 }       

 // execute query
 $sql = $_POST['pLat'];
 $result = pg_query($dbh, $sql);
 if (!$result) {
     die("Error in SQL query: " . pg_last_error());
 }       
$array = array();
 // iterate over result set
 // print each row
 while ($row = pg_fetch_array($result)) {
    $i++;
echo $row[0];
 }       

 // free memory
 pg_free_result($result);       

 // close connection
 pg_close($dbh);
 ?>  

what php echos is array and what onSuccess takes as parameter is string. what to do!

Yogesh Kumar
  • 682
  • 1
  • 10
  • 29

3 Answers3

5

Here, i am just showing simple demo.You have to update as per your requirement.

PHP Side

Creating simple PHP Array , For more detail click here

<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
  {
  echo $cars[$x];
  echo "<br>";
  }
 echo json_encode($cars); 
 exit;
?>

Android Side

Now How to read PHP array inside in android ?

String String_Response = ""; // this is your web response

Create a ArrayList.

    ArrayList<String> User_List = new ArrayList<String>();


                  try
              {
            JSONArray jArray = new JSONArray(String_Response);
            for (int i = 0; i < jArray.length(); i++)
            {
                       JSONObject json_data = jArray.getJSONObject(i);                                                        
               User_List.add(json_data.getString("your_json_obj"));
            }

               } 
                  catch (Exception e)
              {
            Log.e(TAG, "" + e);
               }

Also check out below link in that you will get more idea how to send and receive data from android to php.

Android + PHP Communication Example

Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
  • sir thanks for this example. it worked for me with your exam also.. now ive got json too.. thnx.. before this i did the same using IMPLODE AND SPLIT().. but thanx for this one. – Yogesh Kumar Apr 26 '13 at 11:29
  • any time.i will always ready to help.please accept the answer if its good for you – Chintan Khetiya Apr 26 '13 at 11:31
  • please tell me how to accept the ans and how to vote up an ans. also in the above code wat does "your_json_obj" imples, is it the string_response.. the one in which the json object is being stored when cuming from php..? – Yogesh Kumar Apr 26 '13 at 12:21
  • `your_json_obj` is your `JSON_TAG` which is assign to read keyword.you will find make simple web service. so you will get idea.For accept the answer you just have to click on right arrow , so that will change as green and clcik ` ^ ` for upvote – Chintan Khetiya Apr 26 '13 at 12:26
  • Yet,you haven't accept the answer,please do so good for other fresher can – Chintan Khetiya Apr 27 '13 at 06:53
  • thnk you for dis info sir, n to accept your ans i need repo of 15+, i only have repo of 14 .. for nw. :P – Yogesh Kumar Apr 28 '13 at 06:23
  • @chintan.. please see one of my ques. http://stackoverflow.com/questions/16353561/java-net-socketexceptionoperation-time-out-when-running-app-on-real-device – Yogesh Kumar May 03 '13 at 09:22
1

It is hard for me to understand what you wish to achieve, but perhaps I can help a little with: Implode your php array into a string (http://php.net/manual/en/function.implode.php) then at least you can see what you are returning and perhaps re-process it client side?

Al.
  • 330
  • 1
  • 13
  • got it, what we can do is to use a **IMPLODE** func in our php, and add (,) after every array value and then let it be passed as string. after getting it in java code use. *split(",")* func of String and store them in an array. working fine for me. no json needed :P – Yogesh Kumar Apr 26 '13 at 11:27
1

got it, what we can do is to use a IMPLODE func in our php, and add (,) after every array value and then let it be passed as string. after getting it in java code use. split(",") func of String and store them in an array.

working fine for me. no json needed :P

Yogesh Kumar
  • 682
  • 1
  • 10
  • 29