Php 截击发送参数并获得响应
我有一个应用程序,我使用Php 截击发送参数并获得响应,php,android,mysql,json,android-volley,Php,Android,Mysql,Json,Android Volley,我有一个应用程序,我使用phpapi从mysql插入和检索gps数据。我在发送或获取数据方面没有问题,我正在努力解决的问题是,当我尝试将参数发送到我的api(例如用户id)时,我得到了错误数据,这意味着我在请求时做了一些错误的事情。我正在尝试发送用户id,然后将数据检索到与该id相关联的列表视图中。我看到了一些使用JsonArrayRequest的示例,这可能是我应该使用的,但我在尝试使用Post方法和参数实现它时不断出错 public void test( final String id_us
phpapi
从mysql
插入和检索gps数据。我在发送或获取数据方面没有问题,我正在努力解决的问题是,当我尝试将参数发送到我的api
(例如用户id)时,我得到了错误数据,这意味着我在请求时做了一些错误的事情。我正在尝试发送用户id
,然后将数据检索到与该id相关联的列表视图中。我看到了一些使用JsonArrayRequest
的示例,这可能是我应该使用的,但我在尝试使用Post方法和参数实现它时不断出错
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);
}
}
错误日志
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)
org.json.json.typeMismatch(json.java:111)上的
false mManagedProfileInQuietMode:false mKeyguardVisible:false
mCurrentUserId:0 mCurrentProfileId:0 mSecondSpaceStatusIconVisible:true
showIcon:false
W/System.err:org.json.JSONObject(JSONObject.java:160)
位于org.json.JSONObject(JSONObject.java:173)
Trilhosportagulate.MeusTrilhos$1.onResponse(MeusTrilhos.java:135)
Trilhosplugate.MeusTrilhos$1.onResponse(MeusTrilhos.java:128)
解决了它,尝试检索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);
}
public void test2(最终字符串id\u用户){
RequestQueue RequestQueue=Volley.newRequestQueue(MeusTrilhos.this);
StringRequest StringRequest=新的StringRequest(Request.Method.POST,urlget,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
System.out.println(响应);
Toast.makeText(MeusTrilhos.this,“boa entrou”,
Toast.LENGTH_LONG).show();
试一试{
JSONArray数组=新的JSONArray(响应);
对于(int i=0;i
使用固定id测试php端,以确保错误不在服务器端。这表明android端存在问题…请使用postman发布错误logsIve测试php端,我得到如下回报:[{“id”:68,“titulo”:“teste cati”,“id_user”:6,“Deficuldade”:“Facil”}]发布错误日志我用日志更新问题,但我不知道如何正确格式化日志响应是json数组而不是对象…你能发布json响应吗
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);
}