php - No response from volley -
this seems asked question, have tried every question on stackoverflow.
i unable receive response server using volley. error inside onerrorrespose() is:
com.android.volley.parseerror: org.json.jsonexception: value <br of type java.lang.string cannot converted jsonobject
- using post method
the method php looks like:
so params must like:
@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; }
where keys in global defined as:
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";
and here makerequest() method call inside onclick() listener:
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") tried , name column name in database works fine } 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); }
i afraid missing something. first try on volley.
the application doesn't crash. don't @ all.
php code:
<?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 public); $string=""; $data=array(); if(!$con){ $string.="connection failed. bad access database!"; $data[]=$string; } else{ $query="select * client_side_records 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 merchant_side_records"; $result=mysqli_query($con,$query); while($row=mysqli_fetch_assoc($result)){ foreach($row $key => $value) { $data[$key] = $value; } } } } else{ $string.="login failed"; $data[]=$string; } } } $data=json_encode($data); echo $data; ?>
as suggested tried using hurl.it check if server responding post request, seems fine:
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"}
<?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 public); if (!$con) { $response['error'] = true; $response['message'] = "connection failed. bad access database!"; } else { $query="select * client_side_records 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 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 merchant_side_records 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. must post"; } header("content-type:application/json"); echo json_encode($response); ?> //output shown below 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 = 0; < jsonarray.length(); i++) { jsonobject jsonobject = jsonarray.getjsonobject(i); string name = jsonobject.getstring("name"); } }
Comments
Post a Comment