简体   繁体   English

Android Volley忽略参数无法连接

[英]Android Volley ignoring params can't connect

I'm trying to connect to a local API using volley, I passed all the required parameters but it's not working, if I get rid of the email and password and let only the token request it works, so the problem is in the email and password, but those parameters are being ignored, how can I get it working? 我正在尝试使用Volley连接到本地API,我传递了所有必需的参数,但是如果我摆脱了电子邮件和密码,仅让令牌请求起作用,它就无法工作,所以问题出在电子邮件和密码,但是那些参数被忽略了,我如何使它工作?

package quest.testvolley;

import com.android.volley.AuthFailureError;
import com.android.volley.VolleyLog;
import com.kpbird.volleytest.R;



        import org.json.JSONObject;

        import android.app.Activity;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.View;
        import android.widget.TextView;

        import com.android.volley.Request;
        import com.android.volley.RequestQueue;
        import com.android.volley.Response;
        import com.android.volley.VolleyError;
        import com.android.volley.toolbox.JsonObjectRequest;
        import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {

    private TextView txtDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtDisplay = (TextView) findViewById(R.id.txtDisplay);

        RequestQueue queue = Volley.newRequestQueue(this);


        String url = "http://192.168.1.1/represente-mais-api/api/clientes";

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("email", "rm@sss.com.br");
        params.put("senha", "sss");
        //params.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0afd19MfacGa3FFm8CM1hG0eDiIk8");




        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,
                url, new JSONObject(params),
                new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                txtDisplay.setText("Response => "+response.toString());
                findViewById(R.id.progressBar1).setVisibility(View.GONE);
            }
        }, new Response.ErrorListener() {



                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d( "Error: " + error.getMessage());


                }
            })



        {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();


                    headers.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0bDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");

                    return headers;
                }



        };
        queue.add(req);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

You are specifying the HTTP Request type as GET, yet adding form data to it. 您将HTTP请求类型指定为GET,但尚未向其中添加表单数据。

  JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, ... 

You want to be doing an HTTP Post method. 您要执行HTTP Post方法。 Therefore you should replace Request.Method.GET to Request.Method.POST to post FormData to the server. 因此,您应该将Request.Method.GET替换为Request.Method.POST以将FormData发布到服务器。

More about HTTP Methods here . 更多关于HTTP方法在这里

EDIT: If you are getting a 401 while trying an HTACCESS see this question . 编辑:如果您在尝试HTACCESS时得到401,请参阅此问题 You need to pass the parameters using an Authenticator. 您需要使用身份验证器传递参数。

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

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