繁体   English   中英

Volley JsonObjectRequest Post 请求不起作用

[英]Volley JsonObjectRequest Post request not working

我正在使用 android Volley 提出请求。 所以我使用这个代码。 我不明白一件事。 我在我的服务器中检查 params 始终为空。 我认为 getParams() 不起作用。 我该怎么做才能解决这个问题。

 RequestQueue queue = MyVolley.getRequestQueue();
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                        hideProgressDialog();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                      hideProgressDialog();
                    }
                }) {
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("id","1");
                params.put("name", "myname");
                return params;
            };
        };
        queue.add(jsObjRequest);

尝试使用这个助手类

import java.io.UnsupportedEncodingException;
import java.util.Map;    
import org.json.JSONException;
import org.json.JSONObject;    
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public CustomRequest(int method, String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

在活动/片段中使用这个

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());

requestQueue.add(jsObjRequest);

您可以创建自定义JSONObjectReuqest并覆盖getParams方法,或者您可以在构造函数中提供它们作为JSONObject放入请求正文中。

像这样(我编辑了你的代码):

JSONObject obj = new JSONObject();
obj.put("id", "1");
obj.put("name", "myname");

RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,obj,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
             System.out.println(response);
             hideProgressDialog();
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
             hideProgressDialog();
        }
    });
queue.add(jsObjRequest);

对我来说很简单! 几周前我收到了:

这在getBody()方法中,而不是在 post 请求的getParams()中。

这是我的:

    @Override
/**
 * Returns the raw POST or PUT body to be sent.
 *
 * @throws AuthFailureError in the event of auth failure
 */
public byte[] getBody() throws AuthFailureError {
    //        Map<String, String> params = getParams();
    Map<String, String> params = new HashMap<String, String>();
    params.put("id","1");
    params.put("name", "myname");
    if (params != null && params.size() > 0) {
        return encodeParameters(params, getParamsEncoding());
    }
    return null;

}

(我假设您想发布您在 getParams 中编写的参数)

我在构造函数中为请求提供了参数,但是由于您正在动态创建请求,因此您可以在 getBody() 方法的覆盖中对它们进行硬编码。

这是我的代码的样子:

    Bundle param = new Bundle();
    param.putString(HttpUtils.HTTP_CALL_TAG_KEY, tag);
    param.putString(HttpUtils.HTTP_CALL_PATH_KEY, url);
    param.putString(HttpUtils.HTTP_CALL_PARAM_KEY, params);

    switch (type) {
    case RequestType.POST:
        param.putInt(HttpUtils.HTTP_CALL_TYPE_KEY, RequestType.POST);
        SCMainActivity.mRequestQueue.add(new SCRequestPOST(Method.POST, url, this, tag, receiver, params));

如果你想要更多,最后一个字符串参数来自:

param = JsonUtils.XWWWUrlEncoder.encode(new JSONObject(paramasJObj)).toString();

paramsJObj 是这样的: {"id"="1","name"="myname"}通常的 JSON 字符串。

当您处理 JsonObject 请求时,您需要在初始化中传递链接后立即传递参数,请查看以下代码:

        HashMap<String, String> params = new HashMap<>();
        params.put("user", "something" );
        params.put("some_params", "something" );

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, "request_URL", new JSONObject(params), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

           // Some code 

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //handle errors
        }
    });


}

您需要做的就是覆盖 Request 类中的 getParams 方法。 我遇到了同样的问题,我搜索了答案,但找不到合适的答案。 问题与 get 请求不同,服务器重定向的 post 参数可能会被丢弃。 例如,阅读这个 因此,不要冒险让您的请求被网络服务器重定向。 如果您的目标是http://example/myapp ,请提及您的服务的确切地址,即http://example.com/myapp/index.php
Volley 没问题,工作完美,问题出在其他地方。

覆盖函数 getParams 工作正常。 您使用 POST 方法并将 jBody 设置为 null。 这就是它不起作用的原因。 如果您想发送空 jBody,您可以使用 GET 方法。 我已经覆盖了 getParams 方法,它可以使用 GET 方法(和 null jBody)或者使用 POST 方法(和 jBody != null)

也有所有的例子在这里

我曾经遇到过同样的问题,空的 POST 数组是由于请求的重定向(在您的服务器端)引起的,修复 URL 以便在它到达服务器时不必重定向。 例如,如果使用服务器端应用程序上的 .htaccess 文件强制使用 https,请确保您的客户端请求具有“https://”前缀。 通常当重定向发生时,POST 数组会丢失。 我希望这有帮助!

它适用于可以尝试使用 Volley Json Request and Response 和 Java Code 进行调用。

public void callLogin(String sMethodToCall, String sUserId, String sPass) {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST, ConstantValues.ROOT_URL_LOCAL + sMethodToCall.toString().trim(), addJsonParams(sUserId, sPass),
//                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("onResponse", response.toString());
                        Toast.makeText(VolleyMethods.this, response.toString(), Toast.LENGTH_LONG).show(); // Test

                        parseResponse(response);
//                        msgResponse.setText(response.toString());
//                        hideProgressDialog();
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("onErrorResponse", "Error: " + error.getMessage());
                        Toast.makeText(VolleyMethods.this, error.toString(), Toast.LENGTH_LONG).show();
//                hideProgressDialog();
                    }
                }) {

            /**
             * Passing some request headers
             */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }


        };

        requestQueue.add(jsonObjectRequest);
    }

    public JSONObject addJsonParams(String sUserId, String sPass) {
        JSONObject jsonobject = new JSONObject();
        try {
//            {"id":,"login":"secretary","password":"password"}

            ///***//
            Log.d("addJsonParams", "addJsonParams");

//            JSONObject jsonobject = new JSONObject();

//            JSONObject jsonobject_one = new JSONObject();
//
//            jsonobject_one.put("type", "event_and_offer");
//            jsonobject_one.put("devicetype", "I");
//
//            JSONObject jsonobject_TWO = new JSONObject();
//            jsonobject_TWO.put("value", "event");
//            JSONObject jsonobject = new JSONObject();
//
//            jsonobject.put("requestinfo", jsonobject_TWO);
//            jsonobject.put("request", jsonobject_one);

            jsonobject.put("id", "");
            jsonobject.put("login", sUserId); // sUserId
            jsonobject.put("password", sPass); // sPass


//            js.put("data", jsonobject.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonobject;
    }

    public void parseResponse(JSONObject response) {

        Boolean bIsSuccess = false; // Write according to your logic this is demo.
        try {
            JSONObject jObject = new JSONObject(String.valueOf(response));
            bIsSuccess = jObject.getBoolean("success");


        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(VolleyMethods.this, "" + e.toString(), Toast.LENGTH_LONG).show(); // Test
        }

    }



构建gradle(应用程序)

 dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.1.0' implementation 'androidx.appcompat:appcompat:1.1.0-alpha01' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'com.android.volley:volley:1.1.1' }

安卓清单
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley

fun peticion(){
    val jsonObject = JSONObject()
    jsonObject.put("user", "jairo")
    jsonObject.put("password", "1234")
    val queue = Volley.newRequestQueue(this)
    val url = "http://192.168.0.3/get_user.php"
    // GET: JsonObjectRequest( url, null,
    // POST: JsonObjectRequest( url, jsonObject,
    val jsonObjectRequest = JsonObjectRequest( url, jsonObject,
        Response.Listener { response ->
            // Check if the object 'msm' does not exist
            if(response.isNull("msm")){
                println("Name: "+response.getString("nombre1"))
            }
            else{
                // If the object 'msm' exists we print it
                println("msm: "+response.getString("msm"))
            }
        },
        Response.ErrorListener { error ->
            error.printStackTrace()
            println(error.toString())
        }
    )
    queue.add(jsonObjectRequest)
}

主要活动
当您使用 JsonObjectRequest 时,必须发送 jsonobject 并接收 jsonobject 否则您将收到错误,因为它只接受 jsonobject。
 import com.android.volley.Request import com.android.volley.Response import com.android.volley.toolbox.JsonObjectRequest import com.android.volley.toolbox.Volley fun peticion(){ val jsonObject = JSONObject() jsonObject.put("user", "jairo") jsonObject.put("password", "1234") val queue = Volley.newRequestQueue(this) val url = "http://192.168.0.3/get_user.php" // GET: JsonObjectRequest( url, null, // POST: JsonObjectRequest( url, jsonObject, val jsonObjectRequest = JsonObjectRequest( url, jsonObject, Response.Listener { response -> // Check if the object 'msm' does not exist if(response.isNull("msm")){ println("Name: "+response.getString("nombre1")) } else{ // If the object 'msm' exists we print it println("msm: "+response.getString("msm")) } }, Response.ErrorListener { error -> error.printStackTrace() println(error.toString()) } ) queue.add(jsonObjectRequest) }

文件 php get_user.php
 <?php header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: *"); // we receive the parameters $json = file_get_contents('php://input'); $params = json_decode($json); error_reporting(0); require_once 'conexion.php'; $mysqli=getConex(); $user=$params->user; $password=$params->password; $res=array(); $verifica_usuario=mysqli_query($mysqli,"SELECT * FROM usuarios WHERE usuario='$user' and clave='$password'"); if(mysqli_num_rows($verifica_usuario)>0){ $query="SELECT * FROM usuarios WHERE usuario='$user'"; $result=$mysqli->query($query); while($row = $result->fetch_array(MYSQLI_ASSOC)){ $res=$row; } } else{ $res=array('msm'=>"Incorrect user or password"); } $jsonstring = json_encode($res); header('Content-Type: application/json'); echo $jsonstring; ?>

文件 php conexion
 <?php function getConex(){ $servidor="localhost"; $usuario="root"; $pass=""; $base="db"; $mysqli = mysqli_connect($servidor,$usuario,$pass,$base); if (mysqli_connect_errno($mysqli)){ echo "Fallo al conectar a MySQL: " . mysqli_connect_error(); } $mysqli->set_charset('utf8'); return $mysqli; } ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM