-1

Android クライアントから Web サービスへの接続で問題が発生しました。私の MainActivity クラスでは、プログラムは 66 行目でクラッシュします。

transporte.call(accionSoap, sobre); 

問題は accionSoap にある可能性があると思います。どんな助けも非常に感謝しています。

主な活動:

package com.example.ejemplosoap;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

//Napespace definido en el servicio web
private final String namespace = "http://ws.webapp.org/";
//Fichero de definicion del servicio web
private final String url = "http://10.0.2.2:8080/WebServiceServer/services/Hello";
//namespace + metodo
private final String accionSoap = "http://ws.webapp.org/sayHello";
//Metodo que queremos ejecutar en el servicio web
private final String Metodo = "sayHello";

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClickLogin(View view){
    try {

        System.out.println("1");
        // Modelo el request
        SoapObject request = new SoapObject(namespace, Metodo);
        //request.addProperty("name", "Juan!"); // Paso parametros al WS
        PropertyInfo primerParametro = new PropertyInfo ();
        System.out.println("2");
        primerParametro.setName("name");
        primerParametro.setValue("Juan");
        request.addProperty(primerParametro);
        System.out.println("3");
        // Modelo el Sobre
        SoapSerializationEnvelope sobre = new         SoapSerializationEnvelope(SoapEnvelope.VER11);
        //si esta hecho o no en puntoNet
        sobre.dotNet = false;
        sobre.setOutputSoapObject(request);
        System.out.println("4");

        // Modelo el transporte
        HttpTransportSE transporte = new HttpTransportSE(url);
        System.out.println("5");

        // Llamada
        transporte.call(accionSoap, sobre);
        System.out.println("6");

        // Resultado
       // SoapPrimitive resultado = (SoapPrimitive) sobre.getResponse();
        String cadenaDevuelta =(String) sobre.getResponse();
        Log.i("Resultado", cadenaDevuelta);


    } catch (Exception e) {
        Log.e("MainActivity", "ERROR");
                e.printStackTrace();

    }

}



}

LogCat:

10-17 10:26:10.221: I/System.out(721): 1
10-17 10:26:10.221: I/System.out(721): 2
10-17 10:26:10.231: I/System.out(721): 3
10-17 10:26:10.271: I/System.out(721): 4
10-17 10:26:10.281: I/System.out(721): 5
10-17 10:26:10.321: D/dalvikvm(721): GC_FOR_ALLOC freed 100K, 8% free 2588K/2808K, paused 38ms, total 42ms
10-17 10:26:10.381: E/MainActivity(721): ERROR
10-17 10:26:10.381: W/System.err(721): android.os.NetworkOnMainThreadException
10-17 10:26:10.391: W/System.err(721):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-17 10:26:10.391: W/System.err(721):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-17 10:26:10.391: W/System.err(721):  at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
10-17 10:26:10.391: W/System.err(721):  at libcore.io.IoBridge.connect(IoBridge.java:112)
10-17 10:26:10.391: W/System.err(721):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-17 10:26:10.401: W/System.err(721):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-17 10:26:10.401: W/System.err(721):  at java.net.Socket.connect(Socket.java:842)
10-17 10:26:10.401: W/System.err(721):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
10-17 10:26:10.411: W/System.err(721):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-17 10:26:10.411: W/System.err(721):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-17 10:26:10.421: W/System.err(721):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-17 10:26:10.421: W/System.err(721):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-17 10:26:10.421: W/System.err(721):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
10-17 10:26:10.421: W/System.err(721):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
10-17 10:26:10.421: W/System.err(721):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
10-17 10:26:10.431: W/System.err(721):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
10-17 10:26:10.431: W/System.err(721):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
10-17 10:26:10.441: W/System.err(721):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
10-17 10:26:10.451: W/System.err(721):  at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
10-17 10:26:10.451: W/System.err(721):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
10-17 10:26:10.462: W/System.err(721):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
10-17 10:26:10.462: W/System.err(721):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
10-17 10:26:10.471: W/System.err(721):  at com.example.ejemplosoap.MainActivity.onClickLogin(MainActivity.java:66)
10-17 10:26:10.481: W/System.err(721):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 10:26:10.481: W/System.err(721):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 10:26:10.491: W/System.err(721):  at android.view.View$1.onClick(View.java:3592)
10-17 10:26:10.491: W/System.err(721):  at android.view.View.performClick(View.java:4202)
10-17 10:26:10.501: W/System.err(721):  at android.view.View$PerformClick.run(View.java:17340)
10-17 10:26:10.512: W/System.err(721):  at android.os.Handler.handleCallback(Handler.java:725)
10-17 10:26:10.512: W/System.err(721):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 10:26:10.512: W/System.err(721):  at android.os.Looper.loop(Looper.java:137)
10-17 10:26:10.512: W/System.err(721):  at android.app.ActivityThread.main(ActivityThread.java:5039)
10-17 10:26:10.521: W/System.err(721):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 10:26:10.521: W/System.err(721):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 10:26:10.521: W/System.err(721):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-17 10:26:10.531: W/System.err(721):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-17 10:26:10.531: W/System.err(721):  at dalvik.system.NativeStart.main(Native Method)
10-17 10:26:10.612: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0
10-17 10:26:10.612: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0
10-17 10:26:10.681: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0
10-17 10:26:10.681: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0

ありがとうございました!

ShadishKumar ソリューションのログ:

10-17 11:29:26.734: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.744: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.744: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.754: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.774: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.774: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.875: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.884: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.904: I/System.out(767): 1
10-17 11:29:26.904: I/System.out(767): 2
10-17 11:29:26.904: I/System.out(767): 3
10-17 11:29:26.904: I/System.out(767): 4
10-17 11:29:26.904: I/System.out(767): 5
10-17 11:29:26.944: D/dalvikvm(767): GC_FOR_ALLOC freed 308K, 16% free 2600K/3080K, paused 32ms, total 32ms
10-17 11:29:26.955: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.955: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.975: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:26.975: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0
10-17 11:29:27.095: E/MainActivity(767): ERROR
10-17 11:29:27.095: W/System.err(767): java.io.IOException: HTTP request failed, HTTP status: 500
10-17 11:29:27.105: W/System.err(767):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:195)
10-17 11:29:27.105: W/System.err(767):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
10-17 11:29:27.105: W/System.err(767):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
10-17 11:29:27.105: W/System.err(767):  at com.example.ejemplosoap.MainActivity$ServiceCallTask.doInBackground(MainActivity.java:75)
10-17 11:29:27.105: W/System.err(767):  at com.example.ejemplosoap.MainActivity$ServiceCallTask.doInBackground(MainActivity.java:1)
10-17 11:29:27.115: W/System.err(767):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-17 11:29:27.115: W/System.err(767):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-17 11:29:27.115: W/System.err(767):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-17 11:29:27.125: W/System.err(767):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-17 11:29:27.125: W/System.err(767):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-17 11:29:27.134: W/System.err(767):  at java.lang.Thread.run(Thread.java:856)
4

3 に答える 3

0
public void onClickLogin(View view){    

Runnable runnable = new Runnable() {
                        public void run() {
                                 runOnThread();
                                };
                    Thread mythread = new Thread(runnable);
                    mythread.start();

                }
    }


//=====
    public void runOnThread(){

    System.out.println("1");
            // Modelo el request
            SoapObject request = new SoapObject(namespace, Metodo);
            //request.addProperty("name", "Juan!"); // Paso parametros al WS
            PropertyInfo primerParametro = new PropertyInfo ();
            System.out.println("2");
            primerParametro.setName("name");
            primerParametro.setValue("Juan");
            request.addProperty(primerParametro);
            System.out.println("3");
            // Modelo el Sobre
            SoapSerializationEnvelope sobre = new         SoapSerializationEnvelope(SoapEnvelope.VER11);
            //si esta hecho o no en puntoNet
            sobre.dotNet = false;
            sobre.setOutputSoapObject(request);
            System.out.println("4");

            // Modelo el transporte
            HttpTransportSE transporte = new HttpTransportSE(url);
            System.out.println("5");

            // Llamada
            transporte.call(accionSoap, sobre);
            System.out.println("6");

            // Resultado
           // SoapPrimitive resultado = (SoapPrimitive) sobre.getResponse();
            String cadenaDevuelta =(String) sobre.getResponse();
            Log.i("Resultado", cadenaDevuelta);


        } catch (Exception e) {
            Log.e("MainActivity", "ERROR");
                    e.printStackTrace();

        }

    }
于 2013-10-17T11:03:49.873 に答える
0

Web サービス呼び出しを操作するには、asynctask を使用する必要があります。

package com.example.ejemplosoap;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

//Napespace definido en el servicio web
private final String namespace = "http://ws.webapp.org/";
//Fichero de definicion del servicio web
private final String url = "http://10.0.2.2:8080/WebServiceServer/services/Hello";
//namespace + metodo
private final String accionSoap = "http://ws.webapp.org/sayHello";
//Metodo que queremos ejecutar en el servicio web
private final String Metodo = "sayHello";

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClickLogin(View view){

    new ServiceCallTask().execute();
}

class ServiceCallTask extends AsyncTask<String, String, String>
{

    @Override
    protected String doInBackground(String... params) {
        try {

            System.out.println("1");
            // Modelo el request
            SoapObject request = new SoapObject(namespace, Metodo);
            //request.addProperty("name", "Juan!"); // Paso parametros al WS
            PropertyInfo primerParametro = new PropertyInfo ();
            System.out.println("2");
            primerParametro.setName("name");
            primerParametro.setValue("Juan");
            request.addProperty(primerParametro);
            System.out.println("3");
            // Modelo el Sobre
            SoapSerializationEnvelope sobre = new         SoapSerializationEnvelope(SoapEnvelope.VER11);
            //si esta hecho o no en puntoNet
            sobre.dotNet = false;
            sobre.setOutputSoapObject(request);
            System.out.println("4");

            // Modelo el transporte
            HttpTransportSE transporte = new HttpTransportSE(url);
            System.out.println("5");

            // Llamada
            transporte.call(accionSoap, sobre);
            System.out.println("6");

            // Resultado
           // SoapPrimitive resultado = (SoapPrimitive) sobre.getResponse();
            String cadenaDevuelta =(String) sobre.getResponse();
            Log.i("Resultado", cadenaDevuelta);

        } catch (Exception e) {
            Log.e("MainActivity", "ERROR");
                    e.printStackTrace();
        }
        return null;
    }


}



}

asynctask の詳細については、こちらを参照してくださいhttp://developer.android.com/reference/android/os/AsyncTask.html

于 2013-10-17T10:48:03.380 に答える
0
android.os.NetworkOnMainThreadException

許可されていないネットワーク操作をメイン スレッドで実行しています。別のスレッドで操作を実行してください。

AsyncTaskメソッドで呼び出しを使用して実行するかdoInBackground、または を作成してメソッドでnew Thread()呼び出しを実行しますrun

于 2013-10-17T10:44:01.690 に答える