我必须发布用户名,键和另一个参数,以将数据作为JSONObject并在RecyclerView中显示。我在android中使用Volley Library,尝试了所有可能的方法都没有帮助。 即使通过Postman进行的测试似乎可以正常工作,并且在发布这三个参数时也可以获取数据,但是我不知道我在android中缺少什么。
PHP代码:
<?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代码:
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);
}
邮递员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"
}
]
当我运行该应用程序时,出现此异常:
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)
答案 0 :(得分:1)
您已完成所有代码,并在发送响应之前已设置json = [“ error”];
[如果答案正确或表达清楚(请进一步解释),以便回答者给出更相关的答案,请不要忘记接受答案并进行投票]
<?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);
?>
答案 1 :(得分:0)
因为您期望使用JSONArray,所以不要使用StringRequest,而要使用volley中提供的JsonArrayRequest。请参阅此文档:https://developer.android.com/training/volley/request
您的错误日志也表明
java.lang.String无法转换为JSONObject
因此,我建议您尝试以下方法:
JsonArrayRequest req = new JsonArrayRequest(Request.Method.POST, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(JSONArray response) {
//.............
因为您期望使用json数组。
答案 2 :(得分:0)
我解决了这个问题,实际上这很荒谬。 在第一个params.put中的“用户名”旁边,这导致无法获取数据并引起异常的问题..即使我仔细检查了一下,也不知道我是怎么错过的..感谢所有花费时间并尝试过的人帮我我很感激。
@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;
}