Estou com a seguinte situação, tenho um servidor em PHP:
class Teste {
function obtemStatus(){
return 'ok';
}
}
$server = new SoapServer(null, array('uri'=>'http://test-uri/'));
$server->setClass('Teste');
$server->handle();
E um cliente em PHP também:
$options = array(
'location' => 'http://localhost/android/server/index.php',
'uri' => 'http://test-uri/'
);
$client = new SoapClient(null, $options);
echo $client->obtemStatus();
Isso está funcionando perfeitamente, mas agora quero utilizar esse serviço pelo Anndroid, tenho o seguinte código:
private String NAMESPACE = "urn://http://localhost";
private String METHOD_NAME = "obtemStatus";
private String URL = "localhost/android/client/index.php";
private String SOAP_ACTION = "localhost/android/client/index.php/obtemStatus";
// observação: antes do localhost está o http:// mas tive que tirar para postar aqui
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
SoapObject soap = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(soap);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
Está dando o seguinte erro:
09-22 20:59:27.501 9637-9637/com.anderson.soap D/HyLog﹕ I : /data/font/config/dfactpre.dat, No such file or directory (2)
09-22 20:59:27.661 9637-9637/com.anderson.soap D/AndroidRuntime﹕ Shutting down VM
09-22 20:59:27.661 9637-9637/com.anderson.soap E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.anderson.soap, PID: 9637
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anderson.soap/com.anderson.soap.MyActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:251)
at java.net.InetAddress.getAllByName(InetAddress.java:229)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:120)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:176)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
at com.anderson.soap.MyActivity.onCreate(MyActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
Alguém sabe oque pode ser?