1

Facing issues calling wcf service from android. web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="httpBehavior">
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="false" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        <behavior name="webBehavior">
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="GateP.Service1">
        <endpoint name="webHttpBinding" address="" binding="webHttpBinding" contract="GateP.IService1" behaviorConfiguration="httpBehavior" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding maxReceivedMessageSize="10000000" receiveTimeout="01:00:00">
          <readerQuotas maxStringContentLength="10000000" />
        </binding>
      </webHttpBinding>
    </bindings>    
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <defaultDocument>
      <files>
        <add value="Service1.svc" />
      </files>
    </defaultDocument>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

IService1.cs

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "UserLogin",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json)]
        GPUsers UserLogin(GPUsers request);

Service1.svc

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace GateP
{
    public class Service1 : IService1
    {
#region GPUser Login
        public GPUsers UserLogin(GPUsers request)
        {
            return request;
        }
        #endregion
    }
}

Here is GPUser class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace GateP
{
    [DataContract]
    public class GPUsers
    {
        [DataMember]
        public int userID { get; set; }
        [DataMember]
        public string username { get; set; }
        [DataMember]
        public string password { get; set; }
        [DataMember]
        public string department { get; set; }
        [DataMember]
        public bool userActive { get; set; }
    }
}

Android code (java file)

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * Created by Arslan-PC on 4/7/2017.
 */
public class CallWebService {

    //Calling web service using in json format
    public JSONObject JsonObjectService(String url, JSONObject data)
    {
        JSONObject jsonResponse=null;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("content-type", "application/json");
        httpPost.setHeader("Accept", "application/json");

        HttpClient httpClient = new DefaultHttpClient();
        // Building the JSON object.
        /*JSONObject data = new JSONObject();*/
        String result = null;
        StringEntity entity ;
        try
        {
           JSONObject request=new JSONObject();
            request.put("request", data);

            entity = new StringEntity(request.toString());
            httpPost.setEntity(entity);
            // Making the call.
            HttpResponse response = httpClient.execute(httpPost);
            int status=response.getStatusLine().getStatusCode();
            if(status==200)
            {
                HttpEntity responseEntity = response.getEntity();
                if(responseEntity!=null)
                {
                    InputStream stream = responseEntity.getContent();
                    BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
                    jsonResponse = new JSONObject(buffer.readLine());
                }
            }
        }
        catch (JSONException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        catch(UnsupportedEncodingException e2)
        {

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonResponse;
    }
}

What i tried so far: Publish wcf service on iis using file system of visual studio. After publishing when i try browsing this address 192.168.5.103:12345 on mobile and desktop, it just looks fine but when i access this using wcf it throws status 400 with bad request error. I don't know what is the main problem. Attaching screenshots please have a look. Also i use this line in gradle compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

enter image description here Tools: Visual studio 2012 and Android studio

0 Answers0