java - Android Volley 忽略参数无法连接

标签 java android http-headers httprequest android-volley

我正在尝试使用 volley 连接到本地 API,我传递了所有必需的参数,但它不起作用,如果我去掉电子邮件和密码并只让 token 请求起作用,那么问题出在电子邮件和密码,但这些参数被忽略了,我怎样才能让它工作?

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.

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

You want to be doing an HTTP Post method. Therefore you should replace Request.Method.GET to Request.Method.POST to post FormData to the server.

More about HTTP Methods here.

编辑:如果您在尝试 HTACCESS 时收到 401,请参阅此 question .您需要使用身份 validator 传递参数。

关于java - Android Volley 忽略参数无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24368870/

相关文章:

java - android中如何检测ArrayList内容一段时间没有改变?

url - Google OAuth 2.0 redirect_uri_mismatch错误

java - 如何在 Java 中防止 403 HTTP 错误代码?

java - 删除 String 中的 0 字节 (UTF-8) 字符

java - "Bad version number in .class file"是否有 Maven 插件来检查此错误?

java - 如何导入 Android 库并在生产代码和测试中使用它?

android - 我的具有 Android 测试的 Android 应用程序中的依赖冲突错误

android - 以编程方式,如何识别信标是属于 Eddystone 还是 iBeacon?

http-headers - 如何转义 HTTP header 中的换行 rune 字?

java - 是否有适用于Google App引擎的开源Java SIP服务器?