title: Error sending user selected rating to server and refreshing ratingbar with json response using Android Volley?
body: <p>I have a RatingBar where the user can rate a product. When the user selects their rating, the app is supposed to send the rating to the server along with the username and product id. Then the server is supposed to calculate the average overall rating and return it as a response. The RatingBar is then supposed to refresh with the overall rating for that product. Unfortunately, when tested, the rating system doesn't work and the error that is produced doesn't make any sense or hint to an issue. I've tried the logcat in verbose, debug and error mode but still get the same result. I've tried retrieving/sending the "float rating" but that produced the same result. I also included a "toast" message onClick to confirm that the rating is being processed and it is. I have no idea what is going wrong here and would like to know if I have the system running correctly? If not, what can I do to ensure the system works correctly? I converted <a href="http://www.thaicreate.com/mobile/android-rating-vote-and-listview-part-2.html" rel="nofollow">this</a> tutorial to use the Volley library but kept the PHP rating code the same.</p>
  
  <p><strong>This is the logcat response</strong></p>
  
  <pre><code>    04-03 17:48:02.538 20253-20253/com.curtrostudios.testapp E/new_rating: Rating Update Error: null
      04-03 17:48:02.562 20253-20253/com.curtrostudios.testapp D/Volley: [1] Request.finish: 10328 ms: [ ] http://localhost/testapp/updateRating.php 0x1b9800e3 NORMAL 8
  </code></pre>
  
  <p><strong>If I try this within the browser using the POST paramerters, I get a HTTP 500 error</strong></p>
  
  <pre><code>    http://localhost/demoapp/updateRating.php?pid=1&amp;userid=1&amp;ratingpnt=3
  </code></pre>
  
  <p><strong>This is my PHP code</strong></p>
  
  <pre><code>    &lt;?
  
      //load and connect to MySQL database stuff
      require("config.php");
  
      // Check Duplicate
      $strSQL = "SELECT * FROM rating WHERE 1 AND ProductID = '".$_POST["pid"]."' 
      AND UserID = '".$_POST["userid"]."'  ";
      $objQuery = mysql_query($strSQL);
      $obResult = mysql_fetch_array($objQuery);
      if($obResult)
      {
          $arr['StatusID'] = "0"; 
          $arr['Error'] = "Duplicate Vote!";  
          $arr['Rating'] = "0";   
          mysql_close($objConnect);
  
          echo json_encode($arr);
          exit();
      }
  
      /*** Insert rating ***/
      $strSQL = "INSERT INTO rating (ProductID,UserID,RatingPoint) 
          VALUES (
              '".$_POST["pid"]."',
              '".$_POST["userid"]."',
              '".$_POST["ratingpnt"]."'
              ) ";
      $objQuery = mysql_query($strSQL);
  
      /*** Update Rating Average ***/
      $strSQL = " UPDATE products SET
          prod_rating = (SELECT AVG(RatingPoint) FROM rating WHERE ProductID = '".$_POST["pid"]."')
          WHERE pid = '".$_POST["pid"]."' ";
      $objQuery = mysql_query($strSQL);
  
      // Return New Rating Point
      $strSQL = "SELECT * FROM products WHERE 1 AND pid = '".$_POST["pid"]."'  ";
      $objQuery = mysql_query($strSQL);
      $obResult = mysql_fetch_array($objQuery);
      if($obResult)
      {
          $arr['StatusID'] = "1"; 
          $arr['Error'] = ""; 
          $arr['Rating'] = $obResult["Rating"];   
      }
  
      /**
          $arr['StatusID'] // (0=Failed , 1=Complete)
          $arr['Error'] // Error Message
          $arr['Rating'] // New Rating Point
      */
  
      mysql_close($objConnect);
  
      echo json_encode($arr);
  ?&gt;
  </code></pre>
  
  <p><strong>This is my ratingbar</strong></p>
  
  <pre><code>    final String rating = intent.getStringExtra("rating");
  prodRating = (RatingBar)findViewById(R.id.ratingBar);
          prodRating.setNumStars(5);
          prodRating.setMax(5);
          prodRating.setStepSize(0.1f);
          prodRating.setRating(Float.valueOf(rating));
          prodRating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
              @Override
              public void onRatingChanged(RatingBar ratingBar, float newRating, boolean fromUser) {
                      updateRating(pid, uid, ratingBar.getRating());
              }
          });
  </code></pre>
  
  <p><strong>This is the function to updateRating</strong></p>
  
  <pre><code>    public void updateRating(final String pid, final String userid,
                              final Float strNewRating) {
  
          StringRequest strReq = new StringRequest(Request.Method.POST,
                  AppConfig.URL_UPDATE_RATING, new Response.Listener&lt;String&gt;() {
  
              @Override
              public void onResponse(String response) {
                  Log.d(AppConfig.TAG_NR, "Update Rating Response: " + response.toString());
                  try {
                      JSONObject jObj = new JSONObject(response);
                      boolean error = jObj.getBoolean("error");
  
                      // Check for error node in json
                      if (!error) {
                          // rating successfully posted
                          String newRate = jObj.getString("prod_rating");
  
                          prodRating.setRating(Float.valueOf(newRate));
                          Log.d(AppConfig.TAG_NR, "New rating:" + newRate);
  
  
                      } else {
                          // Error updating rating. Get the error message
                          String errorMsg = jObj.getString("error_msg");
                          Toast.makeText(getApplicationContext(),
                                  errorMsg, Toast.LENGTH_LONG).show();
                      }
                  } catch (JSONException e) {
                      // JSON error
                      e.printStackTrace();
                      Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                  }
  
  
              }
          }, new Response.ErrorListener() {
  
              @Override
              public void onErrorResponse(VolleyError error) {
                  Log.e(AppConfig.TAG_NR, "Rating Update Error: " + error.getMessage());
                  Toast.makeText(getApplicationContext(),
                          error.getMessage(), Toast.LENGTH_LONG).show();
                  //hideDialog();
              }
          }) {
  
              @Override
              protected Map&lt;String, String&gt; getParams() {
                  // Posting params to register url
                  Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
                  params.put("pid",pid);
                  params.put("userid", userid);
                  params.put("ratingpnt", Float.valueOf(strNewRating).toString());
  
                  return params;
              }
  
          };
  
          // Adding request to request queue
          VolleyController.getInstance().addToRequestQueue(strReq, AppConfig.TAG_NR);
  
      }
  </code></pre>