我有一个应用程序,可以从中插入和检索gps数据
mysql
使用
php api
。我在发送或获取数据方面没有问题,我正在努力解决的问题是,当我尝试将参数发送到
api
(例如user\u id)我得到
Error Data
这意味着我在我的要求下做错了什么。我正试图发送
user_id
,然后将数据检索到
listview
与该id关联。我看到了一些使用
JsonArrayRequest
这可能是我应该使用的,但我在尝试使用Post方法实现它时不断出错
params
。
public void test( final String id_user){
RequestQueue requestQueue =
Volley.newRequestQueue(MeusTrilhos.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST,
urlget, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
int count = response.length();
try {
for(int i = 0; i < count; i++){
JSONObject jo = new JSONObject(response);
Trilhos tri = new Trilhos();
tri.setId(jo.getInt("id"));
tri.setTitulo(jo.getString("titulo"));
tri.setId_user(jo.getInt("id_user"));
tri.setDificuldade(jo.getString("dificuldade"));
TrilhosList.add(tri);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MeusTrilhos.this, "error data",
Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("HttpError", "erro" + error);
}
}){
@Override
protected Map<String, String> getParams() throws
AuthFailureError {
Map<String, String> stringMap = new HashMap<>();
stringMap.put("id_user", id_user);
return stringMap;
}
};
requestQueue.add(stringRequest);
}
}
这是我的php脚本
if($_SERVER['REQUEST_METHOD']=='POST'){
$id_user = $_POST['id_user'];
$user = "root";
$pass = "";
$host= "localhost";
$dbname="check";
$con = mysqli_connect($host,$user,$pass,$dbname);
$stmt=$con->prepare("SELECT id,titulo, id_user,dificuldade FROM trilhos
where id_user='".$id_user."'");
//executing the query
$stmt->execute();
$stmt->bind_result($id, $titulo, $id_user, $dificuldade);
$trilhos=array();
while($stmt->fetch()){
$temp=array();
$temp['id']=$id;
$temp['titulo']=$titulo;
$temp['id_user']=$id_user;
$temp['dificuldade']=$dificuldade;
array_push($trilhos, $temp);
}
echo json_encode($trilhos);
mysqli_close($con);
}
错误日志
at org.json.JSON.typeMismatch(JSON.java:111)
false mManagedProfileInQuietMode: false mKeyguardVisible: false
mCurrentUserId:0 mCurrentProfileId:0 mSecondSpaceStatusIconVisible: true
showIcon:false
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
at org.json.JSONObject.<init>(JSONObject.java:173)
trilhosportugal.MeusTrilhos$1.onResponse(MeusTrilhos.java:135)
trilhosportugal.MeusTrilhos$1.onResponse(MeusTrilhos.java:128)