2017-04-24 80 views
1

我想通過使用API​​獲取數據。我在包含多行的bufferedreader中獲取數據。但是我無法在String中提取它。我知道bufferedreader有數據,我通過使用多個.readLine();來顯示它,但循環不起作用。無法從緩衝讀取器讀取android

protected String[][] doInBackground(Cursor[] cursors) 
    { 
     String list[][] = new String[cursors[0].getCount()][2]; 
     //while(cursors[0].moveToNext()) 
     { 
      String DATA="",temp; 
      String baseAddress="http://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY"; 
      String apiKey="J63P"; 
      Uri Url = Uri.parse(baseAddress) 
        .buildUpon() 
        .appendQueryParameter("symbol","SBIN") 
        .appendQueryParameter("interval","1min") 
        .appendQueryParameter("apikey",apiKey) 
        .build(); 
      Log.d("built URL",Url.toString()); 
      try 
      { 
       url= new URL(Url.toString()); 
       urlconnection= (HttpURLConnection) url.openConnection(); 
       urlconnection.setRequestMethod("GET"); 
       urlconnection.connect(); 

       InputStream inputStream = urlconnection.getInputStream(); 
       if (inputStream==null) 
       { 
        Log.d("inputstream","empty"); 
        return null; 
       } 
       BufferedReader reader= new BufferedReader(new InputStreamReader(inputStream)); 

       while((temp = reader.readLine())!=null) 
       { 
        DATA.concat(temp); 
       } 
       Log.d("unedited",DATA); 


      } 
      catch(IOException e) 
      { 
       Log.e("createList", "Error in url ", e); 
       return null; 
      } 


      try { 
       String List = "Time Series (1min)"; 

       JSONObject full = new JSONObject(DATA); 
       JSONArray permin = full.getJSONArray(List); 

       for (int i=0;i<permin.length();i++) 
       { 
        String open,high,low,close; 
        JSONObject current = permin.getJSONObject(i); 

        open = current.getString("1. open"); 
        high = current.getString("2. high"); 
        low = current.getString("3. low"); 
        close = current.getString("4. close"); 
        Log.d("Extracted ",open + " " + high + " " + low + " " + close +"\n"); 
       } 

      } 
      catch (JSONException e) 
      { 
       Log.e("createList","Error in json",e); 
      } 

     } 
     return list; 
    } 

和日誌是:

04-24 14:34:52.009 3574-3619/lcukerd.com.stocknotifier D/built URL: http://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=SBIN&interval=1min&apikey=J63P 
04-24 14:34:53.723 3574-3619/lcukerd.com.stocknotifier E/createList: Error in json 
                     org.json.JSONException: End of input at character 0 of 
                      at org.json.JSONTokener.syntaxError(JSONTokener.java:450) 
                      at org.json.JSONTokener.nextValue(JSONTokener.java:97) 
                      at org.json.JSONObject.<init>(JSONObject.java:156) 
                      at org.json.JSONObject.<init>(JSONObject.java:173) 
                      at lcukerd.com.stocknotifier.MainActivity$createList$override.doInBackground(MainActivity.java:123) 
                      at lcukerd.com.stocknotifier.MainActivity$createList$override.access$dispatch(MainActivity.java) 
                      at lcukerd.com.stocknotifier.MainActivity$createList.doInBackground(MainActivity.java:0) 
                      at lcukerd.com.stocknotifier.MainActivity$createList.doInBackground(MainActivity.java:69) 
                      at android.os.AsyncTask$2.call(AsyncTask.java:288) 
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                      at java.lang.Thread.run(Thread.java:818) 

另外請注意, 「日誌」 語句後內循環的同時,也不能正常工作。 請幫我解決它。

回答

0

你進來的JSON數據例外,它是從日誌很清楚,它說:

04-24 14:34:53.723 3574-3619/lcukerd.com.stocknotifier E/createList: 錯誤JSON

噢:

改變這一行:

DATA.concat(temp); 

data = DATA.concat(temp); 

而且在Java中,最好給它這樣寫:

data += temp; 
+0

在JSON的錯誤是因爲字符串數據是空的。 – Lcukerd

+0

@Lukkerd這是因爲你沒有填充數據,請檢查我的編輯 –