javaandroidandroid-asynctaskjsoupandroid-parser

jsoup not working correctly inside asynctask with multiple if statements


I am making an app to fetch some html from url, using JSOUP. Parsing from single url works correctly but whenever I used more than one urls it doesn't work. This is my code:

public class Test extends AsyncTask<String, Void, Void> {
    String desc;
    String u;
    String key;

    String xy;

    public Test(Activity contex, String key) {
        this.mContex = contex;
        this.key = key;
    }

    Activity mContex;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = ProgressDialog
                .show(getActivity(), "", null, true);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.show();
        xy = key;

    }

    @Override
    protected Void doInBackground(String... params) {
        if (xy == "abcd") {
            String url = "http://.....";
            try {

                Document document = Jsoup.connect(url).get();
                Elements myin = document.select("div.content-wrap");
                desc = myin.text().toString();


            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        respText.setText(desc);

        mProgressDialog.dismiss();

    }

} 

the key is sent to asynctask using switch case.And I want to add 8 more if statements to doInbackground

What my problem is that whenever I run this project without ' if ' statement it works, but if I add 'if(xy=="abcd")' it doesn't work. No error is there, So I can't figure this.


Solution

  • if(xy.equals("abcd")){
        //do stuff
    }
    
    == operator compares memory addresses you should use `.equals()` when working with strings.