KSOAP2 HelloWorld Web服务失败

时间:2014-07-21 18:36:42

标签: android web-services ksoap2

好的,我已经搜索了StackOverflow这个答案以及网络,我没有运气......我只是想尝试做最基本的网络服务功能... HelloWorld我有一个单击按钮,单击它时,文本设置为webservice中的函数返回的任何内容。这是我的代码:

public class MainActivity extends ActionBarActivity {

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://myInfo/App/Service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private final String METHOD_NAME = "HelloWorld";
private  String TAG = "SOAP";

private static String message;

Button b;
TextView tv;


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


    tv = (TextView) findViewById(R.id.textView1);

     b = (Button) findViewById(R.id.button1);
        //Button Click Listener
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {


                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute 
                    task.execute();
                    //If text control is empty

            }
        });
    }


private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        Log.i(TAG, "doInBackground");
        getMessage();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");
        tv.setText(message);
    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
        tv.setText("Rendering...");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

}

public void getMessage() {

    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //Property which holds input parameters
    /*PropertyInfo messagePI = new PropertyInfo();
    //Set Name
    messagePI.setName("Practice");
    //Set Value
    messagePI.setValue(messagePI);
    //Set dataType
    messagePI.setType(double.class);
    //Add the property to request object
    request.addProperty(messagePI);*/
    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    //Set output SOAP object
    envelope.setOutputSoapObject(request);
    //Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        //Invoke web service
        Log.i(TAG, "Point A");
        androidHttpTransport.debug=true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //androidHttpTransport.responseDump;
        //Get the response
        //SoapObject result = (SoapObject) envelope.bodyIn;
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        //Assign it to fahren static variable
        message = response.toString();

        /*if(message !=null){
            tv.setText(meesage.getProperty(0).toString());
        }*/


        //tv.setText("" + results);


    } catch (Exception e) {
        tv.setText(e.getMessage());
        e.printStackTrace();
    }
}

这是我的LogCat

07-21 14:07:50.519: E/AndroidRuntime(9000): FATAL EXCEPTION: AsyncTask #1
07-21 14:07:50.519: E/AndroidRuntime(9000): Process: com.example.mysoap, PID: 9000
07-21 14:07:50.519: E/AndroidRuntime(9000): java.lang.RuntimeException: An error occured while executing doInBackground()
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.lang.Thread.run(Thread.java:841)
07-21 14:07:50.519: E/AndroidRuntime(9000): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6309)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:861)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.checkForRelayout(TextView.java:6724)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.setText(TextView.java:3911)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.setText(TextView.java:3769)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.setText(TextView.java:3744)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at com.example.mysoap.MainActivity.getMessage(MainActivity.java:148)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at com.example.mysoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:81)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at com.example.mysoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-21 14:07:50.519: E/AndroidRuntime(9000):     ... 4 more

当我刚运行程序时出现LogCat错误,但当我实际执行该程序时,我收到此消息

org.xmlpull.v1.XmlPullParserException: expected: START_TAG

在这一行: } catch (Exception e) {

任何帮助都将不胜感激......我现在已经被困在这一段时间了...... :(

2 个答案:

答案 0 :(得分:1)

我认为问题在于:从现在开始你在后台线程(doInBackground())并尝试设置文本(tv.setText)。只有主线程才能做到。 删除该语句“tv.setText(e.getMessage());”它应该工作

catch (Exception e) {
        tv.setText(e.getMessage());
        e.printStackTrace();
    }

这将解决问题: 引起:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。

然后你可以尝试修复另一个问题。

检查以下链接是否可以帮助您进行其他修复: ksoap2 org.xmlpull.v1.xmlpullparserexception expected start_tag error

答案 1 :(得分:1)

是的,你不能在后台调用setText()。只需从getMessage()方法返回值,并在doInBackground()中返回相同的值。您将在onPostExecute()中获取该值,您可以通过调用setText()来设置它。