2012-09-06 62 views
4

我正在尝试新建一个应用程序,并希望获得股票更新,货币兑换更新等。请问您可以建议一些免费API以获取这些更新吗?我已经尝试过:适用于Android的财务API

1)Google财经API不使用的原因:不推荐使用,并很快关闭,这会使我的应用程序过时。

2)Yahoo Finance API不使用的原因:没有适当的文件。原因是雅虎实际上没有财务API。看起来有些人已经反向设计了一个用来提取财务数据的API,但它们正在打破雅虎的服务条款。我不想这样做。 (从http://developer.yahoo.com/forum/General-Discussion-at-YDN/Using-Yahoo-Finance-API-Not-RSS-/1250246255000-0b82f8f0-7f48-3af2-8fe2-e73a138cbfaa读取)

如果提供了一些资源或代码的链接,那将会很棒。我在这里先向您的帮助表示感谢。

+0

如果我是你,我会尝试找出它这样做是基于REST的Web服务。 –

+0

甚至看起来。大多数谷歌提供的链接指向雅虎财经 – harshit

+1

如果他们有一个类似API的REST,那么你应该使用它,我会惊讶,如果它是对他们的服务条款。解析HTML的Java代码,这不是一回事。 –

回答

3

立即使用yql。

查询:"http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D" + from + to + "%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate"

代码片段:

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; 
} 
+1

请告诉我你在这个URL中“from”和“to”是什么意思.... – Srinivasan