首先有个工具类: 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();
//创建HttpGet对象
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse;
try
{
//使用execute方法发送 HttpGet请求,并返回httRresponse对象
httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK)
{
//获得返回结果
response=EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return response;
}
}
然后主类
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 + "个Item,itemTitle的值是:" + title
+ "itemContent的值是:" + content,
Toast.LENGTH_SHORT).show();
}
});
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。