打開URL後的數據:
寫的測試類:
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())的時候就斷了。為什麼這裡會報錯呢?
你好像是在UI主線程裡面獲取網絡數據吧,安卓4.0以後貌似不能這樣,你可以試試看在AndroidManifest.xml文件中把android:targetSdkVersion=“”刪除掉試試看。不過這個方法就算可以也不是治本的方法,
你可以嘗試用AsyncTask來獲取網絡數據試試看