无法向服务器发送登录请求

时间:2013-07-26 06:12:42

标签: android http authentication login android-asynctask

Log cat error:
07-26 05:36:53.271: E/AndroidRuntime(2435):     java.lang.NullPointerException
07-26 05:36:53.271: E/AndroidRuntime(2435):     at com.example.servercommunication.tcpSocket.SendMessage(tcpSocket.java:135)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at com.example.suntrack1.ThirdActivity.SendLoginRequest(ThirdActivity.java:89)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at com.example.suntrack1.ThirdActivity$1.onClick(ThirdActivity.java:75)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at android.view.View.performClick(View.java:4204)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at android.view.View$PerformClick.run(View.java:17355)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at android.os.Handler.handleCallback(Handler.java:725)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at android.os.Looper.loop(Looper.java:137)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-26 05:36:53.271: E/AndroidRuntime(2435):     at dalvik.system.NativeStart.main(Native Method)

主程序:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.thirdactivity);

    txtusername =(EditText)findViewById(R.id.emailid);
    txtPassword =(EditText)findViewById(R.id.password);

    loginBtn=(Button)findViewById(R.id.button1);
    loginBtn.setOnClickListener(onClickListener);
}
@Override
public void onBackPressed() {
   // do nothing.
}
public void onPause()
{
   super.onPause();
}
public void onDestroy()
{
   super.onDestroy();
}

public void onResume()
{
   super.onResume();
}
private OnClickListener onClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.button1:

            if(!AttemptLogin())
            {
                SendLoginRequest();
            }
            break;
        }
    }};

public void SendLoginRequest()
{
pDialog = new ProgressDialog(this);
pDialog.setMessage("Login in Progress..");
pDialog.setTitle("Please wait");
pDialog.show();
final String d =FramePacket.FrameLoginCmd(mUserID, mPassword);
Log.d("Login Request :",d);
tcpSocket.SendMessage(d);

}


@Override
public void messageReceived(String message) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Login activiyt : Recived data :"+message,         Toast.LENGTH_LONG).show();
if (message.contains("$107"))
{
    final String [] data=message.split("\\~");
    //$107~1286~1~0~25-C~SuccessFul Login#
    if(data[2].contains("1"))
    {
        pDialog.dismiss();
        StartLogin=false;
        Contsants.TaxiNo=data[4];
        Toast.makeText(getApplicationContext(), data[5], Toast.LENGTH_LONG).show();
        Intent i  = new Intent(getApplicationContext(),FourthActivity.class);
        i.putExtra("_LOGINNAME", mUserID);
        Contsants.cont_loggedinID=mUserID;
        startActivity(i);
    }
    else 
    {
        pDialog.dismiss();
        Toast.makeText(getApplicationContext(), "Login Failed Please Try again", Toast.LENGTH_LONG).show();
        StartLogin=false;
    }
}

}

boolean StartLogin=false; 
protected boolean AttemptLogin() {
// TODO Auto-generated method stub
boolean cancel=false;
txtusername.setError(null);
txtPassword.setError(null);

mUserID = txtusername.getText().toString();
mPassword = txtPassword.getText().toString();

View focusView = null;

// Check for a valid password.
if (TextUtils.isEmpty(mUserID)) {
    txtusername.setError(getString(R.string.error_field_required));
    focusView = txtusername;
    cancel = true;              }
    /*else if (mPassword.length() < 4) {
    txtPassword.setError(getString(R.string.error_invalid_password));
    focusView = txtPassword;
    cancel = true;
}*/

// Check for a valid email address.
else if (TextUtils.isEmpty(mPassword)) {
    txtPassword.setError(getString(R.string.error_field_required));
    focusView = txtPassword;
    cancel = true;
}

if (cancel) {
    // There was an error; don't attempt login and focus the first
    // form field with an error.
    focusView.requestFocus();
} 
return cancel;

}

如果我单击登录按钮,不幸的是应用程序停止。登录请求不发送到服务器。我不知道问题出在哪里。我发布上面的日志cat错误。请帮我。

1 个答案:

答案 0 :(得分:0)

<{strong> final String d

因此,请检查null是否为空

来尝试关注代码

所以您的代码应

tcpSocket.SendMessage(d)