package com.zb.json_text;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private List<Map<String, String>> slist= new ArrayList<Map<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path="http://m.lalas.cn/help/all_1.html?format=json&size=5";
try {
slist=getJSONObject(path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------------------------listchangdu-------------------------------"+slist.size());
}
public static List<Map<String, String>> getJSONObject(String path) throws Exception {
System.out.println("---------------------------------------进来了-------------------------------------");
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map =null;
URL url=new URL(path);
//利用HttpURLConnection对象,我们可以从网页中获取网页数据
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//单位为毫秒,设置超时时间为5秒
conn.setConnectTimeout(15*1000);
//HttpURLConnection对象是通过HTTP协议请求path路径的,所以需要设置请求方式,可以不设置,因为默认为get
conn.setRequestMethod("GET");
System.out.println("-------------------------conn.getResponseCode"+conn.getResponseCode());
if(conn.getResponseCode()==200){//判断请求码是否200,否则为失败
InputStream is=conn.getInputStream(); //获取输入流
byte[] data =readStream(is); //把输入流转换成字符串组
String json=new String(data); //把字符串组转换成字符串
//数据形式:{"total":2,"success":true,"arrayData":[{"id":1,"name":"小猪"},{"id":2,"name":"小猫"}]}
JSONObject jsonObject=new JSONObject(json); //返回的数据形式是一个Object类型,所以可以直接转换成一个Object
int total=jsonObject.getInt("count");
String keywords=jsonObject.getString("keywords");
System.out.println("=========================================================================");
System.out.println("!!!!!!!!!!!!!!--------------"+total+"--------"+keywords);
//里面有一个数组数据,可以用getJSONArray获取数组
JSONArray jsonArray=jsonObject.getJSONArray("data");
for(int i =1;i<jsonArray.length();i++){
JSONObject item=jsonArray.getJSONObject(i); //得到没个对象
int id =item.getInt("id");
String title=item.getString("title");
String description=item.getString("description");
int time =item.getInt("time");
map=new HashMap<String,String>();
map.put("id", id+"");
map.put("title", title);
map.put("description", description);
map.put("time", time+"");
list.add(map);
}
for (Map<String, String> list2 : list) {
String id = list2.get("id");
String name = list2.get("description");
Log.i("abc", "id:" + id + " | name:" + name);
}
}
return list;
}
private static byte[] readStream(InputStream inputStream) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
bout.write(buffer, 0, len);
}
bout.close();
inputStream.close();
return bout.toByteArray();
}
}
AndroidManifest.xml里也加了权限:
但是运行后是没有拿到任何数据的。调试在走到获取请求码(conn.getResponseCode())的时候就断了。为什么这里会报错呢?