当我使用类的方法时崩溃代码?

时间:2014-08-12 05:53:28

标签: java android

我有一个MainActivity连接到webService login表示它运行正常但是当我使用Logins.Class的方法时,它会让我崩溃:< / p>

public class MainActivity extends Activity {
    EditText UserName;
    EditText PassWord;
    Button Login;
    String mUserName;
    String mPassWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UserName = (EditText) findViewById(R.id.edtUserName);
        PassWord = (EditText) findViewById(R.id.edtPasswprd);
        Login = (Button) findViewById(R.id.btnLogin);

        Login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mUserName = UserName.getText().toString().trim();
                mPassWord = PassWord.getText().toString().trim();
                if (mUserName.length() > 0 && mPassWord.length() > 0) {
                    Logins LG = new Logins().UsernameValue(mUserName)
                            .PasswordValue(mPassWord).LOGINS();
            ------> Log.i("OK", LG.getWebresponse());<--- Crash Here
                } else {
                    Toast.makeText(
                            G.context,
                            G.context.getResources().getString(
                                    R.string.btn_LoginError), Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
    }
}

我有一个Logins.Class登录:

public class Logins {
    private String UsernameValues;
    private String PasswordValues;

    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME = "Login";
    String SOAP_ACTION = "http://tempuri.org/Login";
    String URL = "http://test.com/MobileWebService/Related.asmx";
    private String Webresponse;

    public Logins LOGINS() {
        Thread thread = new Thread(new Runnable() {
            //"xxssxx"
            //123456789
            @Override
            public void run() {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("username", UsernameValues);
                request.addProperty("pass", PasswordValues);
                request.addProperty("device", "0");
                request.addProperty("security", 128110);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setAddAdornments(false);
                envelope.implicitTypes = true;
                envelope.setOutputSoapObject(request);
                HttpTransportSE conn = new HttpTransportSE(URL);
                conn.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                try {
                    conn.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive) envelope
                            .getResponse();
                    Webresponse = response.toString();

                    Log.i("LOG", Webresponse);
                }
                catch (Exception e) {
                    e.printStackTrace();
                    Log.i("xxx", e.toString());
                }
            }
        });
        thread.start();
        return this;
    }

    public Logins UsernameValue(String value) {
        UsernameValues = value;
        return this;
    }

    public Logins PasswordValue(String value) {
        PasswordValues = value;
        return this;
    }

    public String getWebresponse() {
        return Webresponse;
    }

}

所有代码都做得很好,但当我使用getWebresponse MethodMainActivity获取Webresponse的结果时,我会崩溃?

我的崩溃是:

by log Tag :AndroidRuntime
by Log Message :at com.test.MainActivity$1$1.onClick(MainActivity.java:53)
by PID : 9145

1 个答案:

答案 0 :(得分:1)

问题是你直接获得了仍然为空的Webresponse。在完成对Web请求的响应时初始化它。

现在LG.getWebresponse()将返回null,您需要等待Web响应完成,然后才能在日志中使用Webresponse

<强>溶液

您需要使用asyntask而不是thread,并将onPostExecute()方法中的日志安全Webresponse初始化为网络请求的响应。