如何从Web服务获取响应值

时间:2018-09-12 21:21:35

标签: android ksoap2 android-webservice

我在Android上有简单的登录屏幕。我正在向.NET Web服务发送用户名和密码,并且返回的是true或false。我想做,如果返回true,那么我将开始另一个意图。如果返回为假,则我想显示错误消息。

这是我的AsyncTask

private class AsyncCallWS extends AsyncTask<String , Void , Void>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        error.setText("Giriş Yapılıyor....");
    }
    @Override
    protected Void doInBackground(String... objects) {
        getLoginCredentials(username , password);
        return null;
    }
    @Override
    protected void onPostExecute(Void o) {
        super.onPostExecute(o);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

这是我的getLoginCredentials方法

 private void getLoginCredentials(String uName , String passw){
        SoapObject request = new SoapObject(NAMESPACE , METHOD_NAME);

        PropertyInfo usernamePI = new PropertyInfo();
        usernamePI.setName("kAdi");           
        usernamePI.setValue(username);        
        usernamePI.setType(String.class); 
        request.addProperty(usernamePI);

        PropertyInfo passwordPI = new PropertyInfo();
        passwordPI.setName("sifre");         
        passwordPI.setValue(password);      
        passwordPI.setType(String.class);    
        request.addProperty(passwordPI);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
            androidHttpTransport.call(SOAP_ACTION , envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            loginStatus = new Boolean(response.toString());

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


    }

这是我的主要密码

public class MainActivity extends AppCompatActivity {
    final String NAMESPACE = "http://tempuri.org/";
    final String URL = "http://192.168.1.50:60599/AndroidWebService.asmx";
    final String SOAP_ACTION = "http://tempuri.org/AndroidDeneme";
    final String METHOD_NAME = "AndroidDeneme";
    Button btnLogin;
    EditText kAdi , sifre;
    TextView error;

    String username;
    String password;
    String DonenDeger;
    boolean loginStatus;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnLogin = (Button) findViewById(R.id.btnGirisYap);
        kAdi = (EditText) findViewById(R.id.etKullaniciAdi);
        sifre = (EditText) findViewById(R.id.etSifre);
        error = (TextView) findViewById(R.id.txtError);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((kAdi.getText().length() != 0 && kAdi.getText().toString() != null) && (sifre.getText().length() != 0 && sifre.getText().toString() != null)){
                    username = kAdi.getText().toString();
                    password = sifre.getText().toString();
                    new AsyncCallWS().execute();
                    Intent intent = new Intent(MainActivity.this , activity_home.class);

                    /*/if (loginStatus){

                    }
                    else{

                    }*/
                }
                else{
                    error.setText("Boş alan bırakmayınız.");
                }
            }
        });
    }

0 个答案:

没有答案