简体   繁体   English

从Android获取带有StringRequest的JSONArray

[英]Get JSONArray with StringRequest from Android

I am trying to get hold of a JSONArray by using StringRequest in Android along with the POST-method. 我试图通过在POST方法中使用Android中的StringRequest来获取JSONArray。

The php-script is written like: php脚本的编写方式如下:

$statement = mysqli_prepare($con, "SELECT * FROM tickets WHERE user_id > 5 AND completed = 1");
    mysqli_stmt_bind_param($statement, "i", $UID);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $user_id, $beacon_id, $timeFrom, $timeTo, $completed);



    $jsonObject = array();
    $response = array();

    while($status = mysqli_stmt_fetch($statement)){
            $jsonObject = null;

            $jsonObject["beacon_id"]= $beacon_id;
            $jsonObject["timeFrom"]= $timeFrom;
            $jsonObject["timeTo"]= $timeTo;

            $response[] = $jsonObject;
    }

echo json_encode($response);?>

Accessing the web-page with the php-script, the following JSONArray is "echoed": 使用php脚本访问网页,以下JSONArray被“回显”:

[{"beacon_id":10,"timeFrom":"2018-02-14 13:07:11","timeTo":"2018-02-14 14:32:53"},{"beacon_id":3,"timeFrom":"2018-02-24 13:33:37","timeTo":"2018-02-24 13:33:47"}]

And as I understand this is a normal JSONArray with square-brackets representing the array and curly-brackets for each JSONObject inside the array. 而且据我所知,这是一个普通的JSONArray,方括号代表数组,而数组内部的每个JSONObject都带有花括号。

When I try to access this JSONArray through a StringRequest in Android Studio (Java), I do not get any results... 当我尝试通过Android Studio(Java)中的StringRequest访问此JSONArray时,未得到任何结果...

The StringRequest code: StringRequest代码:

public class HistoryRequest extends StringRequest {
    private static final String HISTORY_REQUEST_URL = "http://myURL.../myPHPfile.php";
    private Map<String, String> params;

    public HistoryRequest(String user_id, String instanceID, Response.Listener<String> listener) {
        super(Method.POST, HISTORY_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("user_id", user_id);
        params.put("instanceID", instanceID);
}

    @Override
    public Map<String, String> getParams() {return params;}
}

The ResponseListener in the MainActivity is written like: MainActivity中的ResponseListener的编写方式如下:

Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
    JSONArray jsonArray = new JSONArray(response);
    for(int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        String timeFrom = jsonObject.getString("timeFrom");
        String timeTo = jsonObject.getString("timeTo");

        textviewFrom.append(timeFrom);
        textviewTo.append(timeTo);
        }
    }
};

Any tips why I can't make a JSONArray of the String that in the web-page URL looks like a beautiful JSONArray? 为什么我无法制作网页网址中看起来像漂亮的JSONArray的String的JSONArray?

Thanks in advance! 提前致谢!

UPDATE: 更新:

After switching to JSONArrayRequest instead of StringRequest, I can get the data if I change the MySQL part of the php-script to something static like: 切换到JSONArrayRequest而不是StringRequest之后,如果将php-script的MySQL部分更改为静态内容,则可以获取数据:

SELECT * FROM tickets;

Then I can display all the entries. 然后,我可以显示所有条目。 But I tend to send 1 parameter to the server (user_id), so that I can filter the entries based on the user that is logged in. [SELECT * FROM tickets where user_id = ?], like a traditional POST-method.... 但是我倾向于将1个参数发送到服务器(user_id),以便我可以根据登录的用户过滤条目。[SELECT * FROM tickets where user_id =?],就像传统的POST方法... 。

Someone who knows what to do? 谁知道该怎么办? Read several places that you can not @Override the getParams()-method for this... 阅读一些您不能@Override getParams()方法的地方...

SOLVED: 解决了:

I solved the issue by entering the keyvalue (user_id) into the URL. 我通过在URL中输入键值(user_id)解决了该问题。

//Instead of POST:
$user_id = $_POST["user_id"];

// I use SERVER to get the value after ? in the URL:
$user_id = $_SERVER['QUERY_STRING'];

Doing this with the following java-code: 使用以下Java代码执行此操作:

public class HistoryRequest extends JsonArrayRequest {
private static final String HISTORY_REQUEST_URL = "http://myURLorIPaddress/getHistory.php?";

    public HistoryRequest(String UID, Response.Listener<JSONArray> listener) throws JSONException {
        super(Method.POST, HISTORY_REQUEST_URL + UID,null, listener, null);
}
}

This made it possible to filter the result based on user_id and works like a charm :D 这使得基于user_id过滤结果成为可能,并且就像一个超级魅力:D

I do not get to print anything, but I can see that the Volley library is complaining for some external errors (nothing referred to my code). 我什么都没打印,但是我可以看到Volley库在抱怨一些外部错误(没有引用我的代码)。 The log.i("OnResponseLog", response) does not provide anything. log.i(“ OnResponseLog”,response)不提供任何内容。

Picture of error from LogCat: LogCat的错误图片:

I am working in similar project did you check if there are some null entries? 我在类似的项目中工作,您是否检查过一些空条目? I think the problem might be there by checking your LogCat picture. 我认为通过检查LogCat图片可能存在问题。

Check using isNull() function might be good idea. 使用isNull()函数进行检查可能是个好主意。 https://processing.org/reference/JSONObject_isNull_.html https://processing.org/reference/JSONObject_isNull_.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM