使用SAX解析器的RSS

时间:2014-11-04 08:35:04

标签: android rss

我想在这个简单的SAX RSS解析器代码中使用AsyncTask代替线程 在ListFragment中,什么是更好的Asynctask或多线程。

这是我的代码

public class ListItem扩展了ListFragment {

public  ArrayList<NewsItem> getValues() {
        return values;
}

public void setValues(ArrayList<NewsItem> values) {
        this.values = values;
}

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_news_layout, container, false);
        return view;
}
enter code here
public void onStart() {
        super.onStart();
}

public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        refresh();
        listAdapter = new ListAdapter(getActivity(), R.layout.listitem,
                        getValues());
        setListAdapter(listAdapter);

}
enter code here
public void refresh() {
        Thread th = new Thread(new Runnable() {
                public void run() {
                        ListHandler listHandler = new ListHandler();
                        try {
                                listHandler.processFeed();
                                setValues(listHandler.getNewsItemList());
                                listAdapter.setNewsItemList(getValues());
                                listAdapter.notifyDataSetChanged();
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
        });
        th.start();

}

1 个答案:

答案 0 :(得分:0)

实际上Asynctask也在使用线程,但对于onpostexecute和onprexecute()更好 方法回调如果使用线程,默认情况下不会得到它。

喜欢这个

private class HttpAsyncDealsTask extends
			AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {

		protected ArrayList<HashMap<String, String>> doInBackground(
				String... dealslist) {

			ArrayList<HashMap<String, String>> chekinRsp = new ArrayList<HashMap<String, String>>();
			// get user data from session
			HashMap<String, String> user = session.getUserDetails();
			// mem_id
			String mem_id = user.get(SessionManager.KEY_ID);
			String rest_id = getArguments().getString("getid");
			try {

				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
						2);

				nameValuePairs.add(new BasicNameValuePair("rest_id", rest_id));
				nameValuePairs.add(new BasicNameValuePair("mem_id", mem_id));
				// Creating service handler class instance
				ServiceHandler sh = new ServiceHandler();

				// Making a request to url and getting response
				String jsonStr = sh.makeServiceCall(URL, ServiceHandler.POST,
						nameValuePairs);

				Log.d("Response: ", "> " + jsonStr);
				if (jsonStr != null) {
					try {
						// creating new HashMap
						HashMap<String, String> map = new HashMap<String, String>();
						JSONObject jresponse = new JSONObject(jsonStr);
						JSONObject msg = jresponse.getJSONObject(KEY_MSG);
						//String error = jresponse.getString(KEY_ERROR);
						String msgss = msg.getString(KEY_MSGSS);
						String error = msg.getString(KEY_ERROR);
						
						if(error.equalsIgnoreCase("0"))
						{
							String getid = getArguments().getString("getid");
							String getname = getArguments().getString("getname");
							String getpoints = getArguments().getString("getpoints");
							String getcomp = getArguments().getString("getcomp");
							String getmemberpoint = getArguments().getString("getmemberpoint");
							
							System.out.println(getid + getname + getpoints + getcomp + getmemberpoint);
							
							CheckinPref checkin = new CheckinPref(getActivity());
						    checkin.Prefernce_save(getid
						    		,getname, getpoints
						    		,getcomp, getmemberpoint);
							
						}
						
						map.put(KEY_MSGSS, msgss);
						chekinRsp.add(map);
						// Log.d("OUT", msg);
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}

			} catch (Exception e) {
				Log.d("Response", e.getLocalizedMessage());
			}

			return chekinRsp; // return result
		}

		@Override
		protected void onPostExecute(
				ArrayList<HashMap<String, String>> chekinRsp) {
			System.out.println(chekinRsp.get(0).get(KEY_MSGSS));

			TextView chekinResp = (TextView) getActivity().findViewById(
					R.id.chekinResp);
			chekinResp.setText(chekinRsp.get(0).get(KEY_MSGSS));

			Button backButton = (Button) getActivity().findViewById(
					R.id.btn_restback);
			backButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {

					FragmentManager fm = getActivity().getFragmentManager();
					fm.popBackStack();
				}
			});

		}

	}

并使用
开始 新的HttpAsyncDealsTask()。execute();