作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我在 textview 中获取 JSON 数据的代码。JSON 数据仅在我的代码中。现在,如果我的数据在某个 URL 中,那么我该如何获取这些数据?
旧的答案使用不再支持的 defaultHTTPClient,我尝试使用 retrofit 和 volley 但无法理解。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView output = (TextView) findViewById(R.id.textView1);
String strJson="{ \"Employee\" :[{\"id\":\"101\",\"name\":\"Sonoo Jaiswal\",\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"Vimal Jaiswal\",\"salary\":\"60000\"}] }";
String data = "";
try {
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("id").toString());
String name = jsonObject.optString("name").toString();
float salary = Float.parseFloat(jsonObject.optString("salary").toString());
data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";
}
output.setText(data);
} catch (JSONException e) {e.printStackTrace();}
}
最佳答案
编辑
使用Volley如果找不到 httpclient。
此外,解析脚本与下面提到的相同。
我已经写了一个Blog .引用那个。希望能帮助到你。让我克隆我的博客以满足您的要求。请为此使用正确的命名。这是解析输出。
public class GridUI extends Activity {
ArrayList<Persons> personsList;
GridView gridView;
GridAdapter gridAdapter;
private static final String url="http://www.zippytrailers.com/funlearn/topicsMap";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
personsList= new ArrayList<Persons>();
new JSONAsyncTask().execute(url);
gridView=(GridView)findViewById(R.id.gridview);
gridAdapter = new GridAdapter(this, R.layout.gridview_row, personsList);
gridView.setAdapter(gridAdapter);
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog=new ProgressDialog(GridUI.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if(status==200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono=new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("results");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Persons name = new Persons();
name.setName(object.getString("syllabus"));
name.setDescription(object.getString("grade"));
name.setDob(object.getString("subject"));
name.setCountry(object.getString("topic"));
name.setHeight(object.getString("id"));
personsList.add(name);
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
dialog.cancel();
gridAdapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
关于android - 如何从url获取JSON数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35598649/
我是一名优秀的程序员,十分优秀!