无法从Android

时间:2017-04-24 16:43:58

标签: android bufferedreader

我正在尝试使用API​​获取数据。我在bufferedreader中获取包含多行的数据。但是我无法在String中提取它。我知道bufferedreader有数据,我已经通过使用多个.readLine();显示它进行检查,但循环不起作用。

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

和Log是:

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)

另外,请注意&#34; Log&#34;内部while循环后的语句也不起作用。 请帮我解决一下。

1 个答案:

答案 0 :(得分: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;