3

私が必要とする情報を発行するJavaScriptを提供するWebサイトがあります.Android APIを介して、このJavaScriptを使用して情報を発行し、それを解析する方法はありますか?

これはJavaScriptです:

<script type="text/javascript" src="http://pulllist.comixology.com/js/pulllist/5b467b28e73595311e02fe32c3081e4a.js?date=2013-05-15"></script>

HTML ファイルで実行すると、Web サイトから取得した画像とテキストが表示されることがわかります。そのすべての情報を Android アプリで取得したいと考えています。

4

2 に答える 2

1

表示するだけの場合は、WebViewカスタム スタイルで を使用できます。リンク: http://developer.android.com/reference/android/webkit/WebView.html

String html = "<script type='text/javascript' src='http://pulllist.comixology.com/js/pulllist/5b467b28e73595311e02fe32c3081e4a.js?date=2013-05-15'></script>";
String mime = "text/html";
String encoding = "utf-8";

WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

アップデート

そのJavaScriptファイルからデータを抽出する方法の簡単で汚い例を書きました:

import java.util.*;
import java.lang.*;
import java.net.*;
import java.io.*;

public class JavascriptToUsableData {

    // Method to get a string from an URL
    // Thanks to http://stackoverflow.com/a/4328733/1226267
    public static String getText(String url) throws Exception {
        URL website = new URL(url);
        URLConnection connection = website.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                    connection.getInputStream()));

        StringBuilder response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            response.append(inputLine);

        in.close();

        return response.toString();
    }

    public static void main (String args[]) throws Exception {

        // Get the data
        String source = JavascriptToUsableData.getText("http://pulllist.comixology.com/js/pulllist/5b467b28e73595311e02fe32c3081e4a.js?date=2013-05-15");

        // The index of the item
        int i = 0;

        // We search the data-string from the start
        int position1 = 0;

        // While there are items found, continue
        while(true) {

            // Look for the pattern a[index]={json data};
            String lookFor1 = "a[" + String.valueOf(i) + "]={";
            String lookFor2 = "};";
            position1  = source.indexOf(lookFor1, position1+1);

            // Check if we have a match
            if(position1 > -1) {

                // Find the end of the match
                int position2 = source.indexOf(lookFor2, position1);

                // Get the result
                String result = source.substring(position1 + lookFor1.length() - 1, position2 + 1);

                // Increase the index an check if we can find a next item
                i++;

                // Print out this row, which is a JSON representation of the data you want
                System.out.println(result);

                // I'm not in an Android environment right now, but there is a JSON reader you can use in Android
                // I think it works somthing like this:
                // resultObj = new JSONObject(result);

                // And then access the data like this:
                // resultObj.getString("title");
                // resultObj.getString("guid");
                // resultObj.getString("img");
                // etc

            } else {
                // We haven't found a match, break out of while loop
                break;
            }
        }
    }
}

多くの最適化が可能であり、現在の状態ではおそらくフェイルセーフではありませんが、開始方法に関するヒントが得られる可能性があります。

于 2013-05-16T12:25:51.310 に答える