在Android中没有从Web服务对象响应中检索到任何内容 - java

我已经在C#中创建了此Web服务:

  public class Category
    {
        public int CategoryId;
        public String Name;
       public  String Description;


    }


    [WebMethod]
    public Category GetCategoryById(int id)
    {
        Category C = new Category();

        if (id == 1)
        {
            C.CategoryId = 1;
            C.Name = "bag";
            C.Description = "white";

        }
            return C;

    }

我想在我的Android应用程序中调用它,因此我尝试这样做:

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

     WebServiceCallExample();

}

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


public void WebServiceCallExample()
{
    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME = "GetCategoryById";
    String SOAP_ACTION = "http://tempuri.org/GetCategoryById";
    String URL = "http://192.168.1.11:90/myWebService.asmx";

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

    PropertyInfo pi = new PropertyInfo();
    pi.setName("id");
    pi.setValue(1);
    pi.setType(Integer.class);
    Request.addProperty(pi);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(Request);

    envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
    HttpTransportSE  androidHttpTransport = new HttpTransportSE (URL);

    try
    {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject)envelope.getResponse();
        C.CategoryId =  Integer.parseInt(response.getProperty(0).toString());
        C.Name =  response.getProperty(1).toString();
        C.Description = (String) response.getProperty(2).toString();
        TextView tv = (TextView)findViewById(R.id.TextView01);
        tv.setText("CategoryId: " +C.CategoryId + " Name: " + C.Name + " Description " + C.Description);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

Category.java:

public class Category implements KvmSerializable
{
public int CategoryId;
public String Name;
public String Description;

public Category(){}


public Category(int categoryId, String name, String description) {

    CategoryId = categoryId;
    Name = name;
    Description = description;
}


public Object getProperty(int arg0) {

    switch(arg0)
    {
        case 0:
            return CategoryId;
        case 1:
            return Name;
        case 2:
            return Description;
    }

    return null;
}

public int getPropertyCount() {
    return 3;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch(index)
    {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "CategoryId";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Name";
            break;
        case 2:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Description";
            break;
        default:break;
    }
}

public void setProperty(int index, Object value) {
    switch(index)
    {
        case 0:
            CategoryId = Integer.parseInt(value.toString());
            break;
        case 1:
            Name = value.toString();
            break;
        case 2:
            Description = value.toString();
            break;
        default:
            break;
    }
}
}

但是,当我运行它时,它什么也没有做,没有错误,我也测试了Web服务。工作正常。

错误必须在java中。

这是运行中的日志:

 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest E/Zygote: MountEmulatedStorage()
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest E/Zygote: v2
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest I/libpersona: KNOX_SDCARD checking this for 10356
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest I/libpersona: KNOX_SDCARD not a persona
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G925I_5.0.2 ver=22
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G925I_5.1.1_0039
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest E/Zygote: accessInfo : 0
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
 11-29 18:27:54.391 16392-16392/com.example.hatim.catogerytest I/art: Late-enabling -Xcheck:jni
 11-29 18:27:54.411 16392-16392/com.example.hatim.catogerytest D/TimaKeyStoreProvider: TimaSignature is unavailable
 11-29 18:27:54.411 16392-16392/com.example.hatim.catogerytest D/ActivityThread: Added TimaKeyStore provider
 11-29 18:27:54.451 16392-16392/com.example.hatim.catogerytest I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
 11-29 18:27:54.461 16392-16392/com.example.hatim.catogerytest I/InjectionManager: Inside getClassLibPath caller 
 11-29 18:27:54.471 16392-16392/com.example.hatim.catogerytest D/InjectionManager: InjectionManager
 11-29 18:27:54.471 16392-16392/com.example.hatim.catogerytest D/InjectionManager: fillFeatureStoreMap com.example.hatim.catogerytest
 11-29 18:27:54.471 16392-16392/com.example.hatim.catogerytest I/InjectionManager: Constructor com.example.hatim.catogerytest, Feature store :{}
 11-29 18:27:54.471 16392-16392/com.example.hatim.catogerytest I/InjectionManager: featureStore :{}
 11-29 18:27:54.511 16392-16392/com.example.hatim.catogerytest D/SecWifiDisplayUtil: Metadata value : SecSettings2
 11-29 18:27:54.541 16392-16392/com.example.hatim.catogerytest D/PhoneWindow: *FMB* installDecor mIsFloating : false
 11-29 18:27:54.541 16392-16392/com.example.hatim.catogerytest D/PhoneWindow: *FMB* installDecor flags : -2139029248
 11-29 18:27:54.631 16392-16392/com.example.hatim.catogerytest I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
 11-29 18:27:54.631 16392-16392/com.example.hatim.catogerytest I/System.out: (HTTPLog)-Static: isShipBuild true
 11-29 18:27:54.631 16392-16392/com.example.hatim.catogerytest I/System.out: (HTTPLog)-Thread-1-250613459: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
 11-29 18:27:54.631 16392-16392/com.example.hatim.catogerytest I/System.out: (HTTPLog)-Thread-1-250613459: SMARTBONDING_FEATURE_ENABLED is true
 11-29 18:27:54.631 16392-16392/com.example.hatim.catogerytest I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
 11-29 18:27:54.631 16392-16392/com.example.hatim.catogerytest I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
 11-29 18:27:54.631 16392-16392/com.example.hatim.catogerytest I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
 11-29 18:27:54.641 16392-16392/com.example.hatim.catogerytest W/System.err: android.os.NetworkOnMainThreadException
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at java.net.Socket.connect(Socket.java:882)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:1198)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:395)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:298)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:399)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:110)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:221)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:185)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.example.hatim.catogerytest.MainActivity.WebServiceCallExample(MainActivity.java:84)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.example.hatim.catogerytest.MainActivity.onCreate(MainActivity.java:30)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.app.Activity.performCreate(Activity.java:6500)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3078)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3224)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.os.Looper.loop(Looper.java:145)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6843)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest D/Activity: performCreate Call Injection manager
 11-29 18:27:54.651 16392-16392/com.example.hatim.catogerytest I/InjectionManager: dispatchOnViewCreated > Target : com.example.hatim.catogerytest.MainActivity isFragment :false
 11-29 18:27:54.671 16392-16431/com.example.hatim.catogerytest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
 11-29 18:27:54.681 16392-16392/com.example.hatim.catogerytest D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
 11-29 18:27:54.681 16392-16392/com.example.hatim.catogerytest D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
 11-29 18:27:54.711 16392-16392/com.example.hatim.catogerytest D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered! 
 11-29 18:27:54.801 16392-16431/com.example.hatim.catogerytest D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
 11-29 18:27:54.821 16392-16431/com.example.hatim.catogerytest I/OpenGLRenderer: Initialized EGL, version 1.4
 11-29 18:27:54.831 16392-16431/com.example.hatim.catogerytest I/OpenGLRenderer: HWUI protection enabled for context ,  &this =0x7f88027c40 ,&mEglDisplay = 1 , &mEglConfig = -2012740944 
 11-29 18:27:54.831 16392-16431/com.example.hatim.catogerytest D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
 11-29 18:27:54.831 16392-16431/com.example.hatim.catogerytest D/OpenGLRenderer: Enabling debug mode 0
 11-29 18:27:54.831 16392-16431/com.example.hatim.catogerytest D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
 11-29 18:27:55.031 16392-16392/com.example.hatim.catogerytest I/InjectionManager: dispatchCreateOptionsMenu :com.example.hatim.catogerytest.MainActivity
 11-29 18:27:55.031 16392-16392/com.example.hatim.catogerytest I/InjectionManager: dispatchPrepareOptionsMenu :com.example.hatim.catogerytest.MainActivity
 11-29 18:27:55.091 16392-16392/com.example.hatim.catogerytest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2ea2c52f time:147399188

参考方案

您遇到android.os.NetworkOnMainThreadException
这是Android告知您的方法,您不应从主线程访问网络,因为它可能会冻结UI并导致不良体验。

通常,AsyncTask可用于在网络上执行操作。

Java BlackBerry-如何使用GET方法正确调用php脚本? - java

大家好吗?我正在为BlackBerry Curve 8520做一个非常简单的游戏,我需要从服务器获取排名。为了获取此数据,Web程序员给了我php文件,该文件从数据库获取数据并将其作为动态xml文件返回。我的问题是:如何使用Java代码加载该php文件?如何将get参数传递给该请求?我如何解析从php脚本检索到的xml?提前致谢!弗朗西斯科 参考方案 您这里…

Java:线程池如何将线程映射到可运行对象 - java

试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …

JAVA:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…