有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Volley无法解析构造函数“JSONObjectRequest”

我关注的是这个问题

http://www.安卓hive.info/2014/08/安卓-building-free-wallpapers-app-part-2/

现在我加了一个闪屏,开始了截击请求

教程中提到的在发出JSONObject请求时要使用的代码是

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>()

但当我输入时,它会要求我导入截击库,然后将其更改为

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()

这样做会导致无法解析构造函数的错误

我在Splash活动中的代码。java是:

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Window;
import 安卓.widget.Toast;

import com.安卓.volley.Request;
import com.安卓.volley.Response;
import com.安卓.volley.VolleyError;
import com.安卓.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends Activity {
    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);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();
    setContentView(R.layout.activity_splash);

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

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

    // Preparing volley's json object request

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.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().getPrefManager()
                        .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().getPrefManager()
                    .getCategories() != null && AppController.getInstance().getPrefManager()
                    .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
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(i);
            }

        }
    });

    // disable the cache for this request, so that it always fetches updated
    // json
    jsonObjReq.setShouldCache(false);

    // Making the request
    AppController.getInstance().addToRequestQueue(jsonObjReq);

}

}

我不知道该怎么做才能解决这个问题


共 (0) 个答案