当我尝试解析整个数组时,它可以工作但是当我尝试从URL解析数组元素(如ID,t,l等)时,代码不起作用。
请详细解释,因为我是新手:)
PS:我相信字符串形式的整个数据。喜欢:" l" :" 219.20"。网址如下: http://finance.google.com/finance/info?q=BSE:SBIN
其他资源如下:
MainActivity.java
public class MainActivity extends AppCompatActivity {
public String theUrl;
public String stockName;
ListView lvStock;
HttpURLConnection connection;
URL url;
InputStream inputStream;
BufferedReader bufferedReader;
StringBuffer stringBuffer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvStock = (ListView) findViewById(R.id.lvStocks);
stockName = "SBIN";
theUrl = "http://finance.google.com/finance/info?q=BSE:" + stockName;
Toast.makeText(MainActivity.this, "You have selected : " + stockName, Toast.LENGTH_SHORT).show();
}
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<>();
//Using it to remove the two forward slashes in the json online.
json = json.substring(2);
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");
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);
//Toast.makeText(getApplicationContext(), new StringBuilder().append("").append(result.toString()).toString(), Toast.LENGTH_LONG).show();
StockAdapter adapter = new StockAdapter(getApplicationContext(), R.layout.listrow, result);
lvStock.setAdapter(adapter);
}
}
public class StockAdapter extends ArrayAdapter {
private List<StockModel> stockModelList;
private int resource;
private LayoutInflater inflater;
private StockAdapter(Context context, int resource, List<StockModel> objects) {
super(context, resource, objects);
stockModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listrow, null);
holder.stockID = (TextView) convertView.findViewById(R.id.txtStockID);
holder.stockName = (TextView) convertView.findViewById(R.id.txtStockName);
holder.stockRate = (TextView) convertView.findViewById(R.id.txtStockRate);
holder.stockChange = (TextView) convertView.findViewById(R.id.txtStockChange);
holder.stockChangePercent = (TextView) convertView.findViewById(R.id.txtStockChangePercent);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.stockID.setText(stockModelList.get(position).getId());
holder.stockName.setText(stockModelList.get(position).getT());
holder.stockRate.setText(stockModelList.get(position).getL());
holder.stockChange.setText(stockModelList.get(position).getC());
holder.stockChangePercent.setText(stockModelList.get(position).getCp());
return convertView;
}
}
private class ViewHolder {
private TextView stockRate;
private TextView stockChangePercent;
private TextView stockChange;
private TextView stockName;
private TextView stockID;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refresh) {
new RefreshStock().execute(theUrl);
return true;
}
return super.onOptionsItemSelected(item);
}
}
,另一个类如下:
package org.example.km.http2json.Models;
public class StockModel {
private String id;
private String t;
//private String e;
private String l;
//private String l_fix;
//private String l_cur;
//private String s;
//private String ltt;
//private String lt;
//private String llt_dts;
private String c;
//private String c_fix;
private String cp;
//private String cp_fix;
//private String ccol;
//private String pcls_fix;
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getT(){
return t;
}
public void setT(String t){
this.t = t;
}
public String getL(){
return l;
}
public void setL(String l){
this.l = l;
}
public String getC(){
return c;
}
public void setC(String c){
this.c = c;
}
public String getCp(){
return cp;
}
public void setCp(String cp){
this.cp = cp;
}
}
/*
// [
{
"id": "13564339"
,"t" : "SBIN"
,"e" : "NSE"
,"l" : "219.20" // Last Value
,"l_fix" : "219.20"
,"l_cur" : "Rs.219.20"
,"s": "0"
,"ltt":"3:56PM GMT+5:30"
,"lt" : "Jul 1, 3:56PM GMT+5:30"
,"lt_dts" : "2016-07-01T15:56:11Z"
,"c" : "+0.40" //Change
,"c_fix" : "0.40"
,"cp" : "0.18" //Change Percent
,"cp_fix" : "0.18"
,"ccol" : "chg"
,"pcls_fix" : "218.8" // Previous Closing
}
]
*/
答案 0 :(得分:1)
// there is no data your parsing.. please check
JSONArray jsonArray = parentObject.getJSONArray("");
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");
注意:下面的数据会抛出解析器异常,删除前两个前导字符
// [ { "id": "13564339" ,"t" : "SBIN" ,"e" : "NSE" ,"l" : "219.20" ,"l_fix" : "219.20" ,"l_cur" : "Rs.219.20" ,"s": "0" ,"ltt":"3:56PM GMT+5:30" ,"lt" : "Jul 1, 3:56PM GMT+5:30" ,"lt_dts" : "2016-07-01T15:56:11Z" ,"c" : "+0.40" ,"c_fix" : "0.40" ,"cp" : "0.18" ,"cp_fix" : "0.18" ,"ccol" : "chg" ,"pcls_fix" : "218.8" } ]
答案 1 :(得分:0)
我更新了 RefreshStock 文件
@NSManaged