免责声明:我是Android编程的新手:( 我在这里看到了类似的问题,但解决方案对我不起作用。我试图满足正确发布的问题的所有标准,所以请耐心等待。 我写了一个C#客户端来测试我的wcf服务(SimpleHTTPTransport),它可以在第二台计算机上通过WiFi工作正常,所以我知道没有防火墙问题。我的Android浏览器可以毫不费力地点击http://10.0.0.134:4444/DietService.svc?msdl。为匿名用户设置了服务,因此不需要登录信息。我已经检查了所有可能的参考资料,我可以在这里和网上的其他地方找到2天,没有任何帮助。我使用ksoap2 2.5.2并且无法正常工作。我甚至尝试过设置异步任务。我据称已经复制了#34;工作"代码仍然无法获得wcf服务的任何通信。在这一点上,我完全难过了。这是我的堆栈跟踪,代码和传输时的变量:
Stack Trace:
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:130)
diet.diet.MainActivity$AsyncCaller.doInBackground(MainActivity.java:113)
diet.diet.MainActivity$AsyncCaller.doInBackground(MainActivity.java:83)
android.os.AsyncTask$2.call(AsyncTask.java:295)
java.util.concurrent.FutureTask.run(FutureTask.java:237)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
java.lang.Thread.run(Thread.java:818)
CODE:
package diet.diet;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final Integer TIMEOUT = 60000;
private static final String METHOD_NAME = "GetFoodName";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.0.134:4444/DietService.svc";
final static String SOAP_ACTION = "http://tempuri.org/IDietService/GetFoodName";
Button btn_Request;
TextView tv_Reply;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
btn_Request = (Button) findViewById(R.id.btn_Request);
btn_Request.setOnClickListener(this);
tv_Reply = (TextView) findViewById(R.id.tv_Reply);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
tv_Reply.setText("Got it");
new AsyncCaller().execute();
}
private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
private String resultData;
ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
pdLoading.setMessage("Loading...");
pdLoading.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("id", "7");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
Log.i("CYBERON", "Check 1");
envelope.setOutputSoapObject(request);
Log.i("CYBERON", "Check 2");
HttpTransportSE myHttpTransport = new HttpTransportSE(URL, TIMEOUT);
myHttpTransport.debug = true;
try {
myHttpTransport.call(SOAP_ACTION, envelope);
}
catch (Exception e)
{
StackTraceElement[] stack = e.getStackTrace();
String Trace = "";
for(StackTraceElement line : stack)
{
Trace += line.toString();
Trace += "\n";
}
// Log.i("CYBERON", "Exception caught");
// Log.i("CYBERON", "RESPONSE: " + myHttpTransport.responseDump);
}
Log.i("CYBERON", "Check 3");
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
Log.i("CYBERON", "Check 4");
//to get the data
resultData = result.toString();
// 0 is the first object of data
Log.i("CYBERON", "Check 5");
} catch (Exception e) {
Log.i("CYBERON", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pdLoading.dismiss();
tv_Reply.setText(resultData);
}
}
}
这是我在myHttpTransport.call上打破时所能找到的:
this = {MainActivity$AsyncCaller@4404}
params = {Void[0]@4412}
request = {SoapObject@4413} "GetFoodName{id=7; }"
name = {String@4439} "GetFoodName"
namespace = {String@4440} "http://tempuri.org/"
properties = {Vector@4441} size = 1
attributes = {Vector@4442} size = 0
shadow$_klass_ = {Class@4392} "class org.ksoap2.serialization.SoapObject"
shadow$_monitor_ = -1920603187
envelope = {SoapSerializationEnvelope@4414}
addAdornments = true
classToQName = {Hashtable@4429} size = 5
dotNet = true
idMap = {Hashtable@4430} size = 0
implicitTypes = false
multiRef = null
properties = {Hashtable@4431} size = 0
qNameToClass = {Hashtable@4432} size = 5
bodyIn = null
bodyOut = {SoapObject@4413} "GetFoodName{id=7; }"
name = {String@4439} "GetFoodName"
namespace = {String@4440} "http://tempuri.org/"
properties = {Vector@4441} size = 1
attributes = {Vector@4442} size = 0
shadow$_klass_ = {Class@4392} "class org.ksoap2.serialization.SoapObject"
shadow$_monitor_ = -1920603187
enc = {String@4433} "http://schemas.xmlsoap.org/soap/encoding/"
count = 41
hashCode = -891961955
shadow$_klass_ = {Class@3919} "class java.lang.String"
shadow$_monitor_ = -1900538580
encodingStyle = null
env = {String@4434} "http://schemas.xmlsoap.org/soap/envelope/"
count = 41
hashCode = -1487983936
shadow$_klass_ = {Class@3919} "class java.lang.String"
shadow$_monitor_ = -2032362507
headerIn = null
headerOut = null
version = 110
xsd = {String@4435} "http://www.w3.org/2001/XMLSchema"
xsi = {String@4436} "http://www.w3.org/2001/XMLSchema-instance"
shadow$_klass_ = {Class@4395} "class org.ksoap2.serialization.SoapSerializationEnvelope"
shadow$_monitor_ = -1922298750
myHttpTransport = {HttpTransportSE@4415}
connection = null
debug = true
requestDump = null
responseDump = null
timeout = 60000
url = {String@4426} "http://10.0.0.134:4444/DietService.svc"
count = 38
hashCode = -193616475
shadow$_klass_ = {Class@3919} "class java.lang.String"
accessFlags = 8912913
classLoader = null
classSize = 666
clinitThreadId = 967
componentType = null
dexCache = {DexCache@4508}
dexCacheStrings = {String[41837]@4509}
dexClassDefIndex = 1399
dexTypeIndex = 1366
directMethods = 1877705544
iFields = 1875422136
ifTable = {Object[6]@4510}
name = {String@4473} "java.lang.String"
numDirectMethods = 39
numInstanceFields = 2
numReferenceInstanceFields = 0
numReferenceStaticFields = 2
numStaticFields = 4
numVirtualMethods = 55
objectSize = 0
primitiveType = 131072
referenceInstanceOffsets = 0
sFields = 1875422072
status = 10
superClass = {Class@2987} "class java.lang.Object"
verifyErrorClass = null
virtualMethods = 1877707104
vtable = null
shadow$_klass_ = {Class@735} "class java.lang.Class"
shadow$_monitor_ = 1263145082
shadow$_monitor_ = -1984993388
xmlVersionTag = {String@4427} ""
count = 0
hashCode = 0
shadow$_klass_ = {Class@3919} "class java.lang.String"
shadow$_monitor_ = -2067490254
shadow$_klass_ = {Class@4403} "class org.ksoap2.transport.HttpTransportSE"
shadow$_monitor_ = -2124603501
myHttpTransport.debug = true
答案 0 :(得分:0)
多少学习经历!在寻找更多答案时,我发现有一个较新的ksoap2库(2.6.0)所以我决定从2.5.2升级。当我这样做时,我注意到我使用的是2.5.2 WITHOUT依赖项。升级后,我的堆栈跟踪更详细,并指示权限错误。快速搜索网络让我检查
<uses-permission android:name="android.permission.INTERNET"/>
DUH!我忘了将它添加到我的AndroidManifest.xml文件中。答对了!连接已建立。现在我所要处理的只是合同不匹配。这需要将IDietService添加到 SOAP_ACTION 路径:
改变:
final static String SOAP_ACTION = "http://tempuri.org/GetFoodName";
为:
final static String SOAP_ACTION = "http://tempuri.org/IDietService/GetFoodName";
使用ksoap2库的WITH-DEPENDENCIES版本是解决方案的关键。我不知道版本升级是否有帮助。 我承认当我的catch块在使用ksoap2 2.5.2时发现e.getMessage()的null时我很困惑,但是我假设(是的,我知道)那就是它的方式是。底线 -
检查您的详细信息!