Error while Accessing a WCF Service in Android

General Android Books.
Also feel free to comment, improve and contribute to AndBook, the free Android Learners book.

Error while Accessing a WCF Service in Android

Postby vuyiswamb » Sun Apr 22, 2012 6:47 pm

Good Day All

i am consuming a Wcf service from Android. i am a bit new to Android Dev most of my problems i was able to resolve. this one caught me off guard. My wcf service is defined as follows



Code: Select all
     public List<MemberModel> Helloworld(string number)
            {
            List<MemberModel> lstnames = new List<MemberModel>();
            MemberModel model = new MemberModel();
            model.MEMBER_NAME = "Vuyiswa";
            lstnames.Add(model);
            return lstnames;

            }


and my Memember datacontract class is defined like this

Code: Select all
[DataContract]
    public class MemberModel
        {

         private string _MEMBER_NAME;
 
          [DataMember(Name = "MEMBER_NAME")]
         public string  MEMBER_NAME
            {

            get
                {
                return _MEMBER_NAME;
                }
            set
                {
                _MEMBER_NAME = value;
                }

            }
    }



and my ServiceContract looks like this

Code: Select all
    [DataContract]
    public class MemberModel
        {
        private string _MEMBER_NAME;
 
          [DataMember(Name = "MEMBER_NAME")]
        public string  MEMBER_NAME
            {

            get
                {
                return _MEMBER_NAME;
                }
            set
                {
                _MEMBER_NAME = value;
                }

            }
}


and i am consuming this in my Adroid app like this


Code: Select all
public void getWcfService() throws ClientProtocolException, IOException, JSONException
   {

          TextView textView1 = (TextView)findViewById(R.id.textView1);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(SERVICE_URI + "/Helloworld/1");
           request.setHeader("Accept", "application/json");
          request.setHeader("Content-type", "application/json");
       
          HttpResponse response = httpClient.execute(request);

         HttpEntity responseEntity = response.getEntity();
       
         
        // 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();

         JSONObject MemberModel = new JSONObject(new String(buffer));
         
          textView1.setText(MemberModel.getString("MEMBER_NAME"));
     
   }


i get an Error on the Following line
Code: Select all
       JSONObject MemberModel = new JSONObject(new String(buffer));


and the Error is

Code: Select all
org.json.JSONException: End of input at character 0 of


I have looked at the my Wcf service, i used the wcftestclient to test the public url and it brings that one item in the list.


Thanks for your Help in Advance
vuyiswamb
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 06, 2012 4:22 pm

Top

Return to Android Books

Who is online

Users browsing this forum: No registered users and 2 guests