JSONから個々の要素を取得するときに問題があります。以下のような: "T": "SBIN"誰かがこのJSON解析で私を助けることができますか?
次のようにJSON-URLは次のとおりです。http://finance.google.com/finance/info?q=BSE:SBIN
をし、次のように他のリソースは、次のとおりです。次のように
public class RefreshStock extends AsyncTask<String, String, List<StockModel>> {
@Override
protected List<StockModel> doInBackground(String... params) {
try {
url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
inputStream = connection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
stringBuffer = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null)
stringBuffer.append(line + "\n");
String data = stringBuffer.toString();
return parseJSON(data);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null)
connection.disconnect();
try {
if (bufferedReader != null)
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private List<StockModel> parseJSON(String json) {
List<StockModel> stockModelList = new ArrayList<>();
json = json.substring(3);
//textView.setText(""+json);
try {
StockModel stockModel = new StockModel();
JSONObject parentObject = new JSONObject();
JSONArray jsonArray = parentObject.getJSONArray(json);
JSONObject childObject = jsonArray.getJSONObject(0);
String stockId = childObject.getString("id");
String stockName = childObject.getString("t");
String stockRate = childObject.getString("l");
String stockChange = childObject.getString("c");
String stockChangePercent = childObject.getString("cp");
Toast.makeText(getApplicationContext(),""+stockRate, Toast.LENGTH_LONG).show();
stockModel.setId(stockId);
stockModel.setT(stockName);
stockModel.setL(stockRate);
stockModel.setC(stockChange);
stockModel.setCp(stockChangePercent);
stockModelList.add(stockModel);
return stockModelList;
} catch (JSONException e) {
e.printStackTrace();
}
return stockModelList;
}
protected void onPostExecute(List<StockModel> result) {
super.onPostExecute(result);
StockAdapter adapter = new StockAdapter(getApplicationContext(), R.layout.listrow, result);
lvStock.setAdapter(adapter);
}
}
logcatメッセージは次のとおりです。
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ org.json.JSONException: No value for [
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ {
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ "id": "13564339"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"t" : "SBIN"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"e" : "NSE"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"l" : "222.90"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"l_fix" : "222.90"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"l_cur" : "Rs.222.90"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"s": "0"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"ltt":"3:55PM GMT+5:30"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"lt" : "Jul 4, 3:55PM GMT+5:30"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"lt_dts" : "2016-07-04T15:55:45Z"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"c" : "+3.30"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"c_fix" : "3.30"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"cp" : "1.50"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"cp_fix" : "1.50"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"ccol" : "chg"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ,"pcls_fix" : "219.6"
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ }
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ ]
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ [ 07-04
10:49:42.667 11270:11321 W/System.err ]
at org.json.JSONObject.get(JSONObject.java:389)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:584)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at org.example.km.http2json.MainActivity$RefreshStock.parseJSON(MainActivity.java:117)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at org.example.km.http2json.MainActivity$RefreshStock.doInBackground(MainActivity.java:87)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at org.example.km.http2json.MainActivity$RefreshStock.doInBackground(MainActivity.java:63)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-04 10:49:42.667 11270-11321/org.example.km.http2json W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
ありがとう:P
データを取得するにはどうすればよいですか。私はあなたのGoogleファイナンスのリンクを試してみますが、それはエラー400を返します。そして、私は[google finance APIは今閉じています](https://developers.google.com/finance/)も読んでいます。 –
GOOGLE FINANCEサービスではなく廃止されたAPIです。上のリンクをクリックするだけです。 –
すごい男..お願いします。いいえ叫んではいけません。 –