[英]Android Volley - can't do a JSON request with POST, getting empty response
I am trying to make a json request using Volley library to a php server, but for some reason the server does not receive the json object I am sending, and it responds with empty string. 我正在尝试使用Volley库向php服务器发出json请求,但由于某种原因,服务器未收到我发送的json对象,并且响应为空字符串。 Here is my code
这是我的代码
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MyVolley implements Listener, ErrorListener {
private static Context appContext;
public MyVolley(Context context) {
appContext = context;
}
public void stuff() throws JSONException {
RequestQueue queue = Volley.newRequestQueue(appContext);
JSONObject obj = new JSONObject();
obj.put("param1", "assda");
obj.put("param2", "fassfafsa");
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, "some url here", obj, this, this);
queue.add(stringRequest);
}
@Override
public void onResponse(Object response) {
System.out.println(response);
}
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
}
And when it executes, this is what the server receives 当它执行时,这就是服务器收到的信息
array (
'Content-Type' => 'application/json; charset=utf-8',
'User-Agent' => 'pointless info here',
'Host' => 'some host here',
'Connection' => 'Keep-Alive',
'Accept-Encoding' => 'gzip',
'Content-Length' => '107',
) array (
) array (
)
Why could that be happening? 为什么会这样呢?
Make sure it's not a server side error (try your service using Postman for instance). 确保它不是服务器端错误(例如,使用Postman尝试服务)。
I personally faced the same issue some time ago, changing JsonObjectRequest to StringRequest fixed my problem. 我前一段时间亲自面对过同样的问题,将JsonObjectRequest更改为StringRequest可以解决我的问题。
Take a look at this link : https://stackoverflow.com/a/31613565/7871886 看一下这个链接: https : //stackoverflow.com/a/31613565/7871886
Now I use Retrofit2 instead of Volley... Could be an option. 现在,我使用Retrofit2代替Volley ...可以选择。 Happy coding
快乐编码
Not sure what was your problem, but for the future googlers: 不确定您的问题是什么,但是对于未来的Google员工来说:
My problem was that I was trying to read form $_POST
instead of php://input
我的问题是我试图读取表格
$_POST
而不是php://input
Full code (working): 完整代码(有效):
Java: Java:
JSONObject jsonobj; // declared locally so that it destroys after serving its purpose
jsonobj = new JSONObject();
try {
// adding some keys
jsonobj.put("new key", Math.random());
jsonobj.put("weburl", "hashincludetechnology.com");
// lets add some headers (nested JSON object)
JSONObject header = new JSONObject();
header.put("devicemodel", android.os.Build.MODEL); // Device model
header.put("deviceVersion", android.os.Build.VERSION.RELEASE); // Device OS version
header.put("language", Locale.getDefault().getISO3Language()); // Language
jsonobj.put("header", header);
// Display the contents of the JSON objects
display.setText(jsonobj.toString(2));
} catch (JSONException ex) {
display.setText("Error Occurred while building JSON");
ex.printStackTrace();
}
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonobj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("onResponse()");
try {
result.setText("Response: " + response.toString(2))
System.out.println("Response: " + response.toString(2));
} catch (JSONException e) {
display.setText("Error Occurred while building JSON");
e.printStackTrace();
}
//to make sure it works backwards as well
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("onErrorResponse()");
System.out.println(error.toString());
}
});
System.out.println("After the request is made");
// Add the request to the RequestQueue.
queue.add(jsObjRequest);
Clarification: display
and result
are two TextView
objects I am using to display data on the screen, and queue
is Volley's request queue. 澄清:
display
和result
是我用来在屏幕上显示数据的两个TextView
对象,而queue
是Volley的请求队列。
PHP: PHP:
$inp = json_decode(file_get_contents('php://input')); //$input now contains the jsonobj
echo json_encode(["foo"=>"bar","input"=>$inp]); //to make sure we received the json and to test the response handling
Your Android Monitor should output sth. 您的Android Monitor应该会输出sth。 like :
喜欢 :
{
"foo":"bar",
"input":{
"new key":0.8523024722406781,
"weburl":"hashincludetechnology.com",
"header": {
"devicemodel":"Android SDK built for x86",
"deviceVersion":"7.1",
"language":"eng"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.