使用android studio下载和解析JSON问题
我是android studio的新手,我想知道我做错了什么。我正在使用alpha vantage api获取个人项目的股票数据。问题是,当我创建http请求时,我只下载了大约111天的数据;然而,当我使用我提供代码的链接时,网页会填充更多的天数(多年)的数据。我询问我请求的方式有问题吗 这是我的任务,我叫它AsyncTask使用android studio下载和解析JSON问题,android,json,http,stock,Android,Json,Http,Stock,我是android studio的新手,我想知道我做错了什么。我正在使用alpha vantage api获取个人项目的股票数据。问题是,当我创建http请求时,我只下载了大约111天的数据;然而,当我使用我提供代码的链接时,网页会填充更多的天数(多年)的数据。我询问我请求的方式有问题吗 这是我的任务,我叫它AsyncTask public class JsonResponseTask extends AsyncTask<String,String,String> {
public class JsonResponseTask extends AsyncTask<String,String,String> {
protected void onPreExecute(){
super.onPreExecute();
}
protected String doInBackground(String... params){
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
InputStream inputStream = null;
StringBuffer stringBuffer = null;
try{
URL url = new URL(params[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
if(httpURLConnection.getResponseCode() == 200) {
inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
stringBuffer = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + "\n");
Log.d("Response: ", "> " + line); //a lot of logs
}
}
else{
Log.e("mainActivity", "Error response: " + httpURLConnection.getResponseCode());
}
return stringBuffer.toString();
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally{
parseJson(stringBuffer.toString(), params[1]);
}
return null;
}
公共类JsonResponseTask扩展异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
}
受保护的字符串doInBackground(字符串…参数){
HttpURLConnection HttpURLConnection=null;
BufferedReader BufferedReader=null;
InputStream InputStream=null;
StringBuffer-StringBuffer=null;
试试{
URL=新URL(参数[0]);
httpURLConnection=(httpURLConnection)url.openConnection();
httpURLConnection.connect();
如果(httpURLConnection.getResponseCode()==200){
inputStream=httpURLConnection.getInputStream();
bufferedReader=新的bufferedReader(新的InputStreamReader(inputStream));
stringBuffer=新的stringBuffer();
字符串行=”;
而((line=bufferedReader.readLine())!=null){
stringBuffer.append(行+“\n”);
Log.d(“响应:”,“>”+行);//大量日志
}
}
否则{
Log.e(“mainActivity”,“错误响应:+httpURLConnection.getResponseCode());
}
返回stringBuffer.toString();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE){
e、 printStackTrace();
}最后{
parseJson(stringBuffer.toString(),参数[1]);
}
返回null;
}
下面是它在字符串缓冲区完成后调用的parseJson方法
static List<String> parseJson(String response, String FeatureType){
String currFeature = null;
ArrayList<String> ListofIndicators = new ArrayList<>();
//this the day iterator it will loop through the weekdays to pull technical indicators specified in json
//
Calendar calander = Calendar.getInstance();
String date = null;
try{
JSONObject jsonResponse = new JSONObject(response);
JSONObject jsonResults = jsonResponse.getJSONObject("Technical Analysis: " + FeatureType);
//I know that there is a large amount of info but I only want to go back to till.... like 2002
//so I'll only make a limited number of iterations
for (int i = 0; i < jsonResults.length(); i++) {
JSONObject currFeatureTimeStep = jsonResults;
while(calander.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calander.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
//go back one day
calander.add(Calendar.DATE, -1);
}
//generate date string
date = String.valueOf(calander.get(Calendar.YEAR)) + "-" + String.valueOf(calander.get(Calendar.MONTH)) + "-" + String.valueOf(calander.get(Calendar.DAY_OF_MONTH));
currFeature = currFeatureTimeStep.getString(date);
calander.add(Calendar.DATE, -1);
ListofIndicators.add(currFeature);
}
} catch (JSONException e){
Log.e("MainActivity", "Error parsing JSON response", e);
}
return ListofIndicators;
}
静态列表解析JSON(字符串响应,字符串特征类型){
字符串currFeature=null;
ArrayList ListofIndicators=新的ArrayList();
//这是迭代器,它将在工作日循环以提取json中指定的技术指标
//
C