繁体   English   中英

如何在Asynctask中加载JsonObjectRequest?

[英]How to load JsonObjectRequest in an Asynctask?

我正在尝试使用进度条在启动画面中使用asynctask加载json请求,但我不知道该怎么做,有人可以帮我吗?

这是我的启动画面活动:

public class SplashActivity extends ActionBarActivity {

private static final String TAG = SplashActivity.class.getSimpleName();
private static final String TAG_FEED = "feed", TAG_ENTRY = "entry",
        TAG_GPHOTO_ID = "gphoto$id", TAG_T = "$t",
        TAG_ALBUM_TITLE = "title";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_splash);



    // Picasa request to get list of albums
    String url = AppConst.URL_PICASA_ALBUMS
            .replace("_PICASA_USER_", AppController.getInstance()
                    .getPrefManger().getGoogleUserName());

    Log.d(TAG, "Albums request url: " + url);

    // Preparing volley's json object request
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,
            null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Albums Response: " + response.toString());
                    List<Category> albums = new ArrayList<Category>();
                    try {
                        // Parsing the json response
                        JSONArray entry = response.getJSONObject(TAG_FEED)
                                .getJSONArray(TAG_ENTRY);

                        // loop through albums nodes and add them to album
                        // list
                        for (int i = 0; i < entry.length(); i++) {
                            JSONObject albumObj = (JSONObject) entry.get(i);
                            // album id
                            String albumId = albumObj.getJSONObject(
                                    TAG_GPHOTO_ID).getString(TAG_T);

                            // album title
                            String albumTitle = albumObj.getJSONObject(
                                    TAG_ALBUM_TITLE).getString(TAG_T);

                            Category album = new Category();
                            album.setId(albumId);
                            album.setTitle(albumTitle);

                            // add album to list
                            albums.add(album);

                            Log.d(TAG, "Album Id: " + albumId
                                    + ", Album Title: " + albumTitle);
                        }

                        // Store albums in shared pref
                        AppController.getInstance().getPrefManger()
                                .storeCategories(albums);

                        // String the main activity
                        Intent intent = new Intent(getApplicationContext(),
                                MainActivity.class);
                        startActivity(intent);
                        // closing spalsh activity
                        finish();

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                getString(R.string.msg_unknown_error),
                                Toast.LENGTH_LONG).show();
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Volley Error: " + error.getMessage());

                    // show error toast
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.splash_error),
                            Toast.LENGTH_LONG).show();

                    // Unable to fetch albums
                    // check for existing Albums data in Shared Preferences
                    if (AppController.getInstance().getPrefManger()
                            .getCategories() != null && AppController.getInstance().getPrefManger()
                            .getCategories().size() > 0) {
                        // String the main activity
                        Intent intent = new Intent(getApplicationContext(),
                                MainActivity.class);
                        startActivity(intent);
                        // closing spalsh activity
                        finish();
                    } else {
                        // Albums data not present in the shared preferences
                        // Launch settings activity, so that user can modify
                        // the settings

                        Intent i = new Intent(SplashActivity.this,
                                SettingsActivity.class);
                        // clear all the activities
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(i);
                    }

                }
            });

//禁用此请求的缓存,以便它始终获取更新的// json

   jsonObjReq.setShouldCache(false);

//发出请求

   AppController.getInstance().addToRequestQueue(jsonObjReq);

}}

使用进度条在我的启动画面中使用asynctask加载json请求

因为使用Volley库,所以无需使用AsyncTask来显示ProgressDialog

后台请求完成后,在UI线程上调用了onResponse方法。 在调用addToRequestQueue方法之前显示ProgressDialog

ProgressDialog pd = ProgressDialog.show(this,"Please Wait...","Please Wait...");
AppController.getInstance().addToRequestQueue(jsonObjReq);

onResponse方法中调用ProgressDialog.dismiss()

@Override
public void onResponse(JSONObject response) {
    //.. your code here...
    pd.dismiss();
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM