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

Connect to WCF service through android

Discussion in 'Android Development' started by Atkinson_88, Dec 9, 2013.

  1. Atkinson_88

    Atkinson_88 Newbie
    Thread Starter

    Hello, Im trying to connect to a WCF service hosted on one of my websites, this WCF Service returns "Hello World" nothing exciting i've been followinf this
    Calling a RESTful web service from an Android application | vrsbrazil

    Below is my Request task

    [HIGH]
    package Methods;

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.StatusLine;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;

    import android.os.AsyncTask;

    public class RequestTask extends AsyncTask<String, String, String>{

    public String rarr;

    @Override
    protected String doInBackground(String... uri) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;

    rarr = "Hello My Friend";

    try {
    //"http://www.mycashadvance.co.uk/WCFService/HelloWorld"
    response = httpclient.execute(new HttpGet(uri[0]));
    StatusLine statusLine = response.getStatusLine();

    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    response.getEntity().writeTo(out);
    out.close();
    rarr = "Inside status";
    //responseString = out.toString();
    rarr = out.toString();
    } else{
    rarr = "first Error";
    //Closes the connection.
    response.getEntity().getContent().close();
    throw new IOException(statusLine.getReasonPhrase());

    }
    } catch (ClientProtocolException e) {
    rarr = "Second Error";
    e.printStackTrace();
    } catch (IOException e) {
    rarr = "Third Error";
    e.printStackTrace();
    }

    return rarr;
    }

    @Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);
    //Do anything with response..
    }



    }

    [/HIGH]
    below is how I call it, i then try to assign task.rarr to a toast but its always blank.

    [HIGH]
    RequestTask task = (RequestTask) new RequestTask().execute("http://www.mycashadvance.co.uk/WCFService/HelloWorld");
    [/HIGH]
    Toast
    [HIGH]
    Toast.makeText(MainActivity.this, task.rarr, Toast.LENGTH_SHORT).show();
    [/HIGH]

    I've added

    [HIGH]
    <uses-permission android:name="android.permission.INTERNET" />
    [/HIGH]

    to the manifest

    Any help would be appreciated.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Rukbat

    Rukbat Extreme Android User

    Have you tried calling the same service in exactly the same way with, say, PHP or Python or some other method? I've pulled out a lot of hair trying to fix a bug in a client when it was the server that had the bug.
     
    alostpacket likes this.
  3. Atkinson_88

    Atkinson_88 Newbie
    Thread Starter

    Hello,

    I haven't as of yet no,

    I did try connecting to this wcf service via another. Net application and that was a succes, someone has told me to,move the toast inside on the onPOST method which gets called after the request task but when , put the toast there it asks for a context which i Assumed was a class so I placeed Requesttask there and it didn'. Like it someone then said use entityulis.tostring and well that's where got confused, reason for this is because rarr cant be read but by puttig it inside the onPOST method I'd be able to read it
     
  4. alostpacket

    alostpacket Over Macho Grande?

    Try something like this. Note we get the String result from the actual response Entity

    And, yes you should return the string from doInBackground, and put your toast in onPostExecute

    [HIGH] public String connect(String url)
    {
    String result = null;
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    params.setParameter(CoreProtocolPNames.USER_AGENT, "Atkinson_88s.awesome.user.agent");

    DefaultHttpClient httpclient = new DefaultHttpClient(params);

    httpcall = new HttpGet( url );

    HttpResponse response;

    try
    {
    response = httpclient.execute( httpcall );

    Log.i( TAG, response.getStatusLine().toString() );

    HttpEntity entity = response.getEntity();

    if ( entity != null )
    {
    result = EntityUtils.toString( entity );
    }

    }
    catch ( ClientProtocolException e )
    {
    e.printStackTrace();
    }
    catch ( IOException e )
    {
    e.printStackTrace();
    }

    return result;
    }[/HIGH]
     
  5. Rukbat

    Rukbat Extreme Android User

    This is one if the reasons I try to not be associated with (and it's not a typo) dot not.
     
    alostpacket likes this.
  6. alostpacket

    alostpacket Over Macho Grande?

    lol. Of course all my Ruby programming friends would say the same of Java :p
     
  7. Rukbat

    Rukbat Extreme Android User

    Ruby? Is that the color the PHP manual was originally? (There's a server-side language not called PHP?)
     
  8. alostpacket

    alostpacket Over Macho Grande?

    get. out.


    :p


    I used to dabble in php a long time ago (before it had classes heh) but i would much prefer java or ruby for server stuff.

    Anything that thought it was a good idea to follow the lead of perl needs to die a quick death!


    /also apologies to the OP we're having a bit of good fun language religous war in your thread :)
     
  9. Atkinson_88

    Atkinson_88 Newbie
    Thread Starter

    Thanks for the response(s) LAst night i placed the toast inside the onPost method and below is what I recieved from the stacktrace

    12-09 14:39:09.274: E/AndroidRuntime(1613): FATAL EXCEPTION: main 12-09 14:39:09.274: E/AndroidRuntime(1613): Process: com.example.newapplication, PID: 1613 12-09 14:39:09.274: E/AndroidRuntime(1613): java.lang.NullPointerException 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.widget.Toast.(Toast.java:93) 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.widget.Toast.makeText(Toast.java:241) 12-09 14:39:09.274: E/AndroidRuntime(1613): at Methods.RequestTask.onPostExecute(RequestTask.java:64) 12-09 14:39:09.274: E/AndroidRuntime(1613): at Methods.RequestTask.onPostExecute(RequestTask.java:1) 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.os.AsyncTask.finish(AsyncTask.java:632) 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.os.AsyncTask.access$600(AsyncTask.java:177) 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.os.Handler.dispatchMessage(Handler.java:102) 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.os.Looper.loop(Looper.java:137) 12-09 14:39:09.274: E/AndroidRuntime(1613): at android.app.ActivityThread.main(ActivityThread.java:4998) 12-09 14:39:09.274: E/AndroidRuntime(1613): at java.lang.reflect.Method.invokeNative(Native Method) 12-09 14:39:09.274: E/AndroidRuntime(1613): at java.lang.reflect.Method.invoke(Method.java:515) 12-09 14:39:09.274: E/AndroidRuntime(1613): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 12-09 14:39:09.274: E/AndroidRuntime(1613): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 12-09 14:39:09.274: E/AndroidRuntime(1613): at dalvik.system.NativeStart.main(Native Method)




    My onPost method looked like this

    [HIGH]
    @Override protected void onPostExecute(String result) { super.onPostExecute(result); Toast.makeText(null, rarr, Toast.LENGTH_SHORT).show(); }
    [/HIGH]

    again I tried placing RequestTask.class as the context but it didnt like it so I tried null and got the above stack trace
     
  10. alostpacket

    alostpacket Over Macho Grande?

    Toast needs an activity as a parameter i think, right?

    you should pass it something like

    MyActivity.this
     
  11. Atkinson_88

    Atkinson_88 Newbie
    Thread Starter

    [HIGH]
    @Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);

    Toast.makeText(MainActivity.this, "Bleee", Toast.LENGTH_SHORT).show();
    }

    [/HIGH]

    gives me the following error
    No enclosing instance of type MainActivity is Accessible in scope

    I then change it to

    [HIGH]
    @Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);

    Toast.makeText(MainActivity.class, "Bleee", Toast.LENGTH_SHORT).show();
    }
    [/HIGH]
    which again erros as its moaning about MainActivity.class

    Do you happen to have a working example of this?

    The method I'm trying to call which is inside the service is ReturnVal
     
  12. alostpacket

    alostpacket Over Macho Grande?

    That's weird. Are you in a Fragment?


    try this:

    [HIGH]@Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);

    showMyToast();
    }[/HIGH]

    Then, elsewhere in your activity, OUTSIDE of the async task.
    [HIGH]
    private void showMyToast()
    {
    Toast.makeText(this, "Bleee", Toast.LENGTH_SHORT).show();
    }[/HIGH]
     
  13. Atkinson_88

    Atkinson_88 Newbie
    Thread Starter

    Nope not inside a fragment, ill give that a go tonight.
     
  14. alostpacket

    alostpacket Over Macho Grande?

    I guess I should have asked, where is you AsyncTask running?

    Typically these run within an activity or fragment as an inner class.

    In order to show a toast, you will somehow need access to the activity instance that calls the AsyncTask.
     
Loading...
Similar Threads - Connect WCF service
  1. annie333
    Replies:
    6
    Views:
    240
  2. godlovingman
    Replies:
    15
    Views:
    786
  3. CFresh
    Replies:
    5
    Views:
    631
  4. Krishnobiswas
    Replies:
    5
    Views:
    631
  5. rubazig
    Replies:
    15
    Views:
    901
  6. Spirit84
    Replies:
    5
    Views:
    798
  7. thecriser
    Replies:
    0
    Views:
    707
  8. Tizfaver
    Replies:
    3
    Views:
    1,016
  9. SlimC83
    Replies:
    2
    Views:
    1,007
  10. thecriser
    Replies:
    2
    Views:
    768

Share This Page

Loading...