简体   繁体   中英

Connect Android App to Java web services And pass Value to Web services

This is the web services that i want to connect and send value from android virtual machine.

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

This is the Android Code to connect to web service

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();
      }
     }

It does not work, can someone help me please what i do wrong. Thank you.

SOAP calls can get messy. There is a better way of calling a web service. Check out the following 2 videos:

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

In the first video the author generated java code by using the command line and pointing to a wsdl and in the second video he calls functions from the generated java code.

And if you want to do some really interesting things with web services check out:

http://cxf.apache.org/

Hope this helps.

PS. there is a really cool tool if you are into SOAP: http://www.soapui.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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