0

そのようなFacebookのURLでjsonからデータを取得するアプリを開発します: https://www.facebook.com/feeds/page.php?id=105310924507&format=json

問題は、データが問題なく正常に取得される場合と、次のような悪いメッセージが表示される場合があることです: 残念ながら、アプリは停止しました!

私は何の問題もありませんが、私はたくさん試します

これは私のコードです:

JSON 構成

package com.engahmedphp.facebookcollector;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

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();
////            HttpFeed httpFeed = new HttpFeed(url);
//            
//            HttpGet httpGet = new HttpGet(url);
// 
//            HttpResponse httpResponse = httpClient.execute(httpGet);
//            HttpEntity httpEntity = httpResponse.getEntity();
//            is = httpEntity.getContent();           


             HttpClient httpClient = new DefaultHttpClient();
            // HttpFeed httpFeed = new HttpFeed(url);

             HttpUriRequest httpGet = new HttpGet(url);
             HttpResponse httpResponse = null;
             try {
             httpResponse = httpClient.execute(httpGet);


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


        try {
             HttpEntity httpEntity = httpResponse.getEntity();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(
//                    is, "UTF-8"), 8);
//            StringBuilder sb = new StringBuilder();
//            String line = null;
//            while ((line = reader.readLine()) != null) {
//                sb.append(line + "\n");
//            }
//            is.close();
            json = EntityUtils.toString(httpEntity);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 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;

    }
}

データを取得するメソッド

public void getNewFeeds() {

        String url = "https://www.facebook.com/feeds/page.php?id=" + fpageid
                + "&format=json";
        try {
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();
    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);


        // Getting Array of Contacts
        entries = json.getJSONArray("entries");

        // looping through All Contacts
        for (int i = entries.length() - 1; i >= 0; i--) {

            JSONObject e = entries.getJSONObject(i);

            if (!dbPages.checkFeed(e.getString("id"))) {
                // Storing each json item in variable
                String fid = e.getString("id");

                String title = Html.fromHtml(e.getString("title"))
                        .toString();
                String content = e
                        .getString("content")
                        .replaceAll(
                                "<img[^>]*?src\\s*=\\s*['\"]([^'\"]*?)['\"][^>]*?/?>",
                                "").toString();
                // String content = e.getString("content").toString();
                //
                // Document doc = Jsoup.parse(content);
                // Element imgs = doc.select("img").first();
                // imgs.outerHtml();
                //
                // String src = img.attr("src");
                Pattern p = Pattern
                        .compile("<img[^>]*?src\\s*=\\s*['\"]([^'\"]*?)['\"][^>]*?/?>");
                Matcher m = p.matcher(e.getString("content"));

                String img = "";
                if (m.find()) {                     
                    img = m.group(0);
                }

                dbPages.addFeed(fid, fpageid, title, content, img);
            }

        }
        feedsCount = entries.length();

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

}

黄色の丸太猫

09-25 08:19:08.859: W/System.err(19351): org.apache.http.conn.HttpHostConnectException: Connection to https://www.facebook.com refused
09-25 08:19:08.874: W/System.err(19351):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
09-25 08:19:08.874: W/System.err(19351):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-25 08:19:08.874: W/System.err(19351):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-25 08:19:08.874: W/System.err(19351):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-25 08:19:08.882: W/System.err(19351):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-25 08:19:08.882: W/System.err(19351):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-25 08:19:08.882: W/System.err(19351):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-25 08:19:08.882: W/System.err(19351):    at com.engahmedphp.facebookcollector.JSONParser.getJSONFromUrl(JSONParser.java:57)
09-25 08:19:08.890: W/System.err(19351):    at com.engahmedphp.facebookcollector.GetFeedsService.getNewFeeds(GetFeedsService.java:217)
09-25 08:19:08.890: W/System.err(19351):    at com.engahmedphp.facebookcollector.GetFeedsService.onHandleIntent(GetFeedsService.java:96)
09-25 08:19:08.890: W/System.err(19351):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
09-25 08:19:08.890: W/System.err(19351):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 08:19:08.890: W/System.err(19351):    at android.os.Looper.loop(Looper.java:137)
09-25 08:19:08.898: W/System.err(19351):    at android.os.HandlerThread.run(HandlerThread.java:60)
09-25 08:19:08.898: W/System.err(19351): Caused by: java.net.ConnectException: failed to connect to /31.13.86.16 (port 443): connect failed: ETIMEDOUT (Connection timed out)
09-25 08:19:08.906: W/System.err(19351):    at libcore.io.IoBridge.connect(IoBridge.java:114)
09-25 08:19:08.906: W/System.err(19351):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-25 08:19:08.906: W/System.err(19351):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-25 08:19:08.906: W/System.err(19351):    at java.net.Socket.connect(Socket.java:842)

丸太の赤

09-25 08:19:08.937: E/SQLiteDatabase(19351): close() was never explicitly called on database '/data/data/com.engahmedphp.facebook/databases/facebook' 
09-25 08:19:08.937: E/SQLiteDatabase(19351): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:2063)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1117)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1074)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1161)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:826)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at com.engahmedphp.facebook.DAOPages.open(DAOPages.java:57)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at com.engahmedphp.facebook.GetFeedsService.onStartCommand(GetFeedsService.java:200)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2373)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.app.ActivityThread.access$1900(ActivityThread.java:128)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.os.Looper.loop(Looper.java:137)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at android.app.ActivityThread.main(ActivityThread.java:4514)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at java.lang.reflect.Method.invokeNative(Native Method)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at java.lang.reflect.Method.invoke(Method.java:511)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-25 08:19:08.937: E/SQLiteDatabase(19351):    at dalvik.system.NativeStart.main(Native Method)
09-25 08:19:08.937: E/AndroidRuntime(19351): FATAL EXCEPTION: IntentService[getFeedsService]
09-25 08:19:08.937: E/AndroidRuntime(19351): java.lang.NullPointerException
09-25 08:19:08.937: E/AndroidRuntime(19351):    at com.engahmedphp.facebook.JSONParser.getJSONFromUrl(JSONParser.java:70)
09-25 08:19:08.937: E/AndroidRuntime(19351):    at com.engahmedphp.facebook.GetFeedsService.getNewFeeds(GetFeedsService.java:217)
09-25 08:19:08.937: E/AndroidRuntime(19351):    at com.engahmedphp.facebook.GetFeedsService.onHandleIntent(GetFeedsService.java:96)
09-25 08:19:08.937: E/AndroidRuntime(19351):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
09-25 08:19:08.937: E/AndroidRuntime(19351):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 08:19:08.937: E/AndroidRuntime(19351):    at android.os.Looper.loop(Looper.java:137)
09-25 08:19:08.937: E/AndroidRuntime(19351):    at android.os.HandlerThread.run(HandlerThread.java:60)

前もって感謝します

4

1 に答える 1

0

ホストに http を使用するようにホストを変更します

http://www.facebook.com 

これの代わりに

 https://www.facebook.com 

そして2番目のものでは、カーソルを閉じていません。最初にカーソルを適切に閉じます。

また、リクエスト時にサーバー (/31.13.86.16 (ポート 443)) がオンライン状態であることを確認してください。

于 2013-09-25T06:32:52.847 に答える