我正在尝试检索来自不同国家/地区的学生回答的正确答案总数。
我正在使用JSON
链接到我的php然后链接到我的数据库。但我似乎无法阅读单词之间的空格。例如,数据库中的国家/地区名称为"Czech Republic"
。但是当我回音时,它会显示"Czech"
。
php代码中的$country
来自android studio,我发送了一个url链接Config.LEADERSHIPBOARD_URL_ME + country;
以连接到数据库。
这是我的PHP代码:
<?php
$country = $_GET['country'];
require "conn.php";
$sql_mcq = "SELECT * FROM mcqparticipator WHERE country = '".$country."'";
$r_mcq = mysqli_query($conn,$sql_mcq);
$result = array();
$mcqCount = 0;
if($r_mcq)
{
while($row = mysqli_fetch_array($r_mcq))
{
if(($row['correctAns'] == "1"))
{
$mcqCount++;
}
}
array_push($result,array(
"McqCount"=>$mcqCount,
"Country"=>$country));
echo json_encode(array("result"=>$result));
}
else
{
echo "No connection";
}
$conn->close();
?>
这是我android studio
的代码:
// Description: This method will generate a url and send a request
// This method will call showJSON() method to start sending the response
private void getData(){
loading = ProgressDialog.show(getActivity(), "Please wait...","Fetching", false, false);
url = Config.LEADERSHIPBOARD_URL_ME + country;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
Toast.makeText(getActivity(), "Country in studio: " + country, Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Country in database: " + dcountry, Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Mcq count: " + McqCount, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(getActivity(),"No existing question", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue= Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
// Description: This method will generate a response using JSON via php.
// Retreive and compare the participants answer with the correct answer
public void showJSON(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
McqCount = collegeData.getString(Config.MCQ_COUNT);
dcountry = collegeData.getString("Country");
} catch (JSONException e) {
e.printStackTrace();
}
}
感谢有人可以帮助我,谢谢!