如何使用KSOAP将数据从Android应用程序发送到asp.net Web服务器 [英] How to send data from android app to asp.net web server using KSOAP

查看:47
本文介绍了如何使用KSOAP将数据从Android应用程序发送到asp.net Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从Android应用程序发送到网络服务器我的代码如下



将Ksoap.jar lib链接到我的android app java build之后路径



包com.ahmed.soapvideoservice;



 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.AndroidHttpTransport;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity扩展活动{
字符串 NAMESPACE = http://vladozver.org/;
字符串 METHOD_NAME = GetSumOfTwoInts;
字符串 SOAP_ACTION = http:// vladozver.org/GetSumOfTwoInts;
字符串 URL = http://本地主机:51305 / Service1.asmx的;
TextView tv;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv =(TextView)findViewById(R.id.textView1);
SoapObject Request = new SoapObject(NAMESPACE,METHOD_NAME);

PropertyInfo pi = new PropertyInfo();
pi.setName( Operand1);
pi.setValue( 2 );
pi.setType( int class );
Request.addProperty(pi);

PropertyInfo pi2 = new PropertyInfo();
pi2.setName( Operand2);
pi2.setValue( 5 );
pi2.setType( int class );
Request.addProperty(pi2);


SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true ;
envelope.setOutputSoapObject(Request);

AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
尝试
{
androidHttpTransport.call(SOAP_ACTION,envelope);
SoapPrimitive result =(SoapPrimitive)envelope.getResponse();
tv.setText( result = + result);
}
catch (例外e)
{
e.printStackTrace();

}


}

@Override
public boolean onCreateOptionsMenu(菜单菜单){
getMenuInflater()。inflate(R.menu.activity_main,menu);
return true ;
}
}





这是我的asp.net网络服务器

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;

命名空间 WebService1
{
/// < 摘要 >
/// Service1
的摘要说明
/// < / summary >
[WebService(Namespace = http://vladozver.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem( false )]
// 要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
// < span class =code-comment> [System.Web.Script.Services.ScriptService]
public Service1:System.Web.Services.WebService
{

[WebMethod]
public int GetSumOfTwoInts( int Operand1, int Operand2)
{
return Operand1 + Operand2;
}

}
}





我在android中遇到错误



 11-14 01:59:41.150:E / dalvikvm(338):找不到类'org.ksoap2.serialization。 SoapObject',从方法com.ahmed.soapvideoservice.MainActivity.onCreate引用
11-14 01:59:41.150:W / dalvikvm(338):VFY:无法解析新实例502(Lorg / ksoap2 /序列化/ SoapObject;)在Lcom / ahmed / soapvideoservice / MainActivity中;
11-14 01:59:41.150:D / dalvikvm(338):VFY:在0x0012替换操作码0x22
11-14 01:59:41.160:D / dalvikvm(338):VFY:死代码0x0014-0087在Lcom / ahmed / soapvideoservice / MainActivity; .onCreate(Landroid / os / Bundle;)V
11-14 01:59:41.230:D / AndroidRuntime(338):关闭VM
11-14 01:59:41.230:W / dalvikvm(338):threadid = 1:线程退出未捕获异常(组= 0x40015560)
11-14 01:59:41.240:E / AndroidRuntime(338) :FATAL EXCEPTION:main
11-14 01:59:41.240:E / AndroidRuntime(338):java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject
11-14 01:59:41.240 :E / AndroidRuntime(338):at com.ahmed.soapvideoservice.MainActivity.onCreate(MainActivity.java:29)
11-14 01:59:41.240:E / AndroidRuntime(338):at android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-14 01:59:41.240:E / AndroidRuntime(338):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11 -14 01:59:41.240:E / Android运行时(338):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-14 01:59:41.240:E / AndroidRuntime(338):在android.app.ActivityThread.access $ 1500( ActivityThread.java:117)
11-14 01:59:41.240:E / AndroidRuntime(338):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
11-14 01:59:41.240:E / AndroidRuntime(338):在android.os.Handler.dispatchMessage(Handler.java:99)
11-14 01:59:41.240:E / AndroidRuntime(338):at android .os.Looper.loop(Looper.java:123)
11-14 01:59:41.240:E / AndroidRuntime(338):在android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 01:59:41.240:E / AndroidRuntime(338):at java.lang.reflect.Method.invokeNative(Native Method)
11-14 01:59:41.240:E / AndroidRuntime(338) ):at java.lang.reflect.Method.invoke(Method.java:507)
11-14 01:59:41.240:E / AndroidRuntime(338):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 01:59:41.240:E / A ndroidRuntime(338):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 01:59:41.240:E / AndroidRuntime(338):at dalvik.system.NativeStart .main(原生方法)





固定代码块[/ edit]

解决方案

1500(ActivityThread.java:117)
11-14 01:59:41.240:E / AndroidRuntime(338):at android.app.ActivityThread

H.handleMessage(ActivityThread.java:931)
11-14 01:59:41.240:E / AndroidRuntime(338):在android.os.Handler.dispatchMessage(Handler.java) :99)
11-14 01:59:41.240:E / AndroidRuntime(338):在android.os.Looper.loop(Looper.java:123)
11-14 01:59:41.240 :E / AndroidRuntime(338):在android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 01:59:41.240:E / AndroidRuntime(338):at java.lang.reflect。 Method.invokeNative(Native Method)
11-14 01:59:41.240:E / AndroidRuntime(338):at java.lang.reflect.Method.invoke(Method.java:507)
11-14 01:59:41.240:E / AndroidRuntime(338):at com.android.internal.os.ZygoteInit


MethodAndArgsCaller.run(ZygoteInit.java:839 )
11-14 01:59:41.240:E / AndroidRuntime(338):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 01:59 :41.240:E / AndroidRuntime(338):at dalvik.system.NativeStart.main(Native Method)





固定代码块[/ edit]


I want to send data from the android app to the web server my code is as follows

after link the Ksoap.jar lib to my android app java build path

package com.ahmed.soapvideoservice;

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.AndroidHttpTransport;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	String NAMESPACE = "http://vladozver.org/";
    String METHOD_NAME = "GetSumOfTwoInts";
    String SOAP_ACTION = "http://vladozver.org/GetSumOfTwoInts";
    String URL = "http://localhost:51305/Service1.asmx";
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.textView1);
        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
	       
	       PropertyInfo pi = new PropertyInfo();
	       pi.setName("Operand1");
	       pi.setValue(2);
	       pi.setType(int.class);
	       Request.addProperty(pi);

	       PropertyInfo pi2 = new PropertyInfo();
	       pi2.setName("Operand2");
	       pi2.setValue(5);
	       pi2.setType(int.class);
	       Request.addProperty(pi2);
	       
	       
	       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	       envelope.dotNet=true;
	       envelope.setOutputSoapObject(Request);
	       
	       AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	       try
	       {
	           androidHttpTransport.call(SOAP_ACTION, envelope);
	           SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
	           tv.setText("result = "+result);
	       }
	       catch(Exception e)
	       {
	           e.printStackTrace();
	           
	       }

	       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}



and here is my asp.net web server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://vladozver.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public int GetSumOfTwoInts(int Operand1, int Operand2)
        {
            return Operand1 + Operand2;
        }

    }
}



I got an error in the android

11-14 01:59:41.150: E/dalvikvm(338): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.ahmed.soapvideoservice.MainActivity.onCreate
11-14 01:59:41.150: W/dalvikvm(338): VFY: unable to resolve new-instance 502 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/ahmed/soapvideoservice/MainActivity;
11-14 01:59:41.150: D/dalvikvm(338): VFY: replacing opcode 0x22 at 0x0012
11-14 01:59:41.160: D/dalvikvm(338): VFY: dead code 0x0014-0087 in Lcom/ahmed/soapvideoservice/MainActivity;.onCreate (Landroid/os/Bundle;)V
11-14 01:59:41.230: D/AndroidRuntime(338): Shutting down VM
11-14 01:59:41.230: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-14 01:59:41.240: E/AndroidRuntime(338): FATAL EXCEPTION: main
11-14 01:59:41.240: E/AndroidRuntime(338): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
11-14 01:59:41.240: E/AndroidRuntime(338): 	at com.ahmed.soapvideoservice.MainActivity.onCreate(MainActivity.java:29)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.os.Looper.loop(Looper.java:123)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at java.lang.reflect.Method.invoke(Method.java:507)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at dalvik.system.NativeStart.main(Native Method)



[edit]fixed code blocks[/edit]

解决方案

1500(ActivityThread.java:117) 11-14 01:59:41.240: E/AndroidRuntime(338): at android.app.ActivityThread


H.handleMessage(ActivityThread.java:931) 11-14 01:59:41.240: E/AndroidRuntime(338): at android.os.Handler.dispatchMessage(Handler.java:99) 11-14 01:59:41.240: E/AndroidRuntime(338): at android.os.Looper.loop(Looper.java:123) 11-14 01:59:41.240: E/AndroidRuntime(338): at android.app.ActivityThread.main(ActivityThread.java:3683) 11-14 01:59:41.240: E/AndroidRuntime(338): at java.lang.reflect.Method.invokeNative(Native Method) 11-14 01:59:41.240: E/AndroidRuntime(338): at java.lang.reflect.Method.invoke(Method.java:507) 11-14 01:59:41.240: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit


MethodAndArgsCaller.run(ZygoteInit.java:839) 11-14 01:59:41.240: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 11-14 01:59:41.240: E/AndroidRuntime(338): at dalvik.system.NativeStart.main(Native Method)



[edit]fixed code blocks[/edit]


这篇关于如何使用KSOAP将数据从Android应用程序发送到asp.net Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆