Android ksoap2错误
我使用WCF托管一个.svc服务从MySQL检索数据(都在本地主机上),我编写了一个类在Android上使用该服务,使用ksoap2版本3.1.1和IP 10.0.3.2让Geny Motion访问本地主机Android ksoap2错误,android,ksoap2,Android,Ksoap2,我使用WCF托管一个.svc服务从MySQL检索数据(都在本地主机上),我编写了一个类在Android上使用该服务,使用ksoap2版本3.1.1和IP 10.0.3.2让Geny Motion访问本地主机 package com.example.mapdemo; import java.io.IOException; import java.net.Proxy; import java.net.SocketTimeoutException; import org.ksoap2.SoapEn
package com.example.mapdemo;
import java.io.IOException;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class KetNoiWebService {
private static final String NAMESPACE = "http://tempuri.org/";
private static final String MAIN_REQUEST_URL = "http://10.0.3.2:7741/ToaDoServiceHost.svc";
private static final String SOAP_ACTION = "http://tempuri.org/IToaDo/GetAllToaDo";
private static final SoapSerializationEnvelope getSoapSerializationEnvelope(
SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private static final HttpTransportSE getHttpTransportSE() {
HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL, 60000);
return ht;
}
public static int getAllToaDoTuWebService() {
int count = 1;
String methodname = "GetAllToaDo";
SoapObject request = new SoapObject(NAMESPACE, methodname);
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
HttpTransportSE ht = getHttpTransportSE();
try {
ht.call(SOAP_ACTION, envelope);
SoapObject resultsString = (SoapObject) envelope
.bodyIn;
SoapObject result = (SoapObject)resultsString.getProperty(0);
count = result.getPropertyCount();
} catch (SocketTimeoutException t) {
t.printStackTrace();
} catch (IOException i) {
i.printStackTrace();
} catch (Exception q) {
q.printStackTrace();
}
return count;
}
}
在一个活动中给那个班打电话
package com.example.mapdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class KhuSanXuatActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_khusanxuat);
((TextView)findViewById(R.id.webservicecount))
.setText((KetNoiWebService.getAllToaDoTuWebService()));
}
}
活动_khusanxuat.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="San pham dang co trong kho:" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" - Bap u chua: " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100 "
android:textColor="@android:color/holo_blue_light"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cuon" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" - Phan ga: " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50 "
android:textColor="@android:color/holo_blue_light"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tan" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Du toan san luong:" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" - Bap u chua" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" + Ngay thu hoach du kien: " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_blue_light"
android:text="01/07/2014"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" + San luong du kien: " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200 "
android:textColor="@android:color/holo_blue_light"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cuon" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" - Phan ga" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" + Ngay thu hoach du kien: " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20/07/2014"
android:textColor="@android:color/holo_blue_light"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" + San luong du kien: " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="150 "
android:textColor="@android:color/holo_blue_light"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tan" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webservicecount"/>
</LinearLayout>
您需要使用来执行所有网络操作
您的网络操作可能需要很多时间,如果在主UI线程上完成,UI将没有响应。如果你的用户界面长时间冻结,应用程序可能会被操作系统杀死
因此,Android 4+强制使用后台线程执行网络操作
将执行网络活动的代码放入doInBackground()
中,并使用execute()
执行所有异步任务
以下是异步任务的外观:
private class SendMail extends AsyncTask<String, Integer, Void> {
protected void doInBackground() {
sendEmail();
}
protected void onProgressUpdate() {
//called when the background task makes any progress
}
protected void onPreExecute() {
//called before doInBackground() is started
}
protected void onPostExecute() {
//called after doInBackground() has finished
}
}
私有类SendMail扩展了异步任务{
受保护的void doInBackground(){
sendmail();
}
受保护的void onProgressUpdate(){
//在后台任务取得任何进展时调用
}
受保护的void onPreExecute(){
//在启动doInBackground()之前调用
}
受保护的void onPostExecute(){
//在doInBackground()完成后调用
}
}
您可以使用
new SendMail()在任何地方调用它代码>
您不应该在主线程(UI线程)中调用web服务。
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" name="ToaDoService" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://sang-pc:7741/ToaDoServiceHost.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://sang-pc:7741/ToaDoServiceHost.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://sang-pc:7741/ToaDoServiceHost.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/BogacaServices"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IToaDo_GetAllToaDo_InputMessage">
<wsdl:part name="parameters" element="tns:GetAllToaDo"/>
</wsdl:message>
<wsdl:message name="IToaDo_GetAllToaDo_OutputMessage">
<wsdl:part name="parameters" element="tns:GetAllToaDoResponse"/>
</wsdl:message>
<wsdl:portType name="IToaDo">
<wsdl:operation name="GetAllToaDo">
<wsdl:input wsaw:Action="http://tempuri.org/IToaDo/GetAllToaDo" message="tns:IToaDo_GetAllToaDo_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IToaDo/GetAllToaDoResponse" message="tns:IToaDo_GetAllToaDo_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IToaDo" type="tns:IToaDo">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetAllToaDo">
<soap:operation soapAction="http://tempuri.org/IToaDo/GetAllToaDo" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ToaDoService">
<wsdl:port name="BasicHttpBinding_IToaDo" binding="tns:BasicHttpBinding_IToaDo">
<soap:address location="http://sang-pc:7741/ToaDoServiceHost.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
您应该使用服务或AsyncTask来执行相同的操作
class DoInBack extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
String res = "";
try {
res=KetNoiWebService.getAllToaDoTuWebService();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, ""+result, 1).show();
((TextView)findViewById(R.id.webservicecount))
.setText(result);
}
}
就这样在询问之前,您是否阅读了错误日志并研究了一些信息?很抱歉,因为我还不了解Async,下次我会读得更清楚,谢谢。我希望您明白我的意思。试着首先检查问题的根源,而不是仅仅在网上发布。关于这一点和许多其他常见错误,存在大量重复的问题。祝你愉快。
class DoInBack extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
String res = "";
try {
res=KetNoiWebService.getAllToaDoTuWebService();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, ""+result, 1).show();
((TextView)findViewById(R.id.webservicecount))
.setText(result);
}
}
new DoInBack().execute("");