gpt4 book ai didi

java - Volley Post 请求在 onResponse 内未返回任何响应

转载 作者:行者123 更新时间:2023-12-02 02:41:52 25 4
gpt4 key购买 nike

所以我想打印响应的结果,然后使用 gson 为模型生成数据,但响应永远不会返回,但 onResponse 被调用。!

如果你注意到 Logcat,onResponse 中的 log.i 没有显示..!?但在使用此请求的 Activity 的 onSuccess 内部,它正常显示日志,但如果日志包含响应对象,它不会显示,这非常奇怪并且没有任何意义..!?

日志猫

I/getUrl:: http://1925.232.55/login.php
I/getParams:: {username =samy, password=123456}
D/libc: [NET] android_getaddrinfofornetcontext+,hn 20(0x6),sn(),hints(known),family 0,flags 1024, proc=com...
D/libc: [NET] android_getaddrinfo_proxy get netid:0
D/libc: [NET] android_getaddrinfo_proxy-, success
I/onSuccess:: check /* this log inside the Activity which uses this request */

I tried test the request using PostMan with the same url and params and it returns json response normally..?

postman 回复

{
"status": "success",
"user_id": "10",
"user_name": "samy",
"full_name": "samy samy",
"picture": "st_pictures/IMG_085207.jpg",
"level": "1",
"school": "NY School",
"city": "NY",
"class": "6",
"age": "22",
"teacher": "2",
"token": "f808e758sdfsdfsf162dbdfcf88e3dc8a96"
}

请求代码

 final RequestQueue queue = Volley.newRequestQueue(context);

final StringRequest sr = new StringRequest(Request.Method.POST, getLoginURL(), new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("onResponse: ", response + "");
requestResult.onSuccess(response);


}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
requestResult.onFail(error);

VolleyLog.e("Error: ", error.getMessage());
error.printStackTrace();
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("username ", un);
params.put("password", pass);
Log.i( "getParams: ", params.toString());
return params;
}

@Override
public byte[] getBody() throws AuthFailureError {
Log.i( "getUrl: ",getUrl());
return super.getBody();

}
}
;

queue.add(sr);

更新#1

网址http://clients.intertech.ps/raz/std.php

 E/Volley: [1] 2.onErrorResponse: Error: 
W/System.err: com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of
W/System.err: at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73)
W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123)
W/System.err: Caused by: org.json.JSONException: End of input at character 0 of
W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
W/System.err: at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68)
W/System.err: ... 1 more

最佳答案

这个问题很久以前就被问到了,但我也遇到了同样的问题,几个小时后我让它与 Volley 一起工作

使用StringRequest而不是JsonObjectRequest,出于某种原因我在JOR上遇到了很多问题,并且我使用了@Oussema ArouagetHeaders函数

final Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("RESPONSE", String.valueOf(response));
}
};

final Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

//Handle your errors
}
};

StringRequest request = new StringRequest(Request.Method.POST, url,
responseListener, errorListener) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
//Put your params for post
return params;
}

@Override
public Map<String, String> getHeaders() {
Map<String,String> params = new HashMap<>();
params.put("Content-Type","application/x-www-form-urlencoded"); //-> this is the key
return params;
}
};

.. 您将在 onResponse

中从服务器获取 json 响应

关于java - Volley Post 请求在 onResponse 内未返回任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326119/

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