Android异步HTTP库:如何存储onSuccess方法的值



我需要将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函数不能直接返回值。

如果你想在成功之后做点什么。

你可以喜欢这个代码:

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();
      }
    }
  });
}

相关内容

  • 没有找到相关文章

最新更新