使用URLEncoder在MyTask中出错 - android& java的

时间:2016-03-19 06:24:44

标签: java android eclipse

我在我的活动中使用URLEncoder。但我在MyTask中有错误。我在代码中用错误标记了错误。

public class Search_Ringtone extends SherlockActivity{

ListView lsv_latest;
List<ItemRingCategoryItem> arrayOfRingcatItem;
RingCateItemAdapter objAdapterringitemitem;
AlertDialogManager alert = new AlertDialogManager();
private ItemRingCategoryItem objAllBean;
JsonUtils util;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ringcatitem_activity);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

    lsv_latest=(ListView)findViewById(R.id.latest_list);
    arrayOfRingcatItem=new ArrayList<ItemRingCategoryItem>();

    if (JsonUtils.isNetworkAvailable(Search_Ringtone.this)) {
         String str = Constant.SEARCH_RINGTONE_URL+Constant.SEARCH.replace(" ", "%20");
         String myUrl = URLEncoder.encode(str, "UTF-8");

         MyTask().execute(myUrl); //*Error*

    } else {
        showToast("No Network Connection!!!");
        alert.showAlertDialog(Search_Ringtone.this, "Internet Connection Error",
                "Please connect to working Internet connection", false);
    }


    lsv_latest.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

            objAllBean=arrayOfRingcatItem.get(position);
            Intent intplay=new Intent(getApplicationContext(),SingleRingtone.class);
            Constant.RINGTONE_ITEMID=objAllBean.getRingItemId();
            startActivity(intplay);

        }
    });

}

    private class MyTask extends AsyncTask<String, Void, String> {

    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(Search_Ringtone.this);
        pDialog.setMessage("لطفا صبر کنید...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        return JsonUtils.getJSONString(params[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }

        if (null == result || result.length() == 0) {
            showToast("Server Connection Error");
            alert.showAlertDialog(getApplicationContext(), "Server Connection Error",
                    "May Server Under Maintaines Or Low Network", false);

        } else {

            try {
                JSONObject mainJson = new JSONObject(result);
                JSONArray jsonArray = mainJson.getJSONArray(Constant.LATEST_ARRAY_NAME);
                JSONObject objJson = null;
                if(jsonArray.length()==0)
                {
                    showToast("موردی پیدا نشد!");
                }
                else
                {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        objJson = jsonArray.getJSONObject(i);

                        ItemRingCategoryItem objItem = new ItemRingCategoryItem();

                        objItem.setRingItemId(objJson.getString(Constant.CATEITEMRING_RINDID));
                        objItem.setRingItemCatId(objJson.getString(Constant.CATEITEMRING_RINDCATID));
                        objItem.setRingItemCatName(objJson.getString(Constant.CATEITEMRING_CATENAME));
                        objItem.setRingItemName(objJson.getString(Constant.CATEITEMRING_RINGNAME));
                        objItem.setRingItemUrl(objJson.getString(Constant.CATEITEMRING_RINDURL));
                        objItem.setRingItemDownCount(objJson.getString(Constant.CATEITEMRING_RINDDOWNCOUNT));
                        objItem.setRingItemUser(objJson.getString(Constant.CATEITEMRING_RINDUSER));
                        objItem.setRingItemTag(objJson.getString(Constant.CATEITEMRING_RINDTAG));
                        objItem.setRingItemSize(objJson.getString(Constant.CATEITEMRING_RINDSIZE));
                        objItem.setRingStar(objJson.getString(Constant.LATESTRING_RINGSTAR));
                        objItem.setRingImage(objJson.getString(Constant.LATESTRING_RINGIMAGE));
                        arrayOfRingcatItem.add(objItem);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            setAdapterToListview();
        }

    }
}

2 个答案:

答案 0 :(得分:0)

不应该是new MyTask().execute(myURL);吗?

另外,因为它是AsyncTask,你需要保留对它的引用,直到它完成,否则垃圾收集器会破坏它。

答案 1 :(得分:0)

我这样写并解决了谢谢大家:

        if (JsonUtils.isNetworkAvailable(Search_Ringtone.this)) {
         String str = Constant.SEARCH_RINGTONE_URL+Constant.SEARCH.replace(" ", "%20");
         String myUrl = null;
        try {
            myUrl = URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         new MyTask().execute(myUrl);