API调用在几个月后突然停止工作

时间:2017-06-23 08:46:18

标签: android json

我正在使用以下代码来" GET"使用alpha Vantage API的数据。它工作了大约2个月然后它突然停止工作。我没有看到JSON数据发生任何变化。

private int retrievefromnet(int count,String symbol)
        {
            String temp;
            String baseAddress = "http://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY";
            String apiKey = "J63P";
            Uri Url = Uri.parse(baseAddress)
                    .buildUpon()
                    .appendQueryParameter("symbol", symbol)
                    .appendQueryParameter("interval", "1min")
                    .appendQueryParameter("apikey", apiKey)
                    .build();
            Log.d("createList","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) {
                    return 0;
                }
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                Log.d("createList","here");
                while ((temp = reader.readLine()) != null) {
                    DATA[count] += temp;
                }
                Log.d("unedited", DATA[count]);

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

从JSON中提取数据。

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

                JSONObject jsonObject = new JSONObject(DATA[count]);
                jsonObject = jsonObject.getJSONObject(List);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis() - 34200000 );
                String currtime = sdf.format(calendar.getTime());
                Log.d("currtime",currtime);
                jsonObject = jsonObject.getJSONObject(currtime);

                return jsonObject.getString("4. close");

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

日志说:

D/createList: built URL: http://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=J63P
D/libc: [NET] android_getaddrinfofornetcontext+,hn 19(0x7777772e616c70),sn(),hints(known),family 0,flags 1024, D/libc: [NET] android_getaddrinfo_proxy get netid:0
D/libc: [NET] android_getaddrinfo_proxy-, success
D/createList: here
E/createList: Error in json

请注意,retreivefromnet中try块的最后一个日志语句未显示,也不是catch块的日志。 DATA []是一个字符串数组。它已经初步化了。另外,我检查reader是否为空。

帮我找到问题。

为Abhishek编辑:

06-23 14:25:37.315 16817-16980/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:449)
                                                                       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$PlaceholderFragment$createList.workonJson(MainActivity.java:596)
                                                                       at lcukerd.com.stocknotifier.MainActivity$PlaceholderFragment$createList.doInBackground(MainActivity.java:404)
                                                                       at lcukerd.com.stocknotifier.MainActivity$PlaceholderFragment$createList.doInBackground(MainActivity.java:376)
                                                                       at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                       at java.lang.Thread.run(Thread.java:818)

4 个答案:

答案 0 :(得分:1)

您遇到了JSON解析问题。最好使用GSON库来解析JSON。

EX:

Gson gson = new Gson();
YOUR_OBJECT response = gson.fromJson("your_json_string", YOUR_OBJECT.class);

您可以使用在线工具从json创建java对象。 EX:http://www.jsonschema2pojo.org/

尝试这个:

public int retrievefromnet(int count, String symbol) {
        String temp;
        String baseAddress = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY";
        String apiKey = "J63P";
        Uri Url = Uri.parse(baseAddress)
                .buildUpon()
                .appendQueryParameter("symbol", symbol)
                .appendQueryParameter("interval", "1min")
                .appendQueryParameter("apikey", apiKey)
                .build();
        try {
            URL url = new URL(Url.toString());
            HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection();
            urlconnection.setRequestMethod("GET");
            urlconnection.connect();
            InputStream inputStream = urlconnection.getInputStream();
            if (inputStream == null) {
                return 0;
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            Log.d("createList", "here");
            String responseStr = "";
            while ((temp = reader.readLine()) != null) {
                responseStr += temp;
            }
            Log.d("unedited", responseStr);

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

答案 1 :(得分:0)

要查找问题,请在catch块中使用此代码e.printStackTrace();,并与Exception对象的堆栈跟踪共享日志

答案 2 :(得分:0)

似乎json数据的内容格式已经改变,你有没有检查json内容,它已被修改。你能比较一下之前处理好的json吗?

抱歉我的英文

答案 3 :(得分:0)

很简单。我只需要用HTTPS替换HTTP。 AlphaVantage可能已经改变了一些原因,这就是它早期工作但现在不工作的原因。现在一切正常。