没有从android中的webservice对象响应中恢复

时间:2015-11-29 15:22:08

标签: java c# android web-services android-studio

我在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中。

这是来自run:

logcat
 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

1 个答案:

答案 0 :(得分:0)

您遇到if a, ok := ln.Addr().(*net.TCPAddr); ok { fmt.Println("port", a.Port) } 。 这是让你知道的一种方法,你不应该从主线程访问网络,因为它可能会冻结用户界面并导致糟糕的体验。

通常可以使用AsyncTask在网络上执行操作。