我已经尝试解决这个问题很长时间了。实际上,我正试图从数据库中获取用户对某个特定地方的评分,然后将其传递回android。在android上,根据数据库中的检索信息,然后在RatingBar上适当设置评级。以下是我的代码段:
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);
}
这是我的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"];
}
?>
**我在数据库中的评分是双重类型。
我的logcat打印:java.lang.NumberFormatException**
不能像这样直接将响应转换为浮点值。
此链接将对您的android http响应