M.N M.N - 11 months ago 85
reST (reStructuredText) Question

RequestParams params.put method doesn't receive integer

I'm using Rest service to get a list of States of a Country (countryid) from database. The service runs well in Tomcat but it doesn't run in Android. Below is the code to invoke the Rest service. The params.put("countryid",countryID) method doesn't receive integer. Could you please help me over come this issue?

public void getDataForStateSpinner(){
//new StateSpinnerAsync().execute();
System.out.println("getDataForStateSpinner(), spnCountry adapter: " + spnCountry);
System.out.println("spnCountry.getSelectedItem(): " + spnCountry.getSelectedItem());
if (spnCountry.getSelectedItem()!=null){
System.out.println("getDataForStateSpinner(), spnCountry adapter isn't empty");
final Country country = (Country) spnCountry.getSelectedItem();
int countryID = country.getCountryID();
RequestParams params = new RequestParams();
if (countryID>=0){
params.put("countryid", countryID);
} else{
Toast.makeText(mParent.get(), "Can not get CountryID", Toast.LENGTH_LONG).show();

public void invokeWS_State(RequestParams params){
System.out.println("Inside invokeWS_State");
AsyncHttpClient client = new AsyncHttpClient();
System.out.println("Inside invokeWS_State, client: " + client);
System.out.println("Inside invokeWS_State onSuccess, params: " + params);
client.get(stateURL, params, new AsyncHttpResponseHandler(){
public void onSuccess(String respond){
System.out.println("Inside invokeWS_State onSuccess, stateURL: " + stateURL);

System.out.println("Inside invokeWS_State onSuccess, respond:" + respond);
JSONArray jsonArr = new JSONArray(respond);
//JSONObject jsonObject = jsonArr.getJSONObject(0);
System.out.println("Inside invokeWS_State onSuccess jsonArr:" + jsonArr);
String stateList = jsonArr.toString();
System.out.println("Inside invokeWS_State onSuccess stateList:" + stateList);
states = (ArrayList<State>) fromJasonToJava_State(stateList);
for (State state : states) {
System.out.println("State id: " + state.getStateID() + " name: " + state.getStateName());

spnStateAdapter = new ArrayAdapter<State>(mParent.get(), android.R.layout.simple_spinner_dropdown_item, states);

} catch (JSONException e){
} catch (Exception e) {
// TODO Auto-generated catch block
public void onFailure(int statusCode, Throwable error, String content){
String resultString = null;
if (statusCode == 404){
resultString = "Requested resource not found";
Toast.makeText(mParent.get(), resultString, Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
resultString = "Something went wrong at server end!";
Toast.makeText(mParent.get(), resultString, Toast.LENGTH_LONG).show();
} else {
resultString = "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]";
Toast.makeText(mParent.get(), resultString, Toast.LENGTH_LONG).show();


And below is the Rest service which runs well in Tomcat:

public class StatesResource {
public String getStates(@DefaultValue("1") @QueryParam("countryid") int countryID){
String states = null;
ArrayList<State> feedData = null;
StateLoading stateLoading = new StateLoading();
feedData = stateLoading.getAllStatesForCountry(countryID);
Gson gson = new Gson();
states = gson.toJson(feedData);
}catch (Exception e){
System.out.println("System Error " + e);

return states;


Answer Source

Do only one change, it will work perfect.

 params.put("countryid", ""+countryID);