0

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

    [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

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"/> to my manifest. In my LogCat there is a NetworkOnMainThreadException. How can I fix the problem?

Jonathan
  • 545
  • 1
  • 8
  • 27
  • In which thread is this called? Http client is only usable outside the ui thread. – Rene M. Jul 22 '14 at 19:55
  • @rmertins How do you mean in what thread do I call. I placed it inside of my `oncreate`. Must I move it to another java class? – Jonathan Jul 22 '14 at 19:57
  • Read here http://stackoverflow.com/questions/5150637/networkonmainthreadexception – Rene M. Jul 22 '14 at 19:59
  • As java developer you should know your thread, especially on multi core devices. – Rene M. Jul 22 '14 at 19:59
  • @rmertins Just correct me if I am wrong but I should rather run my code in AsyncTask – Jonathan Jul 22 '14 at 20:11
  • Yes. In any thread you want but not in the main ui thread and don't forget to syncronize when updating the ui. – Rene M. Jul 22 '14 at 20:13
  • @rmertins Do you know where I can get an example of how to Synchronize when Updating UI – Jonathan Jul 23 '14 at 13:44
  • Have a look here: http://stackoverflow.com/questions/12850143/android-basics-running-code-in-the-ui-thread or here is the google android docs part about best practice and performance https://developer.android.com/training/multiple-threads/index.html – Rene M. Jul 23 '14 at 13:48
  • If my response that I get form WCF is not what I am expecting then I did not Synchronize. Right? @rmertins – Jonathan Jul 23 '14 at 13:54
  • I don't know your application and what you expect from your service. When you get a bad result and don't want to show this to your user, then not. Depends on your use case and what your service makes. All my services are using an envelop which is parsabel in a generic way, which tells me if got a result from the service (embeded in the envelop) or if an error happens including a http status code like number concept. Depending on the status code my generic part of client libs decide how to handle the response. – Rene M. Jul 23 '14 at 13:57
  • @rmertins I mean the WCF must return a response string. When I run the thread the response string is `com.example.wcf.ServiceRun@41f893c8` where it should be `exists` – Jonathan Jul 23 '14 at 14:02
  • That is not a response that is an instance identifier of a java object which has no own toString() implementation. It means you printing an Object of kind "ServiceRun" and it has the id 41f893c8 in your memorie ;) Normal process when developing webservices: 1. impl. service, 2. test with browser or SOAP Test Client 3. at least impl. client lib to use in client apps. – Rene M. Jul 23 '14 at 14:05
  • @rmertins So I am printing the string to that position in memory. – Jonathan Jul 23 '14 at 14:08
  • No it is not a String it is a ServiceRun object. Can it be that your webservice is responding a json serialized version of that kind of object? Then your client has already parsed the json string and made an object out of it. Use the debugger in your ide, set a breakpoint to the place where your got your response and inspect it. Sorry I can't further help you. It's going in a direction like I teach you programming java basics. U need to know your ide, the debugger and fundamentals in java. Otherwise it makes no sense, like your last questions – Rene M. Jul 23 '14 at 14:17
  • @rmertins Thanks for the help. It is just the fist time that I am working with WCF. And it is a JSON string – Jonathan Jul 23 '14 at 14:19
  • But your questions are depending on WCF, they are java basics and fundamentles of the android sdk. Good luck – Rene M. Jul 23 '14 at 14:23

0 Answers0