Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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:
Java
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
Updated 5-Jul-12 3:10am
v2

1 solution

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


WCF service method Declaration
C#
public class Sub
{
 public string HostUID { get; set; }
        public string HostName { get; set; }
}


so the rest service method input is as below:
XML
[OperationContract(Name="SampleSUb")]
 [WebInvoke(UriTemplate = "/Subsc", Method = "GET",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json))]
public string Method1(Sub input);


Android Java code:
Java
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(); }
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 6-Jul-12 1:57am    
+5 for sharing!
Sandeep Mewara 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900