简体   繁体   English

从数据库检索评分并在Android上设置RatingBar

[英]Retrieving Rating from Database and Setting of RatingBar on Android

I've been trying to tackle this problem for quite some time now. 我已经尝试解决这个问题已有一段时间了。 I'm actually trying to get the user's rating on a particular place from the database, and then pass it back to android. 我实际上是在尝试从数据库中获取特定位置的用户评分,然后将其传递回android。 On android, according to the retrieve information from the database, then set the rating appropriately on the RatingBar. 在android上,根据从数据库中检索到的信息,然后在RatingBar上适当地设置等级。 Below are my segments of code: 以下是我的代码段:

ArrayList<NameValuePair> post_Parameters = new ArrayList<NameValuePair>();
    post_Parameters.add(new BasicNameValuePair("pID", placeID.toString()));
    post_Parameters.add(new BasicNameValuePair("username", FootprintSession.getUsername().toString()));

    String response2 = null;

    try{

        response2 = (CustomHttpClient.executeHttpPost("http://test.com/getUserRating.php", post_Parameters)).toString();

        if(response.isEmpty())
        {
              //trying to print out what is the response
            Toast.makeText(getBaseContext(), "response"+response2, Toast.LENGTH_LONG).show();
            userRatingBar.setRating(defaultRating);
        }
        else
        {
            Toast.makeText(getBaseContext(), "Found : "+response2, Toast.LENGTH_LONG).show();
            userRating = (float)(Double.parseDouble(response2));
            userRatingBar.setRating(userRating);
        }

    }catch(Exception e){
        Log.e("Error in Rating",""+e);
    }

this is the part on my PHP: 这是我的PHP的一部分:

<?php

$un = $_POST['username'];
$pID = $_POST['pID'];

$mysql_host = "localhost";
$mysql_database = "testDB";
$mysql_user = "testAdmin";
$mysql_password = "**********";

//connect to the database
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);

mysql_select_db($mysql_database, $conn);

$query = "SELECT rating FROM fpRating WHERE username = '$un' AND placeID = '$pID' ";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

if(mysql_num_rows($result) == 0) {
    echo null;
}else
{
    $row = mysql_fetch_assoc($result)
    echo $row["rating"];
}

?> ?>

** My rating on my database is a double type. **我对数据库的评级是双重类型。

My logcat prints: java.lang.NumberFormatException ** 我的logcat打印:java.lang.NumberFormatException **

You can't directly convert response to float value like this . 您不能像这样直接将响应转换为浮点值。

This link will be helpfull for you android http response 该链接将是有益的为你的android HTTP响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM