AsyncTask和SOAP Web服务

时间:2017-07-07 15:43:43

标签: android web-services soap android-asynctask android-studio-2.3

我是SOAP webservices和AsyncTask的新手,我正在开发一个使用SOAP webservices进行登录的应用程序。 我尝试在onCreate方法中完成所有工作,但我得到了android.os.NetworkOnMainThreadException。

所以我尝试使用AsyncTask, 但是我无法在RetrieveFeedTask类中引用emailText和passwordText。

这是我的代码:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    private static String SOAP_ACTION = "intentionally hided";
    private static String NAMESPACE = "intentionally hided";
    private static String METHOD_NAME = "intentionally hided";
    private static String URL = "intentionally hided";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final EditText emailText = (EditText) findViewById(R.id.emailText);
        final EditText passwordText = (EditText) findViewById(R.id.passwordText);
        Button loginButton = (Button) findViewById(R.id.button_login);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new RetrieveFeedTask().execute();

            }
        });
    }

    class RetrieveFeedTask extends AsyncTask<String, String, String> {



        protected String doInBackground(String... urls) {


            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            request.addProperty("userName", emailText.getText().toString());
            request.addProperty("userPassword", passwordText.getText().toString());

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                androidHttpTransport.call(SOAP_ACTION, envelope);

                SoapObject result = (SoapObject) envelope.bodyIn;
                if (result != null) {
                    Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

3 个答案:

答案 0 :(得分:3)

您可以将视图的文本值传递给异步任务的构造函数,并通过内部引用访问它们。

class RetrieveFeedTask extends AsyncTask<String, String, String> {

    private String emailText, passwordText;

    RetrieveFeedTask(String emailText, String passwordText) {
        this.emailText = emailText;
        this.passwordText = passwordText;
    }

    protected String doInBackground(String... urls) {
        ...
    }
}

然后你会这样称呼它:

 new RetrieveFeedTask(
        emailText.getText().toString(), 
        passwordText.getText().toString()
 ).execute();

或者,您可以将参数作为数组直接传递给doInBackground()

protected Void doInBackground(String... urls) {
    String email = urls[0];    
    String password = urls[1]; 
    ....  
}

然后这样称呼它:

String[] urls = { 
    emailText.getText().toString(), 
    passwordText.getText().toString() 
};

new RetrieveFeedTask().execute(urls);

答案 1 :(得分:1)

你可以使用它(AsyncTask with onPreExecute和onPostExecute,但没有NetworkOnMainThreadException):

class RetrieveFeedTask extends AsyncTask<String, String, String> {

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

    @Override
    protected String doInBackground(String... urls) {

        String result = "";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("userName", emailText.getText().toString());
        request.addProperty("userPassword", passwordText.getText().toString());

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;

        try {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapObject result = (SoapObject) envelope.bodyIn;
            if (result != null) {
                result = "Login Successful!";
            } else {
                result = "Login Failed!";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

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

        if (result.equals("Login Successful!")) {
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
        }
    }
}

答案 2 :(得分:0)

您将emailText和passwordText直接传递到AsyncTask,您只需进行一些小的更改:

首先在你的onClick Listener中:

String email = emailText.getText()。toString();

String password = passwordText.getText()。toString();

new RetrieveFeedTask()。execute(email,password);

AsyncTask doInBackground方法中的第二个:

String email = urls [0];

String password = urls [1];

你可以将任意值传递给AsyncTask的execute方法,它在doInBackground方法中作为参数接收。