繁体   English   中英

将Android App连接到Java Web服务并将价值传递给Web服务

[英]Connect Android App to Java web services And pass Value to Web services

这是我要连接并从android虚拟机发送值的Web服务。

@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;
        }
    }

这是用于连接网络服务的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服务。 观看以下2个视频:

http://javabrains.koushik.org/2013/06/writing-web-service-client-stub.html http://javabrains.koushik.org/2013/06/writing-web-service-client-calling.html

在第一个视频中,作者使用命令行并指向wsdl生成了Java代码,在第二个视频中,他从生成的Java代码中调用了函数。

如果您想使用Web服务做一些非常有趣的事情,请查看:

http://cxf.apache.org/

希望这可以帮助。

PS。 如果您喜欢SOAP,那么会有一个非常酷的工具: http : //www.soapui.org/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM