调用webservice时出现致命异常AsyncTask3错误

时间:2015-02-28 09:34:18

标签: android eclipse android-asynctask fatal-error

我试图调用webservice并将返回值设置为来自服务的textview。但是当我点击按钮时,我得到了致命的异常错误。我的错误在哪里?

Java代码:`

public class Login extends Activity {

private String SOAP_ACTION = "http://appointmentsystem.somee.com/HelloWorld";
private String METHOD_NAME = "HelloWorld";
private String NAMESPACE = "http://appointmentsystem.somee.com/";
private String URL = "http://appointmentsystem.somee.com/Service1.asmx";

private SoapObject request = null;
private SoapSerializationEnvelope envelope;
private HttpTransportSE androidHttpTransport;
private SoapPrimitive response;

private EditText loginEdtUser, loginEdtPassword;
private Button loginBtnEnter;

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

    loginEdtUser = (EditText) findViewById(R.id.loginEdtUser);
    loginEdtPassword = (EditText) findViewById(R.id.loginEdtPassword);
    loginBtnEnter = (Button) findViewById(R.id.loginBtnEnter);

    loginBtnEnter.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            AsyncCallWS task = new AsyncCallWS();
            task.execute();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Login();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
    }

    @Override
    protected void onPreExecute() {
    }

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

}

public void Login() {
    try {
        request = new SoapObject(NAMESPACE, METHOD_NAME);

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

        androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            response = (SoapPrimitive) envelope.getResponse();

            loginEdtUser.setText(response.toString());
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }

}

错误:

{02-28 11:31:16.935: E/AndroidRuntime(4489): FATAL EXCEPTION: AsyncTask #3
02-28 11:31:16.935: E/AndroidRuntime(4489): Process: oziylmz.randevusistemi, PID: 4489
02-28 11:31:16.935: E/AndroidRuntime(4489): java.lang.RuntimeException: An error occured while executing doInBackground()
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.lang.Thread.run(Thread.java:841)
02-28 11:31:16.935: E/AndroidRuntime(4489): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.Handler.<init>(Handler.java:200)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.Handler.<init>(Handler.java:114)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.widget.Toast$TN.<init>(Toast.java:351)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.widget.Toast.<init>(Toast.java:101)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.widget.Toast.makeText(Toast.java:265)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at oziylmz.randevusistemi.Login.Login(Login.java:109)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at oziylmz.randevusistemi.Login$AsyncCallWS.doInBackground(Login.java:69)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at oziylmz.randevusistemi.Login$AsyncCallWS.doInBackground(Login.java:1)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-28 11:31:16.935: E/AndroidRuntime(4489):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)}

3 个答案:

答案 0 :(得分:0)

您收到以下错误:

 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

这意味着您在后台尝试更改UI,因为我看到您的代码在Toast方法的catch中尝试了login数据。

 try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            response = (SoapPrimitive) envelope.getResponse();

            loginEdtUser.setText(response.toString());
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }

您无法在后台线程中更新UI,您必须处理onPostExecute中的错误或创建Handler对象并使用它。

您可以通过以下方式解决问题:

1- runOnUiThread Example

2- Handler

3-移动到onPostExecute。

请参阅以下文章了解更多信息:

Android background processing with Handlers, AsyncTask and Loaders - Tutorial

答案 1 :(得分:0)

 Toast.makeText(getApplicationContext(), "Hata:" + e.toString(),
                Toast.LENGTH_SHORT).show();

这导致异常,因为你无法在后台处理中触摸ui元素。

修正: 在async任务的onPostexecute()方法中进行吐司,而不是在UI线程上执行

答案 2 :(得分:0)

你想用

 runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Login();
                    }
             });