Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: JavaWCFAndroidREST
I want to call a rest service which is written in WCF and the input is of type composite which means user class object of .net example of the user class object is as shown below:
public class Sub
{
 public string HostUID { get; set; }
        public string HostName { get; set; }
}
 
so the rest service method input is as below:
[OperationContract(Name="SampleSUb")]
 [WebInvoke(UriTemplate = "/Subsc", Method = "GET")]
public string Method1(Sub input); 
 
Now i want to call the method1 rest service from android java application. I am able to connect to the Rest service but not able to send the input parameters to Sub class.
 
Can any one help me on this???
Posted 5-Jul-12 2:46am
Edited 5-Jul-12 3:10am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just need to call by using JSON method as below from android
 

WCF service method Declaration
public class Sub
{
 public string HostUID { get; set; }
        public string HostName { get; set; }
}
 
so the rest service method input is as below:
[OperationContract(Name="SampleSUb")]
 [WebInvoke(UriTemplate = "/Subsc", Method = "GET",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json))]
public string Method1(Sub input);
 
Android Java code:
URI uri = new URI("http://............."); 
			JSONObject jo1 = new JSONObject();
			jo1.put("Id", "12");
			jo1.put("StringValue", "as");
			HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
			conn.setRequestProperty("Content-Type","application/json; charset=utf-8"); 
			conn.setRequestProperty("Accept", "application/json");
			conn.setRequestProperty("User-Agent", "Pigeon"); 
			conn.setChunkedStreamingMode(0); 
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST"); 
			conn.connect(); 
			DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 
			out.write(jo1.toString().getBytes()); 
			out.flush();  
			int code = conn.getResponseCode(); 
			String message = conn.getResponseMessage(); 
			InputStream in1 = conn.getInputStream();
			StringBuffer sb = new StringBuffer();
			String reply1; 
			try { int chr; while ((chr = in1.read()) != -1) { sb.append((char) chr); }
			reply1 = sb.toString(); }
			finally { in1.close(); }
  Permalink  
v2
Comments
Prasad_Kulkarni at 6-Jul-12 1:57am
   
+5 for sharing!
Sandeep Mewara at 6-Jul-12 2:07am
   
5! for the answer.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 685
1 OriginalGriff 364
2 Manas Bhardwaj 335
3 Abhinav S 263
4 Sampath Lokuge 225
0 Sergey Alexandrovich Kryukov 7,967
1 OriginalGriff 4,332
2 Peter Leow 3,699
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 6 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid