我在没有使用异步任务的情况下完成了 JSON 解析,它在姜饼(2.3)中运行良好,但是当我在 ICS(冰淇淋三明治)应用程序上运行相同的项目时,我听说某处我必须在其中进行解析asynctask,但我是 android 新手,无法做到这一点,有人可以帮助我............
这是我的 JSON 类:-
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
这是我使用 JSON 类的 MAinActivity:-
public class MainActivity extends ListActivity {
Context context;
ArrayList<String> contentList,slugList;
ArrayList<HashMap<String, String>> mylist;
JSONObject json;
JSONArray latest;
ImageView bck;
String shareUrl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
bck = (ImageView) findViewById(R.id.bckbtn);
mylist = new ArrayList<HashMap<String, String>>();
contentList=new ArrayList<String>();
slugList=new ArrayList<String>();
json = JSONfunctions.getJSONfromURL("http://madhuridixit-nene.com/wp/?json=get_category_posts&slug=latest");
context=this;
try{
latest = json.getJSONArray("posts");
for(int i=0;i<latest.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = latest.getJSONObject(i);
String name = e.getString("title_plain");
String content = e.getString("content");
contentList.add(content);
String chng = "–";
String fnl_Str = name.replace(chng, "");
String slug = e.getString("slug");
slugList.add(slug);
map.put("id", String.valueOf(i));
map.put("name", fnl_Str);
map.put("date", e.getString("date"));
shareUrl ="http://madhuridixit-nene.com/latest/post/";
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.activity_latest_tab,
new String[] { "name"},
new int[] { R.id.item_title});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); */
//Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(context,WebViewActivity.class);
intent.putExtra("content", contentList.get(position));
intent.putExtra("shareUrl", shareUrl+slugList.get(position));
intent.putExtra("tab_value", 1);
startActivity(intent);
}
});
}
请帮助我使用 AsyncTask 完成这项工作.......