Итак, проблема в том, что я тестировал в Почтальоне
{
"country": "Australia",
"windProbability": "90"
}
не даст мне результатов, но
{
"country": "Australia",
"windProbability": 90
}
работает отлично
Проблема в том, что когда я отправляю
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
String country = getIntent().getStringExtra("country");
String windProbability = getIntent().getStringExtra("windProbability");
if(country==null){
country="";
windProbability="";
}
params.put("country", country);
params.put("windProbability", String.valueOf(windProbability));
return params;
}
сервер получает
{
"country": "Australia",
"windProbability": "90"
}
Удалите метод get params и отправьте так. Вы не можете отправить значение int, используя функцию getparams.
String url = "your url";
JSONObject postparams = new JSONObject();
try {
postparams.put("country", "Australia");
postparams.put("windProbability", 90);
// make it dynamic by declaring windProbalility as int
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG,postparams.toString());
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, postparams,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG+"Error: ",error.getMessage());
}
});
Используйте объект JSONObject для добавления параметров запроса вместо Map.
void sendTestRequest() throws JSONException {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.google.com";
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("country", "Australia");
jsonRequest.put("windProbability", 90);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonRequest,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(jsonObjectRequest);
}
Map<String, String>
не будет принимать Integer
.