• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Connect to WCF service through android

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.
 
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.
 
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
 
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]
 
Ruby? Is that the color the PHP manual was originally? (There's a server-side language not called PHP?)
 
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 :)
 
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]

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
 
Toast needs an activity as a parameter i think, right?

you should pass it something like

MyActivity.this

[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
 
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]
 
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.
 
Back
Top Bottom