title: Grabbing JSON content with Java
body: <p>Here is a sample of my json_encode in PHP:</p>
  
  <pre><code>print(json_encode($row));
  </code></pre>
  
  <p>leads to {"AverageRating":"4.3"} which is good.</p>
  
  <p>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.</p>
  
  <pre><code> public class Rate extends ListActivity {
  
     JSONArray jArray;
     String result = null;
     InputStream is = null;
     StringBuilder sb = null;
     String Item, Ratings, Review, starAvg;
     RatingBar ratingsBar;
     ArrayList&lt;NameValuePair&gt; param;
  
    public void onCreate(Bundle savedInstanceState) {
  
        starAvg = "0"; // Sets to 0 in case there are no ratings yet.
        new starRatingTask().execute();
        ratingsBar = (RatingBar) findViewById(R.id.theRatingBar);
  
  
    class starRatingTask extends AsyncTask&lt;String, String, Void&gt; {
  
      InputStream is = null;
      String result = "";
  
  
      @Override
      protected Void doInBackground(String... params) {
          String url_select = "http://www.---.com/---/average_stars.php";
  
          ArrayList&lt;NameValuePair&gt; param = new ArrayList&lt;NameValuePair&gt;();
          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) {
  
          String starAvgTwo = null;
          try {
              jArray = new JSONArray(result);
              JSONObject json_data = null;
              for (int i = 0; i &lt; jArray.length(); i++) {
                  json_data = jArray.getJSONObject(i);
                  starAvg = json_data.getString("AverageRating");
  
                     starAvgTwo = starAvg;
  
              }
          } catch (JSONException e1) {
              Toast.makeText(getBaseContext(), "No Star Ratings!",
                      Toast.LENGTH_LONG).show();
          } catch (ParseException e1) {
              e1.printStackTrace();
          }
  
  
                  Toast.makeText(getBaseContext(), starAvgTwo,
                          Toast.LENGTH_LONG).show();
  
          ratingsBar.setRating(Float.valueOf(starAvg));
  
  
  
      }
  }
  </code></pre>
  
  <p>That second toast produces a blank (I assume a "" - empty string?).  If I change the toast variable back to starAvg, then it toasts "0".</p>
  
  <p>How can I retrieve the value of 4.3.</p>