解析graph.facebook专辑

时间:2013-07-04 11:14:35

标签: android android-parser

我正在从graph.facebook.com解析json。我设法从graph.facebook.com获得了json。但是我现在得到的问题是我可以从this图中检索“id”中的值。但是,当试图从专辑中检索价值时,我发现这样的错误。

07-04 16:40:46.939: E/AndroidRuntime(13450): FATAL EXCEPTION: AsyncTask #1
07-04 16:40:46.939: E/AndroidRuntime(13450): java.lang.RuntimeException: An error occured while executing doInBackground()
07-04 16:40:46.939: E/AndroidRuntime(13450):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.lang.Thread.run(Thread.java:1019)
07-04 16:40:46.939: E/AndroidRuntime(13450): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-04 16:40:46.939: E/AndroidRuntime(13450):    at android.os.Handler.<init>(Handler.java:121)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at android.widget.Toast.<init>(Toast.java:68)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at android.widget.Toast.makeText(Toast.java:231)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at com.ministry.ensing119app.photos.GetPath$Message.doInBackground(GetPath.java:114)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at com.ministry.ensing119app.photos.GetPath$Message.doInBackground(GetPath.java:1)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-04 16:40:46.939: E/AndroidRuntime(13450):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-04 16:40:46.939: E/AndroidRuntime(13450):    ... 4 more
07-04 16:40:47.149: E/WindowManager(13450): Activity com.ministry.ensing119app.photos.GetPath has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40591a50 that was originally added here
07-04 16:40:47.149: E/WindowManager(13450): android.view.WindowLeaked: Activity com.ministry.ensing119app.photos.GetPath has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40591a50 that was originally added here
07-04 16:40:47.149: E/WindowManager(13450):     at android.view.ViewRoot.<init>(ViewRoot.java:265)
07-04 16:40:47.149: E/WindowManager(13450):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
07-04 16:40:47.149: E/WindowManager(13450):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-04 16:40:47.149: E/WindowManager(13450):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-04 16:40:47.149: E/WindowManager(13450):     at android.app.Dialog.show(Dialog.java:241)
07-04 16:40:47.149: E/WindowManager(13450):     at com.ministry.ensing119app.photos.GetPath$Message.onPreExecute(GetPath.java:159)
07-04 16:40:47.149: E/WindowManager(13450):     at android.os.AsyncTask.execute(AsyncTask.java:391)
07-04 16:40:47.149: E/WindowManager(13450):     at com.ministry.ensing119app.photos.GetPath.onCreate(GetPath.java:49)
07-04 16:40:47.149: E/WindowManager(13450):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-04 16:40:47.149: E/WindowManager(13450):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
07-04 16:40:47.149: E/WindowManager(13450):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
07-04 16:40:47.149: E/WindowManager(13450):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
07-04 16:40:47.149: E/WindowManager(13450):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
07-04 16:40:47.149: E/WindowManager(13450):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 16:40:47.149: E/WindowManager(13450):     at android.os.Looper.loop(Looper.java:123)
07-04 16:40:47.149: E/WindowManager(13450):     at android.app.ActivityThread.main(ActivityThread.java:3701)
07-04 16:40:47.149: E/WindowManager(13450):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 16:40:47.149: E/WindowManager(13450):     at java.lang.reflect.Method.invoke(Method.java:507)
07-04 16:40:47.149: E/WindowManager(13450):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
07-04 16:40:47.149: E/WindowManager(13450):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
07-04 16:40:47.149: E/WindowManager(13450):     at dalvik.system.NativeStart.main(Native Method)

我解析json的代码是这样的。

getSource.java

import com.ministry.ensing119app.HomeScreen;
import com.ministry.ensing119app.R;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import java.util.ArrayList;
import java.util.HashMap;

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

import android.os.AsyncTask;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class GetPath extends Activity {

    public static String url = "http://graph.facebook.com/fundamentalalvarado/?fields=albums.fields(photos.fields(source))";
    public String[] IMAGES;

        // JSON Node names
        protected static final String TAG_PRODUCTS = "image_path";
        protected static final String TAG_CID = "file_id";
        public static final String TAG_NAME = "file_name";

        // contacts JSONArray  
        JSONArray products = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ConnectivityManager connectivityManager 
        = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();


        if ((activeNetworkInfo != null && activeNetworkInfo.isConnected())) {
            Log.d("if condition", "if condition is running");
            new Message().execute(url);

        } else {
            Log.d("if condition", "else condition is running");
        Toast.makeText(getApplicationContext(), "The internet is not present or low. Please check", Toast.LENGTH_LONG).show();
        Intent returnIntent = new Intent(GetPath.this, HomeScreen.class);
        startActivity(returnIntent);
        }
    }

    class Message extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        ProgressDialog progress = new ProgressDialog(GetPath.this);
        JSONParser jParser = new JSONParser();
                @Override
            protected ArrayList<HashMap<String, String>> doInBackground(String... params) {

                    Log.d("doInBackgound","backgound is running");

                    // getting JSON string from URL
                    JSONObject json = jParser.getJSONFromUrl(url);
                    Log.d("Json", json.toString());

                    Log.d("path_parsing", "before parsing");
                    try {
                            Log.d("json id", json.getString("id"));
                            JSONArray albumArray = json.getJSONArray("albums");
                            for(int i=0; i<albumArray.length();i++){
                                JSONArray dataArray = albumArray.getJSONObject(i).getJSONArray("data");
                                Log.d("jsons", dataArray.toString());
                                for(int j=0;j<dataArray.length();j++){
                                    JSONArray photoArray = dataArray.getJSONObject(j).getJSONArray("photos");
                                    Log.d("jsons", photoArray.toString());
                                    for(int k=0; k<photoArray.length();k++){
                                        JSONArray dataArray1 = photoArray.getJSONObject(k).getJSONArray("data");
                                        Log.d("jsons", dataArray1.toString());
                                        for(int l=0;l<dataArray1.length();l++){
                                            String source = dataArray1.getJSONObject(l).getString("source");
                                        }
                                    }
                                }
                            }

                        // Getting Array of Contacts
                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(), "Cannot get the images", Toast.LENGTH_LONG).show();
                        Intent goBack = new Intent(GetPath.this,GridActivity.class);
                        startActivity(goBack);
                        e.printStackTrace();
                    }
                    Log.d("path_parsing", "after parsing");
                    return mylist;

                }

            @Override
            protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
                if(progress.isShowing()){
                    progress.cancel();
                }
                IMAGES = new String[result.size()];
                for(int i=0; i<result.size(); i++){
                   IMAGES[i] = result.get(i).get("file_name");
                }
                for(String path: IMAGES){
                    Log.d("path", path.toString());
                }
                Log.d("postExecute","Postexecute is running");

                Bundle b=new Bundle();
                b.putStringArray("IMAGES", IMAGES);
                Intent i=new Intent(GetPath.this, GridActivity.class);
                i.putExtras(b);
                startActivity(i);
                GetPath.this.finish();
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                // TODO Auto-generated method stub
                super.onProgressUpdate(values);

            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                progress.setTitle("Loading");
                progress.setMessage("Retrieving Images");
                progress.show();
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.get_path, menu);
        return true;
    }
}

任何人都可以告诉我哪里出错了。我是android的新手。

1 个答案:

答案 0 :(得分:2)

经过几个小时的刮擦,来到这个解决方案。效率不高,但它会做你需要的东西。

JSONObject albumObj = json.getJSONObject("albums");
//                          Log.d("Json values", albumObj.toString());
                            JSONArray dataArray1 = albumObj.getJSONArray("data");
                            for(int i=0; i<dataArray1.length(); i++){
//                              Log.d("data id", dataArray1.getJSONObject(i).getString("id"));
                                JSONObject photoObj= dataArray1.getJSONObject(i).getJSONObject("photos");
//                              Log.d("photo object", photoObj.toString());
                                JSONArray dataArray2 = photoObj.getJSONArray("data");
                                for(int j=0; j<dataArray2.length();j++){
                                    Log.d("source", dataArray2.getJSONObject(j).getString("source"));
                                    source = dataArray2.getJSONObject(j).getString("source");

}