Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java WCF Android REST
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
0 Sergey Alexandrovich Kryukov 428
1 CHill60 310
2 BillWoodruff 183
3 DamithSL 163
4 OriginalGriff 163


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100