简体   繁体   English

异步GET请求方法接收数据,但返回空数组,在调试时工作正常

[英]asynchronous GET request method receives data but return null array, works fine while debugging

MainActivity.java MainActivity.java

String[] captcha = new String[2];

b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getMessle();
        }
    });

private void getMessle() {
    captcha[0] = "before";
    captcha[1] = "before2";

    RequestClass rc = new RequestClass();
    captcha = rc.getCaptcha();
    tv.setText(captcha[1]);

}

RequestClass.java RequestClass.java

String[] saResult = new String[2];

public String[] getCaptcha() {

    httpUrl = new HttpUrl.Builder()
            .scheme(strScheme)
            .host(strHost)
            .addPathSegments(path)
            .build();

    request = new Request.Builder()
            .url(httpUrl)
            .addHeader(header, key)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            saResult[0] = response.header("Set-Cookie");
            try {
                jsonObject = new JSONObject(response.body().string());
                saResult[1] = jsonObject.getString("image_url");
            } catch (JSONException je) {

            }
        }
    });
    return saResult;
}

I've checked and it doesn't seem like any exceptions are caught. 我检查了一下,似乎没有发现任何异常。 I also know that my application gets a response as well and the datas are definitely there in onResponse(). 我也知道我的应用程序也能收到响应,并且数据肯定在onResponse()中。 After some extended testing, it looks like my array is assigned before the response arrives. 经过一些扩展的测试后,似乎在响应到达之前就分配了我的数组。

captcha = rc.getCaptcha(); 验证码= rc.getCaptcha();

You call that right after you enqued a request. 您在收到请求后立即致电。 The request is not executed then nor is there a result. 然后不执行该请求,也没有结果。 All goes asynchronous. 一切都是异步的。

Only in onResponse() you will get the result. 仅在onResponse()中,您将获得结果。 A long time later. 很久以后。 And it is in onResponse() that you should handle the result by displaying it in a text view for instance. 在onResponse()中,您应该通过例如在文本视图中显示结果来处理结果。

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

相关问题 WebService方法始终在请求中收到null - WebService method always receives null in request JPA Repository 在获取数据时为空,但在 Spring Boot 中保存数据时工作正常 - JPA Repository is null while getting data but works fine while saving data in Spring Boot 在NULL上进行方法调用可以正常工作-期待NPE - Method invocation on NULL works fine - Expecting NPE Java http请求GET导致超时异常,但在浏览器中可以正常运行 - Java http request GET causes timeout exception while it works fine in the browser 递归方法在调试时有效,但在从JAR运行时会卡住 - Recursive method works while debugging, but get's stuck when running from a JAR getSharedPrefernces仅在一个在另一个活动中正常工作的活动中返回null - getSharedPrefernces return null only in one activity which works fine on another 无法使用 @Controller 获得响应,而 @RestController 工作正常 - Not able to get response with @Controller, while @RestController works fine SmsSentStatus 方法接收 null 而不是 PhoneNumber - SmsSentStatus method receives null instead of PhoneNumber @OnTimer方法在触发时接收空引用 - @OnTimer method receives null references when fired 为什么客户端的input.readObject有时返回“ null”,有时工作正常? - Why client input.readObject sometimes return “null” and sometimes works fine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM