0
public class MainActivity extends Activity {

private EditText username;
private EditText pass;
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://122.252.200.98/fts/registration.asmx";

String user_id;
String password;
String tran="2";
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button signin = (Button) findViewById(R.id.button1);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


            username = (EditText) findViewById(R.id.editText1);
            user_id = username.getText().toString();

            pass = (EditText)  findViewById(R.id.editText2);
            password = pass.getText().toString();



            new LoginTask().execute();

        }
    });   
}

private boolean doLogin(String user_id1, String password1, String transactiontype)    {   

    boolean result=false;
    final String SOAP_ACTION = "http://tempuri.org/getUser";
    final String METHOD_NAME = "getUser";     
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    PropertyInfo username = new PropertyInfo();
    PropertyInfo passw=new PropertyInfo();
    PropertyInfo trans=new PropertyInfo();

    username.setValue(user_id);
    username.setType(String.class);
    username.setName("custname");
    request.addProperty(username);

    passw.setValue(password);
    passw.setType(String.class);
    passw.setName("pwd");
    request.addProperty(passw);

    trans.setValue(tran);
    trans.setType(String.class);
    trans.setName("signtype");
    request.addProperty(trans);

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

    envelope.setOutputSoapObject(request);

    System.out.println(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        Object response= (Object)envelope.getResponse();
        Log.i("reply", response.toString());
        System.out.println("response" +response);

        if(response.toString().equalsIgnoreCase("Success-User Exists"))
        {
            result = true;
            System.out.println(result);
            Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
        }
        else if(response.toString().equalsIgnoreCase("Username does not Exists"))
        {
            result=false;
            Toast.makeText(getApplicationContext(), "username incorrect", Toast.LENGTH_SHORT).show();
        }
        else 
        {
            Toast.makeText(getApplicationContext(), "passsword incorrect", Toast.LENGTH_SHORT).show();
        }

    }catch(SocketException ex)
    {
        Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
        ex.printStackTrace();
    }
    catch (Exception e) {
        Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
        e.printStackTrace();
    }
    return result;

}


private class LoginTask extends AsyncTask<Void, Void, Void> {

    private final ProgressDialog dialog = new ProgressDialog(
            MainActivity.this);

    protected void onPreExecute() {

        this.dialog.setMessage("Logging in...");
        this.dialog.show();

    }


    protected Void doInBackground(Void... unused) {

        boolean auth=doLogin(user_id,password,tran);
        System.out.println(auth);
        Looper.prepare();
        return null;// don't interact with the ui!
    }


    protected void onPostExecute(Void result) {


        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }         
    }

} 
} 

记录猫错误:

11-12 17:10:41.943: E/Error :(19833): Error on soapPrimitiveData() Can't create handler  inside thread that has not called Looper.prepare()
11-12 17:10:41.944: W/System.err(19833): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-12 17:10:41.952: W/System.err(19833):    at android.os.Handler.<init>(Handler.java:152)
11-12 17:10:41.954: W/System.err(19833):    at android.widget.Toast.<init>(Toast.java:99)
11-12 17:10:41.957: W/System.err(19833):    at android.widget.Toast.makeText(Toast.java:262)
11-12 17:10:41.962: W/System.err(19833):    at com.example.logincheck.MainActivity.doLogin(MainActivity.java:113)
11-12 17:10:41.965: W/System.err(19833):    at com.example.logincheck.MainActivity.access$0(MainActivity.java:57)
11-12 17:10:41.967: W/System.err(19833):    at com.example.logincheck.MainActivity$LoginTask.doInBackground(MainActivity.java:145)
11-12 17:10:41.970: W/System.err(19833):    at com.example.logincheck.MainActivity$LoginTask.doInBackground(MainActivity.java:1)
11-12 17:10:41.973: W/System.err(19833):    at android.os.AsyncTask$2.call(AsyncTask.java:216)
11-12 17:10:41.975: W/System.err(19833):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:337)
11-12 17:10:41.978: W/System.err(19833):    at java.util.concurrent.FutureTask.run(FutureTask.java:169)
11-12 17:10:41.981: W/System.err(19833):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1119)
11-12 17:10:41.983: W/System.err(19833):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:612)
11-12 17:10:41.986: W/System.err(19833):    at java.lang.Thread.run(Thread.java:1052)

这个问题已经被问过几次了。但这对我的问题没有帮助。调试时出现上述错误。我不知道我的代码中有什么错误。我通过了这个链接链接。但我仍然没有得到我的问题在哪里。

4

2 回答 2

1

我认为doInBackground在 AsyncTask 的方法中显示吐司的问题。您需要将 UI 相关的东西移动到onPostExecute.

您在 AsyncTask的方法中doLogin调用的方法中显示 Toast。doInBackground你需要删除它。

于 2013-11-12T11:59:55.693 回答
0

查看错误:

  android.widget.Toast.<init>(Toast.java:99)
  at android.widget.Toast.makeText(Toast.java:262)

这是说您的错误在于显示敬酒。您只能在 UI 线程上显示 toast。

     protected Void doInBackground(Void... unused) {

    boolean auth=doLogin(user_id,password,tran);
    System.out.println(auth);
    Looper.prepare();
    return null;// don't interact with the ui!
}

以上发生在后台线程上,doLogin() 正在使用 toast。

请参阅答案以了解如何从异步任务中调用 toast。

你也可以调用 Activity.runOnUiThread(Runnable r) 并将你的 toast 调用放在里面。

要制作这个 DRY 代码,您可以创建一个名为 showToast(String text) 的方法,如下所示:

    public void showToast(String text) {
         try {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainAcivity.this, text, Toast.LENGTH_LONG).show();
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
于 2013-11-12T12:08:34.080 回答