我有一个应用,我使用mysql
从php api
插入并检索gps数据。我发送或获取数据没有问题,我正在努力的部分是当我尝试向我的api
发送参数时(例如user_id)我得到Error Data
这意味着我'我的要求做错了。我正在尝试发送user_id
,然后将数据检索到与该ID相关联的listview
。我看到了一些使用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)
答案 0 :(得分:1)
解决了它,试图检索jsonobject而不是jsonarray
public void test2(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) {
System.out.println(response);
Toast.makeText(MeusTrilhos.this, "boa entrou",
Toast.LENGTH_LONG).show();
try {
JSONArray array=new JSONArray(response);
for (int i=0; i < array.length(); i++) {
JSONObject jo=array.getJSONObject(i);
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();
}
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MeusTrilhos.this, "erro erro",
Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params=new HashMap<String, String>();
params.put("id_user", id_user);
return params;
}
};
requestQueue.add(stringRequest);
}