与WebServices的C#kSOAP2通信 [英] kSOAP2 communication with webservices C#
问题描述
我想送于Android用户的登录凭据在通过Visual Studio我的本地主机上运行一个C#Web服务。我已经使用KSOAP2的SOAP通信,想送登录信息到Web服务,并在UI打印。我用的AsyncTask但我一直在这里得到下面的错误公布。
<一个href=\"http://stackoverflow.com/questions/16291793/ksoap-2-communication-sending-the-asynctask-result-to-ui-thread-and-print\">ksoap 2通信 - 发送AsyncTask的结果UI线程和打印
能否有人请帮助我在哪里出了错?????
公共类MainActivity延伸活动{ 联合国的EditText,PW;
TextView的电视;
按键测试;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮测试=(按钮)findViewById(R.id.login);
UN =(EditText上)findViewById(R.id.et_un);
PW =(EditText上)findViewById(R.id.et_pw);
test.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
最后弦乐NAMESPACE =http://sparking.org/login;
最后弦乐METHOD_NAME =登录;
最后弦乐SOAP_ACTION =http://sparking.org/login;
最终字符串URL =HTTP://本地主机:63734 / service.asmx SoapObject要求=新SoapObject空间(namespace,METHOD_NAME); SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = TRUE;
envelope.setOutputSoapObject(请求);
HttpTransportSE运输=新HttpTransportSE(URL); 尝试{
transport.call(SOAP_ACTION,信封);
SoapPrimitive resultstring =(SoapPrimitive)envelope.getResponse(); } 赶上(例外五)
{
e.printStackTrace();
} }
});
}
}
错误信息:
5月4日至30日:15:09.215:D / gralloc_goldfish(2997):无仿真模拟GPU检测。
5月4日至三十〇日:15:29.105:W / System.err的(2997):android.os.NetworkOnMainThreadException
5月4日至三十〇日:15:29.115:W / System.err的(2997):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
5月4日至三十〇日:15:29.115:W / System.err的(2997):在java.net.InetAddress.lookupHostByName(InetAddress.java:385)
5月4日至三十〇日:15:29.115:W / System.err的(2997):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
5月4日至三十〇日:15:29.125:W / System.err的(2997):在java.net.InetAddress.getAllByName(InetAddress.java:214)
5月4日至三十〇日:15:29.125:W / System.err的(2997):在libcore.net.http.HttpConnection&LT;&初始化GT;(HttpConnection.java:70)
5月4日至三十〇日:15:29.135:W / System.err的(2997):在libcore.net.http.HttpConnection&LT;&初始化GT;(HttpConnection.java:50)
5月4日至三十〇日:15:29.135:W / System.err的(2997):在libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340)
5月4日至三十〇日:15:29.135:W / System.err的(2997):在libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
5月4日至三十〇日:15:29.135:W / System.err的(2997):在libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
5月4日至三十〇日:15:29.135:W / System.err的(2997):在libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
5月4日至三十〇日:15:29.135:W / System.err的(2997):在libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
5月4日至三十〇日:15:29.155:W / System.err的(2997):在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
5月4日至三十〇日:15:29.155:W / System.err的(2997):在libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
5月4日至三十〇日:15:29.165:W / System.err的(2997):在libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
5月4日至三十〇日:15:29.165:W / System.err的(2997):在org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
5月4日至三十〇日:15:29.175:W / System.err的(2997):在org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
5月4日至三十〇日:15:29.175:W / System.err的(2997):在com.example.cmpe220.MainActivity $ 1.onClick(MainActivity.java:73)
5月4日至三十〇日:15:29.185:W / System.err的(2997):在android.view.View.performClick(View.java:4204)
5月4日至三十〇日:15:29.195:W / System.err的(2997):在android.view.View $ PerformClick.run(View.java:17355)
5月4日至三十〇日:15:29.205:W / System.err的(2997):在android.os.Handler.handleCallback(Handler.java:725)
5月4日至三十〇日:15:29.215:W / System.err的(2997):在android.os.Handler.dispatchMessage(Handler.java:92)
5月4日至三十〇日:15:29.215:W / System.err的(2997):在android.os.Looper.loop(Looper.java:137)
5月4日至三十〇日:15:29.225:W / System.err的(2997):在android.app.ActivityThread.main(ActivityThread.java:5041)
5月4日至三十〇日:15:29.225:W / System.err的(2997):在java.lang.reflect.Method.invokeNative(本机方法)
5月4日至三十〇日:15:29.235:W / System.err的(2997):在java.lang.reflect.Method.invoke(Method.java:511)
5月4日至三十〇日:15:29.235:W / System.err的(2997):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
5月4日至三十〇日:15:29.249:W / System.err的(2997):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
5月4日至三十〇日:15:29.249:W / System.err的(2997):在dalvik.system.NativeStart.main(本机方法)
5月4日至三十〇日:17:04.375:D / dalvikvm(2997):GC_CONCURRENT释放175K,12%免费2579K / 2900K,暂停76ms + 8ms的,总229ms
清单:
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.cmpe220
安卓版code =1
机器人:=的versionName1.0&GT; &LT;用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =14/&GT;
&LT;允许机器人:名字=android.permission.INTERNET对&GT;&LT; /许可&GT; &lt;应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme&GT;
&LT;活动
机器人:名字=com.example.220.MainActivity
机器人:标签=@字符串/ APP_NAME&GT;
&所述;意图滤光器&gt;
&lt;作用机器人:名字=android.intent.action.MAIN/&GT; &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
&所述; /意图滤光器&gt;
&LT; /活性GT;
&LT; /用途&gt; &LT; /清单&GT;
当您试图从你的主线程通过HTTP传达错误。
刚刚宣布的请求对象之前添加这些2号线:
StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建立()。
StrictMode.setThreadPolicy(政策);
I am trying to send the login credentials from android user to a C# web service running in my local host through visual studio. I have used KSOAP2 for the SOAP communication and wanted to send the login info to the web service and print it in the UI. I have used Asynctask but I have been getting the below error posted here.
ksoap 2 communication - sending the asynctask result to UI thread and print
Can some one please help where I went wrong ?????
public class MainActivity extends Activity {
EditText un,pw;
TextView tv;
Button test;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test=(Button) findViewById(R.id.login);
un = (EditText) findViewById(R.id.et_un);
pw = (EditText) findViewById(R.id.et_pw);
test.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final String NAMESPACE = "http://sparking.org/login";
final String METHOD_NAME = "login";
final String SOAP_ACTION = "http://sparking.org/login";
final String URL = "http://localhost:63734/service.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.call(SOAP_ACTION, envelope);
SoapPrimitive resultstring = (SoapPrimitive)envelope.getResponse();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
ERROR MESSAGES:
04-30 05:15:09.215: D/gralloc_goldfish(2997): Emulator without GPU emulation detected.
04-30 05:15:29.105: W/System.err(2997): android.os.NetworkOnMainThreadException
04-30 05:15:29.115: W/System.err(2997): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-30 05:15:29.115: W/System.err(2997): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-30 05:15:29.115: W/System.err(2997): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-30 05:15:29.125: W/System.err(2997): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-30 05:15:29.125: W/System.err(2997): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-30 05:15:29.135: W/System.err(2997): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-30 05:15:29.135: W/System.err(2997): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-30 05:15:29.135: W/System.err(2997): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-30 05:15:29.135: W/System.err(2997): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-30 05:15:29.135: W/System.err(2997): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-30 05:15:29.135: W/System.err(2997): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
04-30 05:15:29.155: W/System.err(2997): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-30 05:15:29.155: W/System.err(2997): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-30 05:15:29.165: W/System.err(2997): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-30 05:15:29.165: W/System.err(2997): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
04-30 05:15:29.175: W/System.err(2997): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
04-30 05:15:29.175: W/System.err(2997): at com.example.cmpe220.MainActivity$1.onClick(MainActivity.java:73)
04-30 05:15:29.185: W/System.err(2997): at android.view.View.performClick(View.java:4204)
04-30 05:15:29.195: W/System.err(2997): at android.view.View$PerformClick.run(View.java:17355)
04-30 05:15:29.205: W/System.err(2997): at android.os.Handler.handleCallback(Handler.java:725)
04-30 05:15:29.215: W/System.err(2997): at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 05:15:29.215: W/System.err(2997): at android.os.Looper.loop(Looper.java:137)
04-30 05:15:29.225: W/System.err(2997): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-30 05:15:29.225: W/System.err(2997): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 05:15:29.235: W/System.err(2997): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 05:15:29.235: W/System.err(2997): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-30 05:15:29.249: W/System.err(2997): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-30 05:15:29.249: W/System.err(2997): at dalvik.system.NativeStart.main(Native Method)
04-30 05:17:04.375: D/dalvikvm(2997): GC_CONCURRENT freed 175K, 12% free 2579K/2900K, paused 76ms+8ms, total 229ms
MANIFEST:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cmpe220"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<permission android:name="android.permission.INTERNET"></permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.220.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
That error is when you try to communicate through http from your main thread.
Just add those 2 lines before declaring the 'request' object:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
这篇关于与WebServices的C#kSOAP2通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!