簡體   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