gpt4 book ai didi

java - RequestParams params.put 方法未接收整数

转载 作者:行者123 更新时间:2023-12-01 09:23:56 25 4
gpt4 key购买 nike

我正在使用 Rest 服务从数据库获取国家/地区的州列表 (countryid)。该服务在 Tomcat 中运行良好,但在 Android 中无法运行。以下是调用 Rest 服务的代码。 params.put("countryid",countryID) 方法不接收整数。您能帮我解决这个问题吗?

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);
invokeWS_State(params);
} 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(){
@Override
public void onSuccess(String respond){
try{
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);
spnStateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnState.setAdapter(spnStateAdapter);

} catch (JSONException e){
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
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();
}

}
});
}

下面是在 Tomcat 中运行良好的 Rest 服务:

@Path("/State")
public class StatesResource {
@GET
@Path("/GetStates")
@Produces(MediaType.APPLICATION_JSON)
//http://localhost:8080/com.fms.FMSRestfulWS/State/GetStates?countryid=1
public String getStates(@DefaultValue("1") @QueryParam("countryid") int countryID){
String states = null;
try{
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;
}

}

最佳答案

只做一项改变,它就会完美地工作。

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

关于java - RequestParams params.put 方法未接收整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39990131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com