0

Hi i am new in android php client server. At present, i am doing the response from php sql server sending to Android client multiple result from sql row. Previously, i sending a simple string and receive android like below:

$result_data = array( 
'ResultArray' => 'success',
); 

 #Output the JSON data 
 echo json_encode($result_data); 

Then in android:

// Create a JSON object from the request response
JSONObject jsonObject = new JSONObject(result);

//Retrieve the data from the JSON object
String resultLoging = jsonObject.getString("ResultArray");

Now i want to receive from database having 3 columns: id, phone, name. How would i do that? Thank for your helping

3
  • Will you paste your response.Then we will provide you exact solution Commented Nov 28, 2014 at 5:17
  • In the similar way as it was answered in bazillion similar questions here Commented Nov 28, 2014 at 5:23
  • the code above just example with my simple string encode. I want arrays with multiple result Commented Nov 28, 2014 at 5:29

4 Answers 4

0

use the following format in php

$result = mysql_query("SELECT *FROM tablename") or die(mysql_error());

// check for empty result

if (mysql_num_rows($result) > 0) 

{

    // looping through all results

    // products node

    $response["details"] = array();

    while ($row = mysql_fetch_array($result))

 {

        // temp user array

        $product = array();

        $product["id"] = $row["id"];

        $product["name"] = $row["name"];

array_push($response["details"], $product);

    }

    // success

    $response["success"] = 1;

    // echoing JSON response

    echo json_encode($response);

In android

get success value

int success = json.getInt(TAG_SUCCESS);

get the datas using following format

JSONArray spi = json.getJSONArray("details"); 

Use a for loop to get the object values in the array

for (int i = 0; i < spi.length(); i++) 
{

                        JSONObject c = spi.getJSONObject(i);

 id = c.getString("id");

}
Sign up to request clarification or add additional context in comments.

1 Comment

id = c.getString("id"); this id is array or string?
0

Use JSONArray for multiple json result :

JSONArray jsonArray =  jsonObject.getJSONArray("ResultArray");

Iterate JSONArray and get value from JSONObject :

for (int i=0;i<jsonArray.length();i++){
     JSONObject json = jsonArray.getJSONObject(i);
     String id = json.getString("id");
     String phone = json.getString("phone");
     String name = json.getString("name");
} 

Comments

0

just found this, would be the best for my question https://stackoverflow.com/a/3563464/1345454

$results = array();
while($row = mysql_fetch_array($sql))
{
   $results[] = array(
      'title' => base64_decode($row['title']),
      'price' => $row['price'],
      'seller_user' => $row['user']
   );
}
$json = json_encode($results);

Comments

0

try this

$result_data = array( 
'ResultArray' => 'success',
); 

echo json_encode(array('result'=>$result_data));

in android

     JSONParser jParser = new JSONParser();
     JSONObject json = jParser.getJSONFromUrl("url of php file");
     JsonArray arry = json.getJSONArray("result");
     JSONObject c = arry .getJSONObject(0);
     String resultarr= c.getString("ResultArray");





 public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }            

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
      //  System.out.println(json);

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {





        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

   }
  }

1 Comment

And from which library is jsonparser? Also if it is a parser why it has method called getJSONFromUrl?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.