Android AsyncTask和SOAP Web服务

Android AsyncTask和SOAP Web服务,android,web-services,soap,android-asynctask,android-studio-2.3,Android,Web Services,Soap,Android Asynctask,Android Studio 2.3,我不熟悉SOAP Web服务和AsyncTask,我正在开发一个使用SOAP Web服务进行登录的应用程序。 我试图用onCreate方法完成所有工作,但得到了android.os.NetworkOnMainThreadException 所以我试着用AsyncTask, 但我无法在RetrieveFeedTask类中获取对emailText和passwordText的引用 这是我的密码: public class MainActivity extends AppCompatActivity

我不熟悉SOAP Web服务和AsyncTask,我正在开发一个使用SOAP Web服务进行登录的应用程序。 我试图用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();
            }
        }
    }
public类MainActivity扩展了AppCompatActivity
实现NavigationView.OnNavigationItemSelectedListener{
私有静态字符串SOAP\u ACTION=“故意隐藏”;
私有静态字符串NAMESPACE=“故意隐藏”;
私有静态字符串方法\u NAME=“故意隐藏”;
私有静态字符串URL=“故意隐藏”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
最终EditText emailText=(EditText)findViewById(R.id.emailText);
最终EditText passwordText=(EditText)findViewById(R.id.passwordText);
按钮登录按钮=(按钮)findViewById(R.id.Button\u登录);
loginButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建RetrieveFeedTask().execute();
}
});
}

类RetrieveFeedTask扩展了AsyncTask{ 受保护的字符串doInBackground(字符串…URL){ SoapObject请求=新的SoapObject(名称空间、方法名称); request.addProperty(“用户名”,emailText.getText().toString()); request.addProperty(“userPassword”,passwordText.getText().toString()); SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(请求); envelope.dotNet=true; 试一试{ HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL); 调用(SOAP_操作,信封); SoapObject结果=(SoapObject)envelope.bodyIn; 如果(结果!=null){ Toast.makeText(getApplicationContext(),“登录成功!”,Toast.LENGTH_LONG.show(); }否则{ Toast.makeText(getApplicationContext(),“登录失败!”,Toast.LENGTH_LONG.show(); } }捕获(例外e){ e、 printStackTrace(); } } }
您可以将视图的文本值传递到异步任务的构造函数中,并通过内部引用访问它们

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) {
        ...
    }
}
或者,您可以将参数作为数组直接传递给
doInBackground()

protected Void doInBackground(String... urls) {
    String email = urls[0];    
    String password = urls[1]; 
    ....  
}
然后这样称呼它:

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

new RetrieveFeedTask().execute(urls);

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

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) {
        ...
    }
}
或者,您可以将参数作为数组直接传递给
doInBackground()

protected Void doInBackground(String... urls) {
    String email = urls[0];    
    String password = urls[1]; 
    ....  
}
然后这样称呼它:

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

new RetrieveFeedTask().execute(urls);

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

首先在onClick侦听器中:

字符串email=emailText.getText().toString()

字符串密码=passwordText.getText().toString()

新建RetrieveFeedTask().execute(电子邮件、密码)

AsyncTask doInBackground方法中的第二个:

字符串email=URL[0]

字符串密码=URL[1]


您几乎可以将任意值传递到AsyncTask的execute方法中,这些值在其doInBackground方法中作为参数接收。

如果将emailText和passwordText直接传递到AsyncTask中,您只需要做一些小的更改:

首先在onClick侦听器中:

字符串email=emailText.getText().toString()

字符串密码=passwordText.getText().toString()

新建RetrieveFeedTask().execute(电子邮件、密码)

AsyncTask doInBackground方法中的第二个:

字符串email=URL[0]

字符串密码=URL[1]

您几乎可以将任意值传递到AsyncTask的execute方法中,该方法在其doInBackground方法中作为参数接收。

您可以使用该方法(AsyncTask带有onPreExecute和onPostExecute,但没有NetworkOnMainThreadException):


class RetrieveFeedTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…URL){
字符串结果=”;
SoapObject请求=新的SoapObject(名称空间、方法名称);
request.addProperty(“用户名”,emailText.getText().toString());
request.addProperty(“userPassword”,passwordText.getText().toString());
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
调用(SOAP_操作,信封);
SoapObject结果=(SoapObject)envelope.bodyIn;
如果(结果!=null){
结果=“登录成功!”;
}否则{
结果=“登录失败!”;
}
}捕获(例外e){
e、 printStackTrace();
}
返回结果;
}
@凌驾
受保护的字符串onPostExecute(字符串结果){
super.onPostExecute(结果);
if(result.equals(“登录成功!”){
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG.show();
}否则{
Toast.makeText(getApplicationContext(),“登录失败!”,Toast.LENGTH_LONG.show();
}
}
}
您可以将此(异步)任务与onPreExecute一起使用