Atkinson_88
Newbie
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.
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.

