gpt4 book ai didi

java - Android异步HTTP库: How to store value from onSuccess method?

转载 作者:行者123 更新时间:2023-12-01 12:30:19 26 4
gpt4 key购买 nike

我需要将 getJobs GET 请求的 onSuccess 方法的值存储到 jobs 变量:

private static JSONArray jobs = null;
...
...
public static JSONArray getJobs(String url) throws JSONException {
final JSONArray[] testArray = {null};
RestClient.get(url, null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
testArray[0] = response.getJSONObject("jobs").getJSONArray("values");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return testArray[0];

当我调用它时

try {
jobs = getJobs(JOBS_SEARCH_URL);
} catch (JSONException e) {
e.printStackTrace();
}

我收到下一个错误。因此,据我了解,由于某种原因,它没有到达 jobs 变量,也没有将其存储在那里......

09-22 01:06:17.364  25310-25310/*my_package* E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: *my_package*, PID: 25310
java.lang.RuntimeException: Unable to start activity ComponentInfo*my_package*/*my_package*.JobsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at *my_package*.JobsActivity.getCompanyNames(JobsActivity.java:126)
at *my_package*.JobsActivity.onCreate(JobsActivity.java:94)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5867)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at dalvik.system.NativeStart.main(Native Method)

P.S.:如果 onSuccess 方法为 void,存储 GET 请求响应的好方法是什么?

最佳答案

由于RestClient.get是异步的,所以getJobs函数不能直接返回值。

如果你想在 onSuccess 之后做一些事情。

您可以喜欢此代码:

public static void getJobs(String url) throws JSONException {
RestClient.get(url, null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
JobsActivity.jobs = response.getJSONObject("jobs").getJSONArray("values");

//store the values.
storeJobs(JobsActivity.jobs);

} catch (JSONException e) {
e.printStackTrace();
}
}
});
}

关于java - Android异步HTTP库: How to store value from onSuccess method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25969595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com