bareban 2014年01月07日
android中json获取问题,求来大神啊
做个小练习,想要获取服务器上json的数据,用byte可以获取到,但想把它加到listview中显示就不行 下面是代码
Activity:
package com.huwai.travle_517;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.huwai.JsonParse.HttpTaskManager;
import com.huwai.JsonParse.JSONParse;

public class TravleActivity extends Activity {
// private static String url = "http://www.517huwai.com/Mobile/blogs";
private static ListView listview = null;
private List<HashMap<String, Object>> lists = null;
// private TextView text = null;
public Handler mHandler = new Handler();
protected HttpTaskManager httpManager = HttpTaskManager.getInstance();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_travle);
//listview = (ListView)super.findViewById(R.id.mylist);
//productData();
// text = (TextView)super.findViewById(R.id.mytext);

try {
httpManager.submit(new Runnable() {

@Override
public void run() {

try {
lists = JSONParse.analysis(JSONParse.readParse());

mHandler.post(new Runnable() {
@Override
public void run() {
productData(lists);
}
});
} catch (Exception e) {

} finally {

}
}
});
// text.setText(JSONParse.readParse());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


private void productData( List<HashMap<String, Object>> lists){

List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for(HashMap<String, Object> title : lists){
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("title", title.get("title"));
data.add(item);
}
SimpleAdapter listItemAdapter = new SimpleAdapter(TravleActivity.this,
data,R.layout.listview_item,
new String[]{"title"},
new int[]{R.id.textView});
listview.setAdapter(listItemAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.travle, menu);
return true;
}



}

json:
package com.huwai.JsonParse;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;


public class JSONParse {
//获取JSON

public static String readParse(/*String urlPath*/) {
StringBuffer strB = new StringBuffer();
try {
URL url1 = new URL("http://www.517huwai.com/Mobile/blogs");
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();

InputStreamReader r = new InputStreamReader(conn.getInputStream());
BufferedReader rd = new BufferedReader(r);
String line;
while ((line = rd.readLine()) != null) {
strB.append(line);
Log.i("hello", line);
}
rd.close();
Log.i("xiaoding", "strB .. " + strB.toString());
} catch (Exception e) {
Log.e("xiaoding", "e ="+e);
}
Log.d("xiaoding","strB.toString() .... ="+strB.toString());

return strB.toString();
}

//解析JSON
public static ArrayList<HashMap<String, Object>> analysis(String jsonStr)
throws JSONException {
JSONArray jsonArray = null;


ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", jsonObject.getString("title"));
list.add(map);

}

return list;
}
}

HttpTasKManager:
package com.huwai.JsonParse;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpTaskManager {

private final static ExecutorService POOL = Executors.newFixedThreadPool(8);
private final static HttpTaskManager MANAGER = new HttpTaskManager();

private HttpTaskManager(){}

public static HttpTaskManager getInstance(){
return MANAGER;
}

public void submit(Runnable obj){

POOL.submit(obj);
}

public void submit(Callable<?> obj){

POOL.submit(obj);

}

public void shutdown(){

POOL.shutdown();

}


}

求高手啊!感激不尽
...全文
156 点赞 收藏 8
写回复
8 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告