所以我使用asynctask来访问在线托管的json文件。我能够在我的Android应用程序中显示json。现在我想显示json文件中的某些值。下面是我用过的json文件。
https://feeds.citibikenyc.com/stations/stations.json
例如,假设我想只显示此json中的id。我怎么能这样做?
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()){
pd.dismiss();
}
txtJson.setText(result);
}
这里我在'result'中得到了完整的json值
PS:完成asynctask
private class JsonTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(GetJson.this);
pd.setMessage("Please wait");
pd.setCancelable(false);
pd.show();
}
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null; //BufferedReader reads text from the input stream
try {
URL url = new URL(params[0]); // params[0] is the first value in String..params (String..params can
//have any no.of string parameters )
connection = (HttpURLConnection) url.openConnection();
connection.connect(); //not necessary.works without this.
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer(); // A string buffer is like a String, but can be modified
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
//Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
}
//Log.d("BufferTest",buffer.toString());
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) { //Here the 'result' is whatever that is returned from the
//doinbackground method (Ex: buffer.toString(); )
try {
JSONObject jsonobject = new JSONObject(result);
JSONArray jsonarray = jsonobject.getJSONArray("stationBeanList");
String id = "";
for(int i =0; i<jsonarray.length();i++){
JSONObject jsonObject2 = jsonarray.getJSONObject(i);
id = jsonObject2.getString("stationName");
txtJson.setText(id);
}
} catch (JSONException e) {
e.printStackTrace();
}
super.onPostExecute(result);
if (pd.isShowing()){
pd.dismiss();
}
//txtJson.setText(result);
}
}
答案 0 :(得分:1)
如果您需要示例,请关注this link。
首先创建模型
public class StationBean{
String id;
String stationName;
StationBean() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
现在,如果您想显示数据,则需要使用Recycleview Adapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
List<StationBean> StationBean;
RVAdapter(List<StationBean> StationBean) {
this.stationBean= StationBean;
}
@Override
public StationBeanViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
StationBeanViewHolder pvh = new StationBeanViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
holder.stationName.setText(stationName.get(position).stationName);
}
@Override
public int getItemCount() {
if (stationName!= null) {
return persons.size();
}
return 0;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class StationNameViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView stationName;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
stationName = (TextView) itemView.findViewById(R.id.stationName);
}
}
}
然后你的活动方写下以下内容:
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(mContext);
rv.setLayoutManager(llm);
final RVAdapter rvAdapter = new RVAdapter(personList);
rv.setAdapter(rvAdapter);
try {
JSONObject obj = new JSONObject(result);
JSONArray stationBeanListJSONArray = null;
stationBeanListJSONArray = obj .getJSONArray("stationBeanList");
for (int i = 0; i < stationBeanListJSONArray .length(); i++) {
StationBeanperson = new StationBean();
JSONObject jObj =stationBeanListJSONArray .getJSONObject(i);
person.Id = jObj.getString("id");
person.stationName=jObj.getString("stationName");
personList.add(i, person);
}
rvAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
希望这有帮助!
答案 1 :(得分:0)
您必须使用以下代码解析结果:
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()) {
pd.dismiss();
}
try {
JSONObject json = new JSONObject(result);
JSONArray jsonArray = json.getJSONArray("stationBeanList");
String value = "";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
value = jsonObj.getString("stationName");
Log.d("TAG", "stationName :" + value);
}
} catch (JSONException e) {
e.printStackTrace();
}
// txtJson.setText(result);
}