Android使用Google Finance API的网络服务

发布于 2024-12-10 16:55:14 字数 94 浏览 0 评论 0原文

我是 android 的新手,我真的很困惑如何通过 android 进行谷歌金融 api。谁能给我推荐一些关于它的好文章?以及如何通过 android 使用网络服务。 谢谢

I am a newbie to android, and i am really confused as to how to proceed for google finance api through android. Can anyone suggest me some good articles about it? and also how to use web services through android.
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2024-12-17 16:55:14

金融 API 网址:“http://www.gummy-stuff.org/Yahoo-data。嗯

 private static String convertStreamToString(InputStream is) {
/*
 * To convert the InputStream to String we use the
 * BufferedReader.readLine() method. We iterate until the BufferedReader
 * return null which means there's no more data to read. Each line will
 * appended to a StringBuilder and returned as String.
 */
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
return sb.toString();
}

/*
 * This is a test function which will connects to a given rest service and
* prints it's response to Android Log with labels "Praeda".
 */
public static String[] connect(String url) {
HttpClient httpclient = new DefaultHttpClient();
String[] str = new String[5];

// Prepare a request object
HttpGet httpget = new HttpGet(url);

// Execute the request
HttpResponse response;
try {
    response = httpclient.execute(httpget);
    // Examine the response status
    Log.d(TAG, response.getStatusLine().toString());

    // Get hold of the response entity
    HttpEntity entity = response.getEntity();
    // If the response does not enclose an entity, there is no need
    // to worry about connection release

    if (entity != null) {

        // A Simple JSON Response Read
        InputStream instream = entity.getContent();
        String result = convertStreamToString(instream);
        result=result.replace("parseExchangeRate(", "").replace(");", "");
        Log.d(TAG, result);

        // A Simple JSONObject Creation
        JSONObject json = new JSONObject(result);
        Log.d(TAG, "<jsonobject>\n" + json.toString()
                + "\n</jsonobject>");

        // A Simple JSONObject Parsing
        // JSONArray nameArray=json.names();
        // Log.i("query",nameArray.toString());
        // JSONObject query=json.getJSONObject("query");
        // Log.i("query",query.toString());
        // JSONArray results=query.getJSONArray("results");
        // Log.i("rslts",results.toString());
        // JSONArray quote=results.getJSONArray("quote");
        // JSONObject quote=results.getJSONObject("quote");
        // Log.i("quote",quote.toString());
        JSONObject query = json.getJSONObject("query");
        Log.d(TAG, query.toString());
        JSONObject results = query.getJSONObject("results");
        Log.d(TAG, results.toString());
        JSONObject quote = results.getJSONObject("row");
        Log.d(TAG, quote.toString());
        for (int i = 0; i < quote.length(); i++) {

            // Log.i("Praedafor","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"
            // +"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
            // JSONObject quotes = results.getJSONObject(i)
            // .getJSONObject("quote");
            // Log,i
            // Log.i("name",quote.getString("Name"));
            // Log.i("name","pahunch");
            // Log.i("name",quote.getString("Symbol"));

            // Log.i("name",quote.getString("DaysLow"));

            // Log.i("name",quote.getString("DaysHigh"));

            // Log.i("name",quote.getString("Open"));

            // Log.i("name",quote.getString("PreviousClose"));
            String symbol = quote.getString("rate");
            str[0] = symbol;
            String dayslow = quote.getString("DaysLow");
            str[1]=dayslow;
            // tv1.setText(quote.getString("DaysLow"));
            str[2]= quote.getString("DaysHigh");
            str[3]= quote.getString("Open");
            str[4]= quote.getString("Change");
        }

        // A Simple JSONObject Value Pushing
        // json.put("execution-start-time", "sample value");
        Log.d(TAG, "<jsonobject>\n" + json.toString()
                + "\n</jsonobject>");
        // Log.i("Praeda12",json.get("").toString());

        // Closing the input stream will trigger connection release
        instream.close();
    }

} catch (ClientProtocolException e) {
    Log.d(TAG, "ClientProtocolException");
    e.printStackTrace();
} catch (IOException e) {
    Log.d(TAG, "IOException " + e.getMessage());
    e.printStackTrace();
} catch (JSONException e) {
    Log.d(TAG, "JSONException " + e.getMessage());
    e.getMessage();
}
return str;
}

finance api url: "http://www.gummy-stuff.org/Yahoo-data.htm"

 private static String convertStreamToString(InputStream is) {
/*
 * To convert the InputStream to String we use the
 * BufferedReader.readLine() method. We iterate until the BufferedReader
 * return null which means there's no more data to read. Each line will
 * appended to a StringBuilder and returned as String.
 */
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
return sb.toString();
}

/*
 * This is a test function which will connects to a given rest service and
* prints it's response to Android Log with labels "Praeda".
 */
public static String[] connect(String url) {
HttpClient httpclient = new DefaultHttpClient();
String[] str = new String[5];

// Prepare a request object
HttpGet httpget = new HttpGet(url);

// Execute the request
HttpResponse response;
try {
    response = httpclient.execute(httpget);
    // Examine the response status
    Log.d(TAG, response.getStatusLine().toString());

    // Get hold of the response entity
    HttpEntity entity = response.getEntity();
    // If the response does not enclose an entity, there is no need
    // to worry about connection release

    if (entity != null) {

        // A Simple JSON Response Read
        InputStream instream = entity.getContent();
        String result = convertStreamToString(instream);
        result=result.replace("parseExchangeRate(", "").replace(");", "");
        Log.d(TAG, result);

        // A Simple JSONObject Creation
        JSONObject json = new JSONObject(result);
        Log.d(TAG, "<jsonobject>\n" + json.toString()
                + "\n</jsonobject>");

        // A Simple JSONObject Parsing
        // JSONArray nameArray=json.names();
        // Log.i("query",nameArray.toString());
        // JSONObject query=json.getJSONObject("query");
        // Log.i("query",query.toString());
        // JSONArray results=query.getJSONArray("results");
        // Log.i("rslts",results.toString());
        // JSONArray quote=results.getJSONArray("quote");
        // JSONObject quote=results.getJSONObject("quote");
        // Log.i("quote",quote.toString());
        JSONObject query = json.getJSONObject("query");
        Log.d(TAG, query.toString());
        JSONObject results = query.getJSONObject("results");
        Log.d(TAG, results.toString());
        JSONObject quote = results.getJSONObject("row");
        Log.d(TAG, quote.toString());
        for (int i = 0; i < quote.length(); i++) {

            // Log.i("Praedafor","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"
            // +"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
            // JSONObject quotes = results.getJSONObject(i)
            // .getJSONObject("quote");
            // Log,i
            // Log.i("name",quote.getString("Name"));
            // Log.i("name","pahunch");
            // Log.i("name",quote.getString("Symbol"));

            // Log.i("name",quote.getString("DaysLow"));

            // Log.i("name",quote.getString("DaysHigh"));

            // Log.i("name",quote.getString("Open"));

            // Log.i("name",quote.getString("PreviousClose"));
            String symbol = quote.getString("rate");
            str[0] = symbol;
            String dayslow = quote.getString("DaysLow");
            str[1]=dayslow;
            // tv1.setText(quote.getString("DaysLow"));
            str[2]= quote.getString("DaysHigh");
            str[3]= quote.getString("Open");
            str[4]= quote.getString("Change");
        }

        // A Simple JSONObject Value Pushing
        // json.put("execution-start-time", "sample value");
        Log.d(TAG, "<jsonobject>\n" + json.toString()
                + "\n</jsonobject>");
        // Log.i("Praeda12",json.get("").toString());

        // Closing the input stream will trigger connection release
        instream.close();
    }

} catch (ClientProtocolException e) {
    Log.d(TAG, "ClientProtocolException");
    e.printStackTrace();
} catch (IOException e) {
    Log.d(TAG, "IOException " + e.getMessage());
    e.printStackTrace();
} catch (JSONException e) {
    Log.d(TAG, "JSONException " + e.getMessage());
    e.getMessage();
}
return str;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文