2015-07-20 42 views
4

我有這個問題,我是跟着排球 - 不能解析構造「JSONObjectRequest

http://www.androidhive.info/2014/08/android-building-free-wallpapers-app-part-2/

,並已經開始在那裏我添加一個啓動畫面和我開始凌空請求點

。本教程說在使用JSONObjectRequest時使用的代碼是

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

但是當我輸入它時,它會要求我導入Volley l然後將其更改爲

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

這樣做會導致錯誤無法解析構造函數。

我在我的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<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

請求的構造函數中的參數不正確,因此您正面臨問題。正確的代碼來創建一個JsonObjectRequest

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

      @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 

      } 
     }); 
+1

Thanks for that,但是當我完全按照你的意思鍵入它時,Android Studio突出顯示「方法」並要求導入com.volley.android.method.Request。一旦我這樣做,它將Method.GET更改爲Request.Method.GET?然後出錯代碼爲 – x10sion

+0

的整塊代碼沒有問題Request.Method.GET和Method.GET的意思是相同的 –

+0

你也可以用0代替Method.GET但是不推薦的方式 –

28

你要把你的空值像(字符串)空

對於如: -

JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, url, (String)null, new Response.Listener<JSONObject>() { 
      @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<String, String> getParams(){ 
       return params; 
      }; 
     } 
2

的錯誤是由於較高的使用排球版。 請使用

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

在gradle依賴。 當我遇到同樣的問題時,它解決了我的問題。