【发布时间】:2017-12-31 08:05:25
【问题描述】:
这似乎是一个已经提出的问题,但我已经尝试了几乎所有关于 stackoverflow 的问题。
我无法使用 volley 接收来自服务器的响应。 onErrorRespose() 里面的错误是:
com.android.volley.ParseError: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
- 使用 POST 方法
-
GET方法php的样子:
所以参数必须看起来像:
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, "loyal_customer");
params.put(KEY_PASSWORD, "custumerXYZ");
params.put(KEY_INFO, "merchant_names");
return params;
}
其中全局中的键定义为:
public static final String KEY_USERNAME="username";
public static final String KEY_PASSWORD="password";
public static final String client_side_php="https://vidorvistrom.000webhostapp.com/client_access.php";
public static final String KEY_INFO="info_req";
这是我在 onClick() 监听器中调用的 makeRequest() 方法:
public void makeRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
final JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, client_side_php, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("response: ", String.valueOf(response.getJSONObject("name")));// response.getString("name") also tried and name is the column name in the database which works fine with GET
} catch (JSONException e) {
Log.d("this: ",e.toString());
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, "loyal_customer");
params.put(KEY_PASSWORD, "custumerXYZ");
params.put(KEY_INFO, "merchant_names");
return params;
}
};
requestQueue.add(jsObjRequest);
}
我担心我错过了什么。这是我第一次尝试排球。
应用程序不会崩溃。它根本不做任何事情。
php代码:
<?php
$username=$_POST["username"];
$password=$_POST["password"];
$info_req=$_POST["info_req"];
if($info_req==""){
$string.="Error 404. Not Found";
$data[]=$string;
}
else{
$con=mysqli_connect(details hidden from public);
$string="";
$data=array();
if(!$con){
$string.="Connection Failed. Bad Access to database!";
$data[]=$string;
}
else{
$query="Select * from client_side_records where username='{$username}'";
$result=mysqli_query($con,$query);
$row=mysqli_fetch_assoc($result);
$pass_db=$row["password"];
if($pass_db==$password){
if($info_req=="merchant_names"){
$query="Select name, id from merchant_side_records";
$result=mysqli_query($con,$query);
while($row=mysqli_fetch_assoc($result)){
foreach($row as $key => $value) {
$data[$key] = $value;
}
}
}
}
else{
$string.="Login Failed";
$data[]=$string;
}
}
}
$data=json_encode($data);
echo $data;
?>
按照建议,我尝试使用 hurl.it 检查服务器是否响应发布请求,我似乎很好:
POST https://vidorvistrom.000webhostapp.com/client_access.php
200 OK 36 bytes 480 ms
View Request View Response
HEADERS
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Tue, 25 Jul 2017 12:09:17 GMT
Server: awex
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Request-Id: 8edf2f8e9e53f138e700aef92d58045a
X-Xss-Protection: 1; mode=block
BODY view formatted
{"name":"Paan Singh Tomar","id":"1"}
【问题讨论】:
-
没有回应是什么意思?它崩溃了吗?还是什么?
-
@OussemaAroua 不,它不会崩溃。
-
您是否使用过
postman或hurl.it 等其他工具测试过这个请求? -
添加 lyour logcat 并且不要将您的 api 链接公开
-
@OussemaAroua 当然我会用 logcat 更新,api 链接不是问题。只是一个试验。
标签: php android json android-volley response