1

I have a WCF Service (working tried it in the browser) the code for it

[OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Login/?username={username}&password={password}")]
    bool Login(string username, string password);

    [OperationContract]
    [WebGet(UriTemplate = "Search/?query={query}&type={type}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    List<AndroidBook> Search(string query, int type);

EDIT: the web config file:

<system.serviceModel>
    <services>
      <service name="??????.AndroidBridge">
        <endpoint address="" binding ="webHttpBinding" contract="??????.IAndroidBridge" behaviorConfiguration="webHttpBehavior"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="" >
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp/>
        </behavior>
        <behavior name="webHttpBehavior">
          <webHttp automaticFormatSelectionEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding"/>
    </protocolMapping>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel> 

and when testing it in browser i get the following:

{"LoginResult":true}

which is true but when trying to connect in the android client with this code:

try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpResponse response = httpclient.execute(new HttpGet(urls));
                        StatusLine statusLine = response.getStatusLine();
                        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            response.getEntity().writeTo(out);
                            out.close();
                            String responseString = out.toString();
                            //..more logic
                        } else{
                            //Closes the connection.
                            response.getEntity().getContent().close();
                            throw new IOException(statusLine.getReasonPhrase());
                        }
                    } catch (IOException e) {
                        Log.e("HTTP GET:", e.toString());
                    }

i got an exception of filenotfound or when i get a response with outer codes i got a bad request , i have googled this problem and came across same problems and read the following articles
C#/Java - Consuming WCF Service from Android emulator
Exposing a service running on localhost to an Android emulator?
How to fix android.os.NetworkOnMainThreadException?
and using fiddler i got the correct response in JSON
any help will be appreciated :)

Community
  • 1
  • 1
Mhd
  • 81
  • 6
  • have you used asynctask or a thread for network operation in android? – Illegal Argument Jun 21 '14 at 12:15
  • @IllegalArgument i read about `asynctask` but for testing purposes i have put the following in android code `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); ` do you think that this is the reason for my problem ? – Mhd Jun 21 '14 at 12:18
  • Explain your setup. Webserver? Where? On pc? On web? Using devices or emulator? – greenapps Jun 21 '14 at 12:19
  • i have the server running on localhost pc using VS2013 on Windows 8.1 x64 – Mhd Jun 21 '14 at 12:20
  • You did not tell your complete setup. Though I asked for specific settings. Further question: do you request internet permission in manifest? – greenapps Jun 21 '14 at 12:21
  • @greenapps if i get you correctly i have the website on an **IISEXPRESS** running on localhost:33829 with not deployed on the web – Mhd Jun 21 '14 at 12:25
  • Yes but i had more questions. Please answer. – greenapps Jun 21 '14 at 12:26
  • @greenapps absolutely i have the permission in the manifest `` – Mhd Jun 21 '14 at 12:30
  • Please reread my first comment and answer all. I still do not know your setup. – greenapps Jun 21 '14 at 12:33
  • @greenapps please specify what you need to know because i used both ways device and emulator that's way i didn't answer it – Mhd Jun 21 '14 at 12:36
  • 1
    @Mhd i have the same problem you do and almost the same setup of the webserver please help – Yaser Jaradeh Jun 21 '14 at 12:41
  • @YaserJaradeh bro i am sad :( – Mhd Jun 21 '14 at 13:11
  • If you use an emulator, then on witch pc is the emulator? – greenapps Jun 21 '14 at 13:27

0 Answers0