Reputation: 43
I have to POST username, key and another parameter to get data as JSONObject and show them in a RecyclerView. I'm using Volley Library in android, tried every possible way none of them is helping.. even though testing through Postman seems to work fine and I get data when i post those three parameters but I don't know what am I missing in android..
PHP code:
<?php
require("db/Db.class.php");
if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getMainCategory'])){
$db = new DB();
$username = $_POST['username'];
$key = $_POST['key'];
$query = $db->query("SELECT * FROM oc_api where `username` = '$username' and `key` = '$key' limit 1 ");
if(count($query) == 0){
$json = ["error don't have permittion"];
}else{
$catigory = $db->query("SELECT * FROM `oc_category_description`,oc_category WHERE oc_category_description.category_id = oc_category.category_id and oc_category.status = 1 and oc_category.parent_id = 0 and oc_category_description.language_id = 2");
$json = $catigory;
}
header('Content-Type: application/json');
echo json_encode($json);
return;
}
if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getSlideShow'])){
$db = new DB();
$username = $_POST['username'];
$key = $_POST['key'];
$slideShowId = (int)$_POST['getSlideShow'];
$query = $db->query("SELECT * FROM oc_api where `username` = '$username' and `key` = '$key' limit 1 ");
if(count($query) == 0){
$json = ["error don't have permittion"];
}else{
$banderSlideShow = $db->query("SELECT * FROM `oc_banner_image` where `banner_id` = $slideShowId and language_id = 2");
$json = $banderSlideShow;
}
header('Content-Type: application/json');
echo json_encode($json);
return;
}
if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getAllNews'])){
$db = new DB();
$username = $_POST['username'];
$key = $_POST['key'];
$query = $db->query("SELECT * FROM oc_api where `username` = '$username' and `key` = '$key' limit 1 ");
if(count($query) == 0){
$json = ["error don't have permittion"];
}else{
$catigory = $db->query("SELECT * FROM `oc_blog_article`,`oc_blog_article_description` WHERE oc_blog_article_description.blog_article_id = oc_blog_article.blog_article_id and oc_blog_article.status = 1 and oc_blog_article_description.language_id = 2 order by oc_blog_article.sort_order");
$json = $catigory;
}
header('Content-Type: application/json');
echo json_encode($json);
return;
}
$json = ["error"];
header('Content-Type: application/json');
echo json_encode($json);
?>
JAVA code:
public static final String URL_PRODUCTS = "http://vinsmartangle.net/shop/myApi/index.php";
private void loadProducts(final String user, final String api,final String no) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject category = array.getJSONObject(i);
String id = category.getString("category_id");
String name = category.getString("name");
Category data = new Category();
//adding the product to product list
data.setCategoryNo(id);
data.setCategoryName(name);
categoryList.add(data);
}
categoryAdapter = new CategoryAdapter(getActivity(),categoryList, Fragment_Payment.this);
recyclerView.setAdapter(categoryAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
catch (JsonParseException e)
{
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: Error ", error.getCause());
}
})
{
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username ",user);
params.put("key",api);
params.put("getMainCategory",no);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
//adding our stringrequest to queue
Volley.newRequestQueue(getActivity()).add(stringRequest);
}
Postman Screenshot:
[
{
"category_id": "20",
"language_id": "2",
"name": "رمان",
"description": "<p><br><\/p>",
"meta_title": "رمان",
"meta_description": "مثال لوصف القسم",
"meta_keyword": "",
"image": "catalog\/demo\/compaq_presario.jpg",
"image_Icon": "",
"parent_id": "0",
"top": "1",
"column": "1",
"sort_order": "1",
"status": "1",
"date_added": "2009-01-05 21:49:43",
"date_modified": "2019-07-03 01:53:35"
},
{
"category_id": "83",
"language_id": "2",
"name": "خضروات",
"description": "",
"meta_title": "خضروات",
"meta_description": "",
"meta_keyword": "",
"image": "",
"image_Icon": "",
"parent_id": "0",
"top": "1",
"column": "1",
"sort_order": "2",
"status": "1",
"date_added": "2019-07-04 11:05:33",
"date_modified": "2019-07-05 06:16:54"
},
{
"category_id": "85",
"language_id": "2",
"name": "ورقيات",
"description": "",
"meta_title": "ورقيات",
"meta_description": "",
"meta_keyword": "",
"image": "",
"image_Icon": "",
"parent_id": "0",
"top": "0",
"column": "1",
"sort_order": "0",
"status": "1",
"date_added": "2019-07-04 11:06:41",
"date_modified": "2019-07-04 11:06:41"
},
{
"category_id": "90",
"language_id": "2",
"name": "عسل",
"description": "",
"meta_title": "عسل",
"meta_description": "",
"meta_keyword": "",
"image": "",
"image_Icon": "",
"parent_id": "0",
"top": "1",
"column": "1",
"sort_order": "0",
"status": "1",
"date_added": "2019-07-04 11:08:56",
"date_modified": "2019-07-05 11:59:46"
},
{
"category_id": "87",
"language_id": "2",
"name": "صناديق",
"description": "",
"meta_title": "صناديق",
"meta_description": "",
"meta_keyword": "",
"image": "",
"image_Icon": "",
"parent_id": "0",
"top": "1",
"column": "1",
"sort_order": "0",
"status": "1",
"date_added": "2019-07-04 11:07:43",
"date_modified": "2019-07-05 11:59:30"
},
{
"category_id": "57",
"language_id": "2",
"name": "فواكة موسمية",
"description": "<p><br><\/p>",
"meta_title": "فواكة موسمية",
"meta_description": "",
"meta_keyword": "",
"image": "",
"image_Icon": "",
"parent_id": "0",
"top": "1",
"column": "1",
"sort_order": "3",
"status": "1",
"date_added": "2011-04-26 08:53:16",
"date_modified": "2019-07-03 01:52:47"
},
{
"category_id": "89",
"language_id": "2",
"name": "مشروبات",
"description": "",
"meta_title": "مشروبات",
"meta_description": "",
"meta_keyword": "",
"image": "",
"image_Icon": "",
"parent_id": "0",
"top": "0",
"column": "1",
"sort_order": "0",
"status": "1",
"date_added": "2019-07-04 11:08:01",
"date_modified": "2019-07-04 11:08:01"
}
]
and when I run the app I get this exception:
W/System.err: org.json.JSONException: Value error at 0 of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:101)
at org.json.JSONArray.getJSONObject(JSONArray.java:525)
W/System.err: at com.smart.mangojazan.Fragment_Payment$1.onResponse(Fragment_Payment.java:117)
at com.smart.mangojazan.Fragment_Payment$1.onResponse(Fragment_Payment.java:105)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err: at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Upvotes: 1
Views: 302
Reputation: 43
I resolved this issue and it was pretty ridiculous actually.. there was a little space beside "username" in the first params.put which was causing this issue of not getting data and also causing an exception.. even though I double checked but don't know how i missed that.. Thanks for everyone who took time and tried to help me out. I appreciate that.
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
//the line below was causing the exception. right side of "username " (space)
params.put("username ",user);
params.put("key",api);
params.put("getMainCategory",no);
return params;
}
Upvotes: 0
Reputation: 3593
you have done all your code and just before sending the response have set json=["error"];
[don't forget to accept answer and upvote if the answer is correct or articulate (explain more) your question so that answerers can give more relevant answers]
<?php
require("db/Db.class.php");
if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getMainCategory'])){
//your code
//remove below two lines from every if as it can be called commonly at last;
//header('Content-Type: application/json');
//echo json_encode($json);
}else if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getSlideShow'])){
//your code
}else if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getAllNews'])){
//your code
}else{
$json = ["error"];
}
header('Content-Type: application/json');
echo json_encode($json);
?>
Upvotes: 1
Reputation: 634
Since you are expecting JSONArray hence don't go for StringRequest instead use JsonArrayRequest available in volley. See this documentation: https://developer.android.com/training/volley/request
Your error log is also indicating
java.lang.String cannot be converted to JSONObject
Hence I suggest you try this:
JsonArrayRequest req = new JsonArrayRequest(Request.Method.POST, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(JSONArray response) {
//.............
Since you are expecting a json array.
Upvotes: 0