简体   繁体   English

在本地连接到Web服务时,Android应用程序崩溃

[英]Android app crashes when connecting to webservice IN LOCAL

I got a problem connecting to my webservice from an Android client. 我从Android客户端连接到Web服务时遇到问题。 In my MainActivity class the program crashes at the line: 在我的MainActivity类中,程序在以下行崩溃:

transporte.call(accionSoap, sobre); 

I think that the problem could be something with the accionSoap, any help would be very apreciatted. 我认为accionSoap可能是问题所在,任何帮助都非常准确。

MainActivity: 主要活动:

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();

    }

}



}

Thank you! 谢谢!

NEW LOG UPDATED: 新日志已更新:

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

You have nullpointerexception on this line 您在此行上有nullpointerexception

Log.e("ERROR", e.getMessage());

So e.getMessage() is returning null. 因此e.getMessage()返回null。

So change it to 因此将其更改为

Log.e("ERROR", "" + e.getMessage());

or put a if(e.getMessage()!=null) before this line 或在该行之前放置if(e.getMessage()!=null)

Your problem seems to be on this line - probably line 76, and the only one in the method which seems like it uses Log.e 您的问题似乎在这行上-可能是第76行,并且该方法中似乎唯一使用Log.e的问题

Log.e("ERROR", e.getMessage());

Most likely the getMessage() method of your exception returns null, and the Log.e() call does not handle that. 您的异常的getMessage()方法很可能返回null,而Log.e()调用不会处理该错误。

Consider instead 考虑代替

Log.e("MainActivity", "ERROR");
e.printStackTrace();

Taking into account that the first argument to the Log methods is traditionally a "tag" identifying the logging component, rather than the "content" of the message. 考虑到Log方法的第一个参数传统上是标识日志记录组件的“标签”,而不是消息的“内容”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM