Android Async HTTP库:如何从onSuccess方法存储值?

时间:2014-09-22 08:24:36

标签: java scope android-async-http

我需要将onSuccess GET请求的getJobs方法的值存储到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();
}

我收到下一个错误。据我所知,由于某些原因,它没有达到工作变量,也没有将其存储在那里......

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请求响应的好方法是什么?

1 个答案:

答案 0 :(得分:1)

因为RestClient.get是异步的,所以getJobs func不能直接返回值。

如果你想在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();
      }
    }
  });
}