我遇到了问题...... 我的PHP代码使用GET方法工作,当我获得“ID”& GET“Rate”我的SQLDatabase已更新,我的输出是树参数EncodedJson(OldRate,YourRate,TotalRate)。 编码一个PHP输出它似乎是正确的,但在AndroidStudio我的GET请求方法有问题。 ERROR内容如下:
01-20 15:04:25.883 3623-3623/info.androidhive.customlistviewvolley E/ERROR: Error occurred
com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of
at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:121)
Caused by: org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at org.json.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:121)
这是我的PHP代码:
<?php
require_once 'include\config.php';
// connect to database
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
mysqli_set_charset($con, 'utf8');
//GETS
if(isset ($_GET['ID']) && isset($_GET['Rating'])){
$ID = $_GET['ID'];
$MyRate = $_GET['Rating']; }
else{return;}
//CountVote
$sql1="UPDATE `civilarticle` SET `Counter`= `Counter`+1 WHERE `ID`=$ID";
$sql2= "SELECT `Counter` FROM `civilarticle` WHERE `ID`=$ID";
if($con->query($sql1) === TRUE ){
//echo "Item updated successfully"."<br>";
} else{
echo "Error : " . $con->error ;
}
if ($result = $con->query($sql2)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$COUNTVote=$row["Counter"];
//echo "<br>".$row["Counter"]."<br>"."*****"."<br>";
}}
//PASTVote
if(`Counter`<1){
$sql3="UPDATE `civilarticle` SET `PastRate`= (`PastRate`+$MyRate)/2 WHERE `ID`=$ID";}
else{$sql3="UPDATE `civilarticle` SET `PastRate`= 0 WHERE `ID`=$ID";}
$sql4= "SELECT `PastRate` FROM `civilarticle` WHERE `ID`=$ID";
if($con->query($sql3) === TRUE ){
//echo "Item updated successfully"."<br>";
} else{
echo "Error : " . $con->error ;
}
if ($result = $con->query($sql4)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$PASTVote=$row["PastRate"];
//echo "<br>".$row["PastRate"]."<br>"."*****"."<br>";
}}
//YOURVote
$sql5 = "UPDATE `civilarticle` SET `YourRate`='$MyRate' WHERE `ID`=$ID";
$sql6="SELECT `YourRate` FROM `civilarticle` WHERE `ID`=$ID";
if($con->query($sql5) === TRUE){
//echo "Item updated successfully"."<br>";
} else{
echo "Error : " . $con->error ;
}
if ($result = $con->query($sql6)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$YOURVote=$row["YourRate"];
//echo "<br>".$YOURVote['YourRate']."<br>"."*****"."<br>";
}}
//AVVote
if(`Counter`<1 ){
$sql7="UPDATE `civilarticle` SET `Rating`=($MyRate + (`Counter`-1)*`PastRate`)/`Counter` WHERE ID=$ID";}
else{$sql7="UPDATE `civilarticle` SET `Rating`= $MyRate WHERE `ID`=$ID";}
$sql8= "SELECT `Rating` FROM `civilarticle` WHERE `ID`=$ID";
if($con->query($sql7) === TRUE){
//echo "Item inserted successfully"."<br>";
} else{
echo "Error : " . $con->error ;
}
if ($result = $con->query($sql8)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$AVVote=$row["Rating"];
//echo "<br>".$row["Rating"]."<br>"."*****"."<br>";
}}
在PHP代码的下面,我编写了它们:
$arr=array("OldRate"=>$PASTVote,"YourRate"=>$YOURVote,"TotalRate"=>$AVVote,"CountVote"=>$COUNTVote);
json_encode($arr);
和我的JavaClass:
int ID=1; //put extra give me this value
public float Rate;
Button btn_submit,btn_detail;
RatingBar ratingbar;
String url= "mylink/android_login_api/rating.php";
TextView pastrate,yourrate,totalrate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
txtTiltle = (TextView) findViewById(R.id.txtTitle);
txtMain = (TextView) findViewById(R.id.txtMain);
imgView = (ImageView) findViewById(R.id.imgView);
///
btn_detail= (Button) findViewById(R.id.button2);
pastrate= (TextView) findViewById(R.id.t1);
yourrate=(TextView) findViewById(R.id.t2);
totalrate=(TextView) findViewById(R.id.t3);
////
Bundle extras = getIntent().getExtras();
if (extras!=null){
if(extras.containsKey("Title")){
title = extras.getString("Title");
txtTiltle.setText(title);
}
}
///my code for rating
btn_submit= (Button) findViewById(R.id.button);
ratingbar = (RatingBar) findViewById(R.id.ratingBar);
btn_submit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Rate = ratingbar.getRating();
Toast.makeText(DetailActivity.this,""+ratingbar.getRating(),Toast.LENGTH_SHORT).show();
if(Rate!=-1){
btn_submit.setEnabled(false);
}
}
});
btn_detail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DetailActivity.this,url,Toast.LENGTH_SHORT).show();
CustomRequest CustomRequest = new CustomRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
pastrate.setText(response.getString("OldRate"));
yourrate.setText(response.getString("YourRate"));
totalrate.setText( response.getString("TotalRate"));
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(DetailActivity.this,response+"",Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("ERROR", "Error occurred ", volleyError);
Toast.makeText(DetailActivity.this,"Failed",Toast.LENGTH_SHORT).show();
volleyError.printStackTrace();
}
})
{
@Override
protected Map<String,String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("ID", String.valueOf(ID));
params.put("Rating",String.valueOf(Rate));
return params;
}
};
AppController.getInstance().addToRequestQueue(CustomRequest);
}
});
}
}
请帮帮我......谢谢你......
答案 0 :(得分:0)
这是因为在PHP中你发送的是Array,在android客户端你正在读取对象。如果你想在android客户端的Json对象中收到,那么请看下面的例子
<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
输出看起来像
{"name":"John","age":30,"city":"New York"}
以适当的JSON对象格式输出上述内容。
如果您希望以Json数组的形式接收,那么您应该以Json数组的形式发送并在客户端读取为JSON数组。
我希望这会有所帮助
请参阅下面的内容,了解如何在PHP端构建Json。