0

I have completely updated this question:

Here is a sample of my json_encode in PHP:

print(json_encode($row));

leads to {"AverageRating":"4.3"} which is good.

But in Java, I can not seem to grab this 4.3 value. Here it is (for an Android project) I have edited non-relevant data.

 public class Rate extends ListActivity {

   JSONArray jArray;
   String result = null;
   InputStream is = null;
   StringBuilder sb = null;
   String Item, Ratings, Review, starAvg;
   RatingBar ratingsBar;
   ArrayList<NameValuePair> param;

  public void onCreate(Bundle savedInstanceState) {

      starAvg = "0";
      new starRatingTask().execute();
      ratingsBar = (RatingBar) findViewById(R.id.theRatingBar);


  class starRatingTask extends AsyncTask<String, String, Void> {

    InputStream is = null;
    String result = "";


    @Override
    protected Void doInBackground(String... params) {
        String url_select = "http://www.---.com/---/average_stars.php";

        ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("item", Item));


        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);


        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // read content
            is = httpEntity.getContent();

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {

            Log.e("log_tag", "Error converting result " + e.toString());
        }

        return null;

    }

    protected void onPostExecute(Void v) {


        try {
            jArray = new JSONArray(result);
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                starAvg = json_data.getString("AverageRating");

            }
        } catch (JSONException e1) {
            Toast.makeText(getBaseContext(), "No Star Ratings!",
                    Toast.LENGTH_LONG).show();
        } catch (ParseException e1) {
            e1.printStackTrace();
        }


                Toast.makeText(getBaseContext(), starAvg,
                        Toast.LENGTH_LONG).show();

        ratingsBar.setRating(Float.valueOf(starAvg));



    }
}

That second toast rating produces a "0" as set in the very beginning.

1 Answer 1

1

Why not pass the 'whole' row back in JSON?

print(json_encode($row));

JSON is designed to represent objects, if you're just passing back a number wouldn't it be better to just pass that as a string of a number? However I understand wanting to use JSON so it's generic, which is why you should just pass the whole row back. It'll then be an array (with one element) that contains the average value.

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

6 Comments

Thanks! then what do I call to pick up the value in the Java here here?? starAvg = json_data.getString(----???---); Usuall the column name is entered but wouldnt that no apply here?
Well you could always make it 'have' a column name in your SQL. Something like: "SELECT AVG(rating) AS AverageRating FROM ratings WHERE item_id = '{$item_id}'" where AverageRating is the name.
Hey thanks, finally had an opportunity to try this and its just not working. I don't know if its the PHP or Java end...
Update: The PHP end now says this {"AverageRating":"4.3"} so that end seems ok. But the Java end seems to be sputtering.
I'm sorry I'm not too familiar with the Java side of it. What library are you using? I can try tinkering. Also, have you tried setting a breakpoint at "jArray = new JSONArray(result);" and stepping through to see what fails for you?
|

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.