0

我正在尝试从http://projectxapp.herokuapp.com/dashboard/ads/api/v1/ads/的 Json 格式获取数据

它显示 NullPointerException,我尝试了多种方法来解决它。但是我还是没有想法!!objectList 似乎是空的。

public class ImagePagerActivity extends Activity {
// url to make request
private static String url =         
  "http://projectxapp.herokuapp.com/dashboard/ads/api/v1/ads/";

// JSON Node names
static final String TAG_META = "meta";
static final String TAG_META_LIMIT = "limit";
private static final String TAG_META_NEXT = "next";
private static final String TAG_META_total_count = "count";
private static final String TAG_OBJECTS = "objects";
private static final String TAG_OBJECTS_END_TIME = "endTime";
private static final String TAG_OBJECTS_ID = "id";
private static final String TAG_OBJECTS_LINK = "link";
private static final String TAG_OBJECTS_PHOTO = "photo";
private static final String TAG_OBJECTS_PHOTO_HEIGHT = "height";
static final String TAG_OBJECTS_PHOTO_RESOURCE_URI = "resourceUri";
private static final String TAG_OBJECTS_PHOTO_SOURCE = "source";
private static final String TAG_OBJECTS_PHOTO_WIDTH = "width";
private static final String TAG_OBJECTS_RESOURCE_URI = "OResourceUri";
private static final String TAG_OBJECTS_START_TIME = "startTime";
// contacts JSONArray
JSONArray meta = null;
JSONArray objects = null;

//
private static final String TEST_FILE_NAME = "Universal Image Loader @#&=+-_.,!()~'%20.png";
private static final String STATE_POSITION = "STATE_POSITION";
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;

ViewPager pager;

@SuppressWarnings("null")
@SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState) {
    //
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads().detectDiskWrites().detectNetwork() 
            .penaltyLog().build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
            .penaltyLog().penaltyDeath().build());
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    ;
    setContentView(R.layout.mos);

    File testImageOnSdCard = new File("/mnt/sdcard", TEST_FILE_NAME);
    if (!testImageOnSdCard.exists()) {
        copyTestImageToSdCard(testImageOnSdCard);
    }

    ArrayList<HashMap<String, String>> objectsList = new     
                                           ArrayList<HashMap<String, String>>();
    JSONParser jParser = new JSONParser();
    JSONObject json = null;
    try {

        json = jParser.getStringContent(url);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        // Getting Array of Contacts
        objects = json.getJSONArray(TAG_OBJECTS);

        // looping through All Contacts
        for (int i = 0; i < objects.length(); i++) {
            JSONObject c = objects.getJSONObject(i);
            String link = c.getString(TAG_OBJECTS_LINK);
            JSONObject photo = c.getJSONObject(TAG_OBJECTS_PHOTO);
            String source = photo.getString(TAG_OBJECTS_PHOTO_SOURCE);
        HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_OBJECTS_LINK, link);
            map.put(TAG_OBJECTS_PHOTO_SOURCE, source);
            // adding HashList to ArrayList
            objectsList.add(map);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

     String[] imageUrls = null;
     if(objectsList.isEmpty());
     Toast.makeText(this, "No Data", Toast.LENGTH_LONG).show();
     Log.i("getData1", "!!!!!!!!!!!");
             //
             for(int i=0; i<objects.length(); i++)
     {
         Log.i("getData2", "!!!!!!!!!!!");
        imageUrls[i]=objectsList.get(i).get(TAG_OBJECTS_PHOTO_RESOURCE_URI);
        Log.i("getData3", "!!!!!!!!!!!");
     }
             //
       private void copyTestImageToSdCard(final File testImageOnSdCard) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                InputStream is = getAssets().open(TEST_FILE_NAME);
                FileOutputStream fos = new FileOutputStream(
                        testImageOnSdCard);
                byte[] buffer = new byte[8192];
                int read;
                try {
                    while ((read = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, read);
                    }
                } finally {
                    fos.flush();
                    fos.close();
                    is.close();
                }
            } catch (IOException e) {
                L.w("Can't copy test image onto SD card");
            }
        }
    }).start();
}

有 JSONParser()。

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}
public static JSONObject getStringContent(String uri) throws Exception {

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(uri));
        HttpResponse response = client.execute(request);
        InputStream ips  = response.getEntity().getContent();
        BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));

        StringBuilder sb = new StringBuilder();
        String s;
        while(true )
        {
            s = buf.readLine();
            if(s==null || s.length()==0)
                break;
            sb.append(s);

        }
        buf.close();
        ips.close();
        jObj = new JSONObject(sb.toString());
        return jObj;

        } 



}

这些是下面的 logcat 消息!

   04-05 07:25:17.889: E/StrictMode(630):   at  
   java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
   04-05 07:25:17.889: E/StrictMode(630):   at java.lang.Thread.run(Thread.java:856)
   04-05 07:25:21.831: E/AndroidRuntime(3285): FATAL EXCEPTION: main
   04-05 07:25:21.831: E/AndroidRuntime(3285): java.lang.RuntimeException: Unable to        
   start activity ComponentInfo{com.sai.mosorder/com.sai.mosorder.ImagePagerActivity}:
   java.lang.NullPointerException
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at      
   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at   
   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at   
   android.app.ActivityThread.access$600(ActivityThread.java:141)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at   
   android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at    
   android.os.Handler.dispatchMessage(Handler.java:99)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at     
   android.os.Looper.loop(Looper.java:137)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at   
   android.app.ActivityThread.main(ActivityThread.java:5041)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at  
   java.lang.reflect.Method.invokeNative(Native Method)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at   
   java.lang.reflect.Method.invoke(Method.java:511)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at  
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at    
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at    
   dalvik.system.NativeStart.main(Native Method)
   04-05 07:25:21.831: E/AndroidRuntime(3285): Caused by:     
   java.lang.NullPointerException
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at    
   com.sai.mosorder.ImagePagerActivity.onCreate(ImagePagerActivity.java:132)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at   
   android.app.Activity.performCreate(Activity.java:5104)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at    
   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  at 
   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
   04-05 07:25:21.831: E/AndroidRuntime(3285):  ... 11 more
   04-05 07:25:22.200: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a    
   build property
   04-05 07:25:22.560: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a     
   build property
   04-05 07:26:07.869: E/ActivityThread(630):   at    
   com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
4

1 回答 1

0

我找到了答案!!!!如果您对在 android 上使用 Json 感到困惑。你必须检查这个网站。

http://mobiforge.com/developing/story/sumption-json-services-android-apps

于 2013-04-06T12:25:58.223 回答