Volley – 无法解析构造函数“JSONObjectRequest

我有这个问题,我跟随

Android Building Free Wallpapers App – Part 2

并且已经到了我添加启动画面并启动Volley请求的程度。

教程在制作JSONObjectRequest时使用的代码是

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

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

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

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

我在SplashActivity.java中的代码是:

 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Window; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.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() { @Override public void onResponse(JSONObject response) { Log.d(TAG, "Albums Response: " + response.toString()); List albums = new ArrayList(); 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  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); } } 

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

(String)null一样转换空值

例如: –

  JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, url, (String)null, new Response.Listener() { @Override public void onResponse(JSONObject response){ pDialog.hide(); Log.d("Reponse", response.toString()); Toast.makeText(getApplicationContext(), response.optString("name"), Toast.LENGTH_LONG).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { volleyError.printStackTrace(); Log.d("Error = ", volleyError.toString()); pDialog.hide(); } }){ @Override protected Map getParams(){ return params; }; } 

该错误是由于使用更高的凌空版本。 请用

 compile 'com.mcxiaoke.volley:library:1.0.0' 

在gradle依赖。 当我遇到同样的问题时,它对我有用。

构造函数中请求的参数不正确,因此您遇到了问题。 创建JsonObjectRequest的正确代码是

 JsonObjectRequest request = new JsonObjectRequest(Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject arg0) { // TODO Auto-generated method stub } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub } });