0

I have Java web application server [ acts like server ].

In Android application, using httppost i have calling the restwebserive server.

My calling is hit the webservice with the response code 200.

Now i want to pass the java class object as like parameter.

sample java class:

public Class Sample{

public String Username;

public String getUsername()
{

return Username;

}

public void setUsername(String user){


this.Username = user;

}}

Used code :[ Is not passing my class object to server ]

  Sample sam = new Sample();
  sam.setUsername("Test");
  JSONObject json = new JSONObject();  
  json.put("Sample", sam);            
  StringEntity se = new StringEntity(json.toString());  
  Httppostrequest.setEntity(se);

when i debugging the server the sample object parameter input is empty.[Not passed properly]
How to pass the class object via http post in android? Please help me on this.

Thanks in advance,

Kums

Taryn
  • 242,637
  • 56
  • 362
  • 405
kumaresan
  • 1
  • 1
  • You need to pass String there inspite of object `sam` like ` jSONObject().put("Sample", "Hello, World!")` AFAIK you can only represent data using Strings in JSON. See [here](http://www.json.org/javadoc/org/json/JSONObject.html) – 100rabh Jun 27 '11 at 13:00
  • Why not use REST webservice like [this](https://stackoverflow.com/a/44302488/4116560)? – vss Jul 12 '17 at 11:08

3 Answers3

0

I have built a library for doing async requests, you can send parameter requests as www.somedomain.com/action?param1="somevalue" etc.. and also there is the option to use string body.

https://github.com/darko1002001/android-rest-client

Check it out, it might be helpful.

DArkO
  • 15,880
  • 12
  • 60
  • 88
0

if you use apache library you can do it one line

JSONSerializer.toJSON(sam);

otherwise i think you have to send it as

 Sample sam = new Sample();
 sam.setUsername("Test");
  JSONObject json = new JSONObject();  
  json.put("sample", sam.getUserName());
StringEntity se = new StringEntity(json.toString());  
 Httppostrequest.setEntity(se);
AD14
  • 1,218
  • 19
  • 32
0

Here is a code snippet

  public void callWebService(String q){  
     HttpClient httpclient = new DefaultHttpClient();  
     HttpGet request = new HttpGet(URL + q);  
     request.addHeader("deviceId", deviceId);  
     ResponseHandler<string> handler = new BasicResponseHandler();  
     try {  
         result = httpclient.execute(request, handler);  
     } catch (ClientProtocolException e) {  
         e.printStackTrace();  
     } catch (IOException e) {  
         e.printStackTrace();  
     }  
     httpclient.getConnectionManager().shutdown();  
     Log.i(tag, result);  
 } // end callWebService()  

}

Use this method to call your webservice

Rahul Sharma
  • 3,637
  • 2
  • 20
  • 18