Question

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();
        }         
    }

} 
} 

Log cat error:

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)

This question is already asked few times. But it doesnt help for my problem. While debugging i got the above error. I dont know what was the mistake in my code. I went through this linklink. But still i didnt get my where is the problem.

Was it helpful?

Solution

I think its issue with showing the toast in the doInBackground Method of the AsyncTask. You need to move the UI related things to onPostExecute.

You are showing Toast in doLogin method which is called in the doInBackground method of the AsyncTask. You need to remove that.

OTHER TIPS

Looking at the error:

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

It's saying your error is with displaying toasts. You can only display toasts on the UI thread.

     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!
}

The above is happening on a background thread, and doLogin() is using toasts.

See this answer to find out how to call a toast from an async task.

You could also call Activity.runOnUiThread(Runnable r) and place your toast call inside that.

To make this DRY code, you could create a method called showToast(String text) that looks like this:

    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();
            }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top