您好,登錄后才能下訂單哦!
首先有個(gè)工具類(lèi): Myhttp.class
package com.example.json; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class Myhttp { public String httpGet(String url) { String response = null; HttpClient httpClient = new DefaultHttpClient(); //創(chuàng)建HttpGet對(duì)象 HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse; try { //使用execute方法發(fā)送 HttpGet請(qǐng)求,并返回httRresponse對(duì)象 httpResponse = httpClient.execute(httpGet); int statusCode = httpResponse.getStatusLine().getStatusCode(); if(statusCode==HttpStatus.SC_OK) { //獲得返回結(jié)果 response=EntityUtils.toString(httpResponse.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } }
然后主類(lèi)
package com.example.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { ListView list; ArrayList<HashMap<String, String>> myArrayList = new ArrayList<HashMap<String, String>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = (ListView) findViewById(R.id.list); new getJsonTask().execute(null, null, null); } class getJsonTask extends AsyncTask<Object, Object, Object> { @Override protected Object doInBackground(Object... params) { String url = "http://www.baidu.com"; Myhttp myHttp = new Myhttp(); String retStr = myHttp.httpGet(url); try { JSONObject json1 = new JSONObject(retStr); String j1 = json1.getString("kinds"); JSONArray j2 = new JSONArray(j1); for (int i = 0; i < j2.length(); i++) { JSONObject jsonObject = j2.getJSONObject(i); String j3 = jsonObject.getString("breeds"); JSONArray j4 = new JSONArray(j3); for (int j = 0; j < j4.length(); j++) { JSONObject jsonObject1 = j4.getJSONObject(j); String j6 = jsonObject1.getString("id"); String j7 = jsonObject1.getString("breedName"); HashMap<String, String> map = new HashMap<String, String>(); map.put("id", j6); map.put("name", j7); myArrayList.add(map); } } } catch (JSONException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object result) { super.onPostExecute(result); start(); } } private void start() { SimpleAdapter mySimpleAdapter = new SimpleAdapter(this, myArrayList, R.layout.list_item, new String[] { "id", "name" }, new int[] { R.id.name, R.id.age }); list.setAdapter(mySimpleAdapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { @SuppressWarnings("unchecked") HashMap<String, String> map = (HashMap<String, String>) list .getItemAtPosition(position); String title = map.get("id"); String content = map.get("name"); Toast.makeText( getApplicationContext(), "你選擇了第" + position + "個(gè)Item,itemTitle的值是:" + title + "itemContent的值是:" + content, Toast.LENGTH_SHORT).show(); } }); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。