这似乎是一个已经问过的问题,但我已经在stackoverflow上尝试了几乎所有问题。
我无法通过凌空接收来自服务器的响应。 onErrorRespose()内部的错误是:
com.android.volley.ParseError: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
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";
这是我的makeRequest()方法,我在onClick()侦听器中调用:
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"}
答案 0 :(得分:1)
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require_once("dbcon.php");
$response=array();
$response['error'] = false;
$response['merchants'] = array();
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['info_req'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$info_req = $_POST['info_req'];
$con=mysqli_connect(details hidden from public);
if (!$con) {
$response['error'] = true;
$response['message'] = "Connection Failed. Bad Access to database!";
} 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)){
$temp = array();
$temp['id'] = $row['id'];
$temp['name'] = $row['name'];
array_push($response['merchants'], $temp);
}
} else if($info_req == "merchant_details") {
$merchant_id = $_POST["merchant_id"];
$query="SELECT name, shop_name, address FROM merchant_side_records WHERE id='{$merchant_id}'";
$result=mysqli_query($con,$query);
while($row=mysqli_fetch_assoc($result)) {
$temp = array();
$temp['name'] = $row['name'];
$temp['shop_name'] = $row['shop_name'];
$temp['address'] = $row['address'];
array_push($response['merchants'], $temp);
}
}
} else {
$response['error'] = true;
$response['message'] = "Login failed";
}
}
} else {
$response['error'] = true;
$response['message'] = "404 not found...";
}
} else {
$response['error'] = true;
$response['message'] = "Please check method. it must be POST";
}
header("content-type:application/json");
echo json_encode($response);
?>
//output as shown below as jsonarray
{
"error": false
"merchants": [{
"id": 1,
"name": abc
},
{
"id": 1,
"name": xyz
}]
}
// change ur android response in volley
boolean error = response.getBoolean("error"));
if(!error) {
JsonArray jsonArray = response.getJSONArray("merchants"));
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
}
}
答案 1 :(得分:1)
好的,我一直在研究很多主题,以便找到解决方案。
确切的问题 :错误的json形成和错误(没错!)参数被传递。
我经历了很多答案而且大多数建议:
我想提前注意一些事情,并明确地将其全部说明给其他人:
但在让我概述一下我最初在做什么之前
getBody()
或getParams()
方法来传递参数。 虽然上面的东西看起来不错,但是来自这里和那里的混乱碎片,猜猜是什么,它们不属于同一个谜题。
现在有一件事我想在前进之前明确表达:
如果您在继续操作之前还不知道,请阅读this。
现在让我们继续进行
如果您使用StringRequest
典型的StringRequest看起来像
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
.
.
.
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
.
.
.
}
}){
.
.
.
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
如果我们使用这种方式接收数据,请注意响应是字符串形式。所以作为一个明显的陈述,php必须回显一个字符串。
您可以收集该字符串并从中生成JSON对象。该字符串必须类似于this。
覆盖getParams()方法以传递POST参数
然后以正常方式继续(操纵该JSON对象......)
如果您使用JsonArrayRequest
首先,我们期待一个来自php的JSON数组,所以php必须回显JSON数组(以方括号开头的那个)
其次,传递POST参数不会以通常的重写方式工作。您必须制作地图并向其添加参数。然后从中生成一个JSON对象,并在第三个参数中传递该对象。
类似的东西:
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");
JSONObject parameters =new JSONObject(params);
public JsonArrayRequest(int method, String url, JSONObject **params**,
Listener<JSONArray> listener, ErrorListener errorListener)
当我们将JSON对象作为post参数传递时,php在POST中接收JSON而不是标准值。
所以你必须像这样解码那些帖子参数:
$json = json_decode( file_get_contents('php://input') );
然后检查这样的参数:
if (isset($json->{'username'}) && isset($json->{'password'}) && isset($json->{'password'})) {
$username = $json->{'username'};
$password = $json->{'password'};
$info_req = $json->{'info_req'};
}
然后当您在onRespose中接收JSON数组时,迭代它并操纵其内容。
如果您使用JsonObjectRequest
(出于不明原因,这在某种程度上更有趣,至少在我的情况下!)
同样,我们期待一个对象,而不是数组!
以类似的方式,我们通过由地图构成的JSON对象发送参数,就像在JsonArrayRequest中一样
所以这必然意味着php正在接收JSON对象作为POST参数,我们必须像以前一样(JsonArrayRequest)在php中做。
现在,在所有这些之后,另一个重要的事情要注意:
您必须准确了解如何制作JSON对象或JSON数组。
In my case(the working one) I went with the third way, receiving JSON object that contains a JSON array with all the necessary values in form of nested JSON objects.
这样的事情:
{"Tag":[{"error":true},{"id":1,"name":"Vidor Vistrom"},{"id":2,"name":"Dealo Ell"}]}
您可能希望看到我的实现(虽然php是安全漏洞!但是对于公共使用,所以不要对公众曝光持怀疑态度):
答案 2 :(得分:0)
尝试覆盖 getBodyContentType
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}