1. Are you ready for the Galaxy S20? Here is everything we know so far!

Connecting to Local PC web service.

Discussion in 'Android Development' started by Ulargrind, Jul 2, 2013.

  1. Ulargrind

    Ulargrind Lurker
    Thread Starter

    I have an android app that must connect to my local webservice with soap but I cant seem to get it working. This is the code I have.

    [HIGH]public void Hello()
    {
    String SOAP_ACTION = "http://tempuri.org/Hello";
    String METHOD_NAME = "Hello";
    String NAMESPACE = "http://tempuri.org/";
    String URL = "http://10.0.2.2/MainService/MainService.asmx";

    try {
    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(Request);

    HttpTransportSE transport= new HttpTransportSE(URL);

    transport.call(SOAP_ACTION, soapEnvelope);
    SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

    MyText.setText(resultString.toString());
    }catch(Exception ex) {
    MyText.setText("Error: " + ex.getMessage());
    }
    }[/HIGH]

    All this does is catch an exception with the message "Null".
    Note that I am not an android developer, this is my very first project.

    Can anyone tell me what I did wrong. Thanks is advance.

    P.S I did the same thing with Netbeans and regular Java and it works perfectly.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Ulargrind

    Ulargrind Lurker
    Thread Starter

    I found the problem. Turns out you cannot make network calls on the main thread.

    [HIGH]
    public void GetGenres()
    {
    SOAP_ACTION = "http://tempuri.org/getGenres";
    METHOD_NAME = "getGenres";
    NAMESPACE = "http://tempuri.org/";
    URL = "http://192.168.1.83/MainService/MainService.asmx?op=getGenres";

    Thread networkThread = new Thread() {
    @Override
    public void run() {
    try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    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() {
    Spinner TempList = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (MainActivity.this, android.R.layout.simple_list_item_1, str.split(","));
    TempList.setAdapter(adapter);
    }
    });
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    };
    networkThread.start();
    }
    [/HIGH]

    For anyone else struggling with this.
     
  3. shredcode

    shredcode Newbie

    I would recommend taking a look at AsyncTasks AsyncTask | Android Developers

    This is an easy way to do network and other items in the background and still have a hook into updating the UI (progress bar, ect)
     
Loading...
Similar Threads - Connecting Local web
  1. srajandroid
    Replies:
    2
    Views:
    299
  2. whantsaa
    Replies:
    3
    Views:
    305
  3. kumaranil13k
    Replies:
    0
    Views:
    241
  4. spharaus
    Replies:
    0
    Views:
    314
  5. Annersch
    Replies:
    6
    Views:
    485
  6. sargan
    Replies:
    4
    Views:
    399
  7. Alex2873645
    Replies:
    11
    Views:
    457
  8. paulb104
    Replies:
    3
    Views:
    612
  9. Andre0815
    Replies:
    0
    Views:
    1,744
  10. Poak
    Replies:
    1
    Views:
    1,522

Share This Page

Loading...