等待onSuccess返回JSON

时间:2016-05-11 14:23:25

标签: android httprequest

我有一个带有函数的类,用于获取我的Cards.class中的一些JSON文件:

public class Cards {
    static JSONObject jsonObj = null;

    public static JSONObject getCards(Context context)
    {
        AsyncHttpClient client = new AsyncHttpClient();
        client.addHeader("x-access-token", Preferences.getToken(context));
        client.get("http://api.app.com/users/" + Preferences.getID(context), null, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] bytes) {
                Log.d("debugIID", "Cards success : " + statusCode);

                String json = new String(bytes);

                try {
                    jsonObj = new JSONObject(json);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.d("debug", "Cards JSONObject : " + jsonObj);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.d("debug", "Cards failure : " + statusCode);
            }
        });

        return(jsonObj);
    }
}

这就是我调用此函数来获取MainActivity.class中的JSON的方法:

final JSONObject json = Cards.getCards(getApplicationContext());
Log.d("DebudIID", "Cards JSON : " + json);

问题是MainActivity中的json Object为null。 在getCards函数中,json与onSuccess很好。

看起来,函数在等待onSuccess之前得到了返回。

我该怎么做?

由于

1 个答案:

答案 0 :(得分:3)

您可以使用回调界面将数据恢复给来电者。请考虑以下示例:

String packageName = “com.android.app”;
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
   // The app is not installed
   intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse(“market://details?id=” + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(“param”, “aaaaa”);
startActivity(intent);

然后您的public interface CardsResponse { onResponseReceived(JSONObject response); } 将如下所示:

getCards

最后来电者:

    public static void getCards(Context context, CardsResponse cardsResponse)
    {
        AsyncHttpClient client = new AsyncHttpClient();
        client.addHeader("x-access-token", Preferences.getToken(context));
        client.get("http://api.app.com/users/" + Preferences.getID(context), null, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] bytes) {
                Log.d("debugIID", "Cards success : " + statusCode);

                String json = new String(bytes);

                try {
                    jsonObj = new JSONObject(json);
                    cardsResponse.onResponseReceived(jsonResponse); // This line will return to your caller

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

                Log.d("debug", "Cards JSONObject : " + jsonObj);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.d("debug", "Cards failure : " + statusCode);
            }
        });
    }