Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have been attempting to to connect to a WCF Service from a Android device. I have read a lot of blogs that does not seem to be useful. One of the Operations running on my WCF is -
C#
[OperationContract]
[WebGet(UriTemplate = "write", ResponseFormat = WebMessageFormat.Json)]
string write();

This writes one entity to a database. When I enter the URL in my phones browser "10.0.0.14/serv/UserManagement.svc/write" I get the relevant message and it writes to the database with no problem. The problem arises when I attempt to Consume the WCF from a android application.

I have jumped between many different solution types and I am currently using
C#
try 
{

    DefaultHttpClient httpClient = new DefaultHttpClient();
    URI uri = new URI("http://10.0.0.14/serv/UserManagement.svc/write"); 

    HttpGet httpget = new HttpGet(uri);
    httpget.setHeader("Accept", "application/json");
    httpget.setHeader("Content-type", "application/json; charset=utf-8");

    HttpResponse response = httpClient.execute(httpget);
    HttpEntity responseEntity = response.getEntity();


}
catch (Exception e) 
{
     e.printStackTrace();
}

This does not work. I have added <uses-permission android:name="android.permission.INTERNET" xmlns:android="#unknown" /> to my manifest.

In my LogCat there is a NetworkOnMainThreadException.

How can I fix the problem?
Posted
Updated 24-Jul-14 5:43am
v2

Seems to be nothing wrong in your WCF code since its returning result when you access it in browser. You should dig into you Android code. You may try by calling your REST service in AsyncTask.

Have a look at below link for more details.

http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception
 
Share this answer
 
You should use AsyncTask

Java
class ConnectToWCF extends AsyncTask<void,> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected String doInBackground(Void... arg0) {
        String is = "";

        try 
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            URI uri = new URI("http://10.0.0.14/serv/UserManagement.svc/write"); 
 
            HttpGet httpget = new HttpGet(uri);
            httpget.setHeader("Accept", "application/json");
            httpget.setHeader("Content-type", "application/json; charset=utf-8");
 
            HttpResponse response = httpClient.execute(httpget);
            HttpEntity responseEntity = response.getEntity();
 
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return is;
    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}


and use it in your main thread i.e onCreate

Java
new ConnectToWCF().execute();
 
Share this answer
 
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();

JSONArray data = new JSONArray(new String(buffer))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900