這裏是我的SplashActivity和JSON呼叫即時通訊試圖使用flickr API ..我一直關注教程上的壁紙應用程序..W/System.err:org.json.JSONException:沒有值的飼料..即時通訊獲取此錯誤..即時通訊使用flickr API
總是得到這個錯誤消息..我不知道該怎麼辦?
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.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.shyamdev.animewallpaperhd.app.AppConst;
import com.shyamdev.animewallpaperhd.app.AppController;
import com.shyamdev.animewallpaperhd.picasa.model.Category;
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_FLICKR_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,
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
// | 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);
}
這裏是logcat的..這裏是錯誤
enter code here
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: org.json.JSONException: No value for feed
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at org.json.JSONObject.getJSONObject(JSONObject.java:609)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at com.shyamdev.animewallpaperhd.SplashActivity$1.onResponse(SplashActivity.java:55)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at com.shyamdev.animewallpaperhd.SplashActivity$1.onResponse(SplashActivity.java:47)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:72)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at android.os.Looper.loop(Looper.java:135)
10-18 06:17:11.937 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5289)
10-18 06:17:11.938 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-18 06:17:11.938 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
10-18 06:17:11.938 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-18 06:17:11.938 20262-20262/com.shyamdev.animewallpaperhd W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-18 06:17:11.998 20262-20302/com.shyamdev.animewallpaperhd V/RenderScript: Application requested CPU execution
10-18 06:17:12.002 20262-20302/com.shyamdev.animewallpaperhd V/RenderScript: 0x558bec9140 Launching thread(s), CPUs 4
需要幫助
import java.io.Serializable;
public class Wallpaper implements Serializable {
private static final long serialVersionUID = 1L;
private String url, photoJson;
private int width, height;
public Wallpaper() {
}
public Wallpaper(String photoJson, String url, int width, int height) {
this.photoJson = photoJson;
this.url = url;
this.width = width;
this.height = height;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPhotoJson() {
return photoJson;
}
public void setPhotoJson(String photoJson) {
this.photoJson = photoJson;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
這裏prefManager
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import com.google.gson.Gson;
import com.shyamdev.animewallpaperhd.app.AppConst;
import com.shyamdev.animewallpaperhd.picasa.model.Category;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class PrefManager {
private static final String TAG = PrefManager.class.getSimpleName();
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "AwesomeWallpapers";
// Google's username
private static final String KEY_GOOGLE_USERNAME = "google_username";
// No of grid columns
private static final String KEY_NO_OF_COLUMNS = "no_of_columns";
// Gallery directory name
private static final String KEY_GALLERY_NAME = "gallery_name";
// gallery albums key
private static final String KEY_ALBUMS = "albums";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
}
/**
* Storing google username
* */
public void setGoogleUsername(String googleUsername) {
editor = pref.edit();
editor.putString(KEY_GOOGLE_USERNAME, googleUsername);
// commit changes
editor.commit();
}
public String getGoogleUserName() {
return pref.getString(KEY_GOOGLE_USERNAME, AppConst.PICASA_USER);
}
/**
* store number of grid columns
* */
public void setNoOfGridColumns(int columns) {
editor = pref.edit();
editor.putInt(KEY_NO_OF_COLUMNS, columns);
// commit changes
editor.commit();
}
public int getNoOfGridColumns() {
return pref.getInt(KEY_NO_OF_COLUMNS, AppConst.NUM_OF_COLUMNS);
}
/**
* storing gallery name
* */
public void setGalleryName(String galleryName) {
editor = pref.edit();
editor.putString(KEY_GALLERY_NAME, galleryName);
// commit changes
editor.commit();
}
public String getGalleryName() {
return pref.getString(KEY_GALLERY_NAME, AppConst.SDCARD_DIR_NAME);
}
/**
* Storing albums in shared preferences
* */
public void storeCategories(List<Category> albums) {
editor = pref.edit();
Gson gson = new Gson();
Log.d(TAG, "Albums: " + gson.toJson(albums));
editor.putString(KEY_ALBUMS, gson.toJson(albums));
// save changes
editor.commit();
}
/**
* Fetching albums from shared preferences. Albums will be sorted before
* returning in alphabetical order
* */
public List<Category> getCategories() {
List<Category> albums = new ArrayList<Category>();
if (pref.contains(KEY_ALBUMS)) {
String json = pref.getString(KEY_ALBUMS, null);
Gson gson = new Gson();
Category[] albumArry = gson.fromJson(json, Category[].class);
albums = Arrays.asList(albumArry);
albums = new ArrayList<Category>(albums);
} else
return null;
List<Category> allAlbums = albums;
// Sort the albums in alphabetical order
Collections.sort(allAlbums, new Comparator<Category>() {
public int compare(Category a1, Category a2) {
return a1.getTitle().compareToIgnoreCase(a2.getTitle());
}
});
return allAlbums;
}
/**
* Comparing albums titles for sorting
* */
public class CustomComparator implements Comparator<Category> {
@Override
public int compare(Category c1, Category c2) {
return c1.getTitle().compareTo(c2.getTitle());
}
}
我認爲你在JSON解析期間遇到問題。也請添加JSON。 –
thnxx需要時間閱讀..我不知道該怎麼辦..這裏是教程網站,我一直遵循http://www.androidhive.info/2014/08/android-building-free-wallpapers- app-part-1/ –
您的API已投入使用?請檢查回覆併發布。 – Rahul