将Android应用程序连接到Java web服务并将值传递给web服务
这是我想要连接的web服务,并从android虚拟机发送价值将Android应用程序连接到Java web服务并将值传递给web服务,java,android,web-services,mongodb,Java,Android,Web Services,Mongodb,这是我想要连接的web服务,并从android虚拟机发送价值 @WebService(serviceName = "EmoDeneme") @Stateless() public class EmoDeneme { /** * This is a sample web service operation */ @WebMethod(operationName = "hello") public String hello(@WebParam(name
@WebService(serviceName = "EmoDeneme")
@Stateless()
public class EmoDeneme {
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String name) {
String BargeName = "";
int BargeNo = 0;
String Starting = "";
String StartingDate = "";
try {
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("Barge");
DBCollection collection = db.getCollection("Emo");
DBObject match = new BasicDBObject("$match", new BasicDBObject("html.table.tbody.Barge.Name", name));
DBObject unwind = new BasicDBObject("$unwind", "$html.table.tbody.Barge");
AggregationOutput output = collection.aggregate(unwind, match);
for (DBObject result : output.results()) {
DBObject htmlObj = (DBObject) result.get("html");
DBObject tableObj = (DBObject) htmlObj.get("table");
DBObject tbodyObj = (DBObject) tableObj.get("tbody");
DBObject bargeObj = (DBObject) tbodyObj.get("Barge");
BargeName = (String) bargeObj.get("Name");
BargeNo = (Integer) bargeObj.get("Bargeno");
Starting = (String) bargeObj.get("Starting");
StartingDate = Starting.substring(0, 10);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
return "Terminal: " + " BargeName :" + BargeName + " BargeNo: " + BargeNo + " ETA
: " + StartingDate;
}
}
这是连接到web服务的Android代码
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "";
private static final String METHOD_NAME = "hello";
private static final String NAMESPACE = "http://mongodb.me.org/";
private static final String URL = "http://10.0.2.2:8080/EmoDeneme/EmoDeneme?WSDL";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread networkThread = new Thread() {
@Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String firstName = "Aan";
//Pass value for fname variable of the web service
PropertyInfo fnameProp =new PropertyInfo();
fnameProp.setName("name");//Define the variable name in the web service `method`
fnameProp.setValue(firstname);//Define value for fname variable
fnameProp.setType(String.class);//Define the type of the variable
request.addProperty(fnameProp);//Pass properties to the variable
SoapSerializationEnvelope envelope = new `SoapSerializationEnvelope(SoapEnvelope.VER11);`
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
final String str = response.toString();
runOnUiThread (new Runnable(){
public void run() {
TextView result;
result = (TextView)findViewById(R.id.textView1);//I have created a text view `& It's id is textView1`
result.setText(str);
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
}
}
它不工作,有人能帮我做什么我做错了。
谢谢。SOAP呼叫可能会变得一团糟。有一种更好的调用web服务的方法。查看以下两个视频: 在第一个视频中,作者使用命令行并指向wsdl生成java代码,在第二个视频中,他从生成的java代码调用函数 如果您想使用web服务做一些真正有趣的事情,请查看: 希望这有帮助
另外,如果你喜欢SOAP,有一个非常酷的工具:“不起作用”如何?你犯了什么错误?您是否收到错误或没有结果?我在web服务端收到空值,来自android的名称变量不将值传递给服务谢谢您的建议,我将关注它们。