Android webservice access error

扶醉桌前 提交于 2019-12-12 03:38:42

问题


I have a simple java web service deployed on Tomcat7 server. I have used Axis2 as soap engine.(Web service & WSDL is OK) Here is the code I have used for the web service

  public class TestWs {
  public String sayHello(){
         return "Hello Grant";
    }
   }

I have accessed this web service from Android 2.2.It is perfectly OK.But this program not works for Android 4.0.3 Code of my Android program

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;

public class AndroidWSClientActivity extends Activity {

private static final String SOAP_ACTION = "http://ws.android4.com/";
private static final String METHOD_NAME = "sayHello";
private static final String NAMESPACE = "http://ws.android4.com/sayHello/";
private static final String URL = "http://175.157.45.91:8085/ForAndroid4/services/TestWs?wsdl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          

    SoapSerializationEnvelope envelope = new 
    SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);

    HttpTransportSE ht = new HttpTransportSE(URL);
    try {
        ht.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        String str = response.toString();
        TextView result;
        result = (TextView)findViewById(R.id.textView1);
        result.setText(str);

    } catch (Exception e) {
        e.printStackTrace();
        TextView result;
        result = (TextView)findViewById(R.id.textView1);
        result.setText("Error!");

    }
  }
 }

Program correctly installed on Emulator. But emulator shows the message set from catch block. What is the error ??? How can I correct it ?? Thanks!


回答1:


In Honeycomb and Ice Cream Sandwich (i.e. Android 3.0+) , you cannot connect to the internet in the main thread (onCreate(), onPause(), onResume() etc.), and you have to instead start a new thread. The reason why this has changed is because network operations can make the app wait for a long time, and if you're running them in the main thread, the whole application becomes unresponsive. If you try to connect from the main thread, Android will throw a NetworkOnMainThreadException.




回答2:


In latest android version, it is not possible make web service call from UI thread.There is a similar thread related to this.

Android kSOAP web service problem

May be this will help you.



来源:https://stackoverflow.com/questions/10051762/android-webservice-access-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!