[英]No response from Volley JSONObject POST request
I want to send a JSONObject POST request with Volley that takes a single parameter.我想使用带有单个参数的 Volley 发送一个 JSONObject POST 请求。 The code I've written, and included below, doesn't seem to receive any response in the onResponse method.我编写并包含在下面的代码似乎没有在 onResponse 方法中收到任何响应。 I can't see what the problem might be to cause this.我看不出是什么问题导致了这种情况。
I have checked on the server-side, and that is working as it should, with the PHP code echoing a JSON encoded array of data.我已经在服务器端进行了检查,它正常工作,PHP 代码回显了一个 JSON 编码的数据数组。
public void mySightingsJsonRequest() {
//get username held in shared preferences (KMcE)
HashMap<String, String> user = newSession.getUsername();
final String username = user.get(SessionUtility.KEY_USERNAME);
JSONObject obj = new JSONObject();
try {
obj.put("Username", username);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest mySightingsRequest = new JsonObjectRequest(Request.Method.POST, ConnectionURLs.MY_SIGHTINGS, obj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "onResponse: MySightings response");
try {
JSONArray jsonArray = response.getJSONArray("server_response");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject sighting = jsonArray.getJSONObject(i);
int sightingRecordingNumber = sighting.getInt("Recording Number");
String sightingImgURL = sighting.getString("IMG");
String sightingCommonName = sighting.getString("Common Name");
String sightingUsername = sighting.getString("Submitted by");
double sightingLongitude = sighting.getDouble("Longitude");
double sightingLatitude = sighting.getDouble("Latitude");
mSightingList.add(new SightingSingle(sightingRecordingNumber, sightingImgURL, sightingCommonName, sightingUsername, sightingLatitude, sightingLongitude));
}
mMySightingsAdapter = new MySightingsAdapter(getActivity(), mSightingList);
mMySightingRecyclerView.setAdapter(mMySightingsAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mRequestQueue.add(mySightingsRequest);
}
Kieran M. 基兰·M。
Please use the following code. 请使用以下代码。
Log.d(TAG, "onResponse: MySightings response" + response.toString());
You should watch response value. 您应该注意响应值。
Now that's a one of methods for debug. 现在,这是一种调试方法。
You can ask if the server can receive json 您可以询问服务器是否可以接收json
or try this code: 或尝试以下代码:
String reuquestBody = "Username="+username
and then replace obj with requestBody 然后用requestBody替换obj
add this in try block... 在try块中添加...
try {
JSONObject jsonObject = new JSONObject(response);
//this will check whether response has "server_response" array or not
if(jsonObject.has("server_response")){
JSONArray jsonArray = jsonObject.getJSONArray("server_response");
for (int i = 0; i < jsonArray.length(); i++) {
//your code as it is....
}
hope this will help you 希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.