Android - 通过HTTPS检索json

时间:2015-02-18 22:12:35

标签: android json facebook facebook-graph-api

我从facebook图api解析json时遇到问题 当我使用facebook URL时:
https://graph.facebook.com/interstacjapl/feed?access_token=MyTOKEN抓住json它不起作用,但当我复制json(它在浏览器中工作)并粘贴到我的网站http://mywebsite/fb.json并更改网站URL在代码中,它运作良好。
当我使用fb图形URL时,它显示错误:

W/System.err(5534): org.json.JSONException: No value for data

解析来自https或URL或代码是否有问题?

JSONParser.java

public class JSONParser {

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

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

MainActivity

public class MainActivity extends Activity {
    ListView list;
    TextView ver;
    TextView name;
    TextView api;
    Button Btngetdata;
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

    //URL to get JSON Array
    private static String url = "https://graph.facebook.com/interstacjapl/feed?access_token=CAACEdEose0cBANLR...";

    //JSON Node Names 
    private static final String TAG = "data";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "message";
    private static final String TAG_API = "type";

    JSONArray android = null;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        oslist = new ArrayList<HashMap<String, String>>();



        Btngetdata = (Button)findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                 new JSONParse().execute();


            }
        });


    }



    private class JSONParse extends AsyncTask<String, String, JSONObject> {
         private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
             ver = (TextView)findViewById(R.id.vers);
             name = (TextView)findViewById(R.id.name);
             api = (TextView)findViewById(R.id.api);
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();



        }

        @Override
        protected JSONObject doInBackground(String... args) {

            JSONParser jParser = new JSONParser();

            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }
         @Override
         protected void onPostExecute(JSONObject json) {
             pDialog.dismiss();
             try {
                    // Getting JSON Array from URL
                    android = json.getJSONArray(TAG);
                    for(int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);

                    // Storing  JSON item in a Variable
                    String ver = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String api = c.getString(TAG_API);




                    // Adding value HashMap key => value


                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_ID, ver);
                    map.put(TAG_NAME, name);
                    map.put(TAG_API, api);

                    oslist.add(map);
                    list=(ListView)findViewById(R.id.list);





                    ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                            R.layout.list_v,
                            new String[] { TAG_ID,TAG_NAME, TAG_API }, new int[] {
                                    R.id.vers,R.id.name, R.id.api});

                    list.setAdapter(adapter);
                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();

                        }
                    });

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


         }
    }

}

1 个答案:

答案 0 :(得分:1)

这有效

String reply = "";
        BufferedReader inStream = null; 
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpRequest = new HttpGet(url);

        try {

            HttpResponse response = httpClient.execute(httpRequest);
            inStream = new BufferedReader(
                new InputStreamReader(
                    response.getEntity().getContent()));

            StringBuffer buffer = new StringBuffer("");
            String line = "";

            while ((line = inStream.readLine()) != null) {
                buffer.append(line);
            }
            inStream.close();

            reply = buffer.toString();  

        } catch (Exception e) {
            //Handle Execptions
        }