Android:Volley HTTP Request自定义标头

时间:2014-06-18 12:24:36

标签: android httprequest android-volley digest-authentication

运行应用时出现以下错误: BasicNetwork.performRequest:意外的响应代码401

我需要传递电子邮件,密码和令牌才能访问该网址,但该网址无效

我上周开始学习android,我不太了解

package quest.testvolley;

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

import android.app.ProgressDialog;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
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 org.json.JSONArray;
import org.json.JSONObject;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {

    private String TAG = this.getClass().getSimpleName();
    private ListView lstView;
    private RequestQueue mRequestQueue;
    private ArrayList<NewsModel> arrNews ;
    private LayoutInflater lf;
    private VolleyAdapter va;
    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lf = LayoutInflater.from(this);

        arrNews = new ArrayList<NewsModel>();
        va = new VolleyAdapter();

        lstView = (ListView) findViewById(R.id.listView);
        lstView.setAdapter(va);
        mRequestQueue =  Volley.newRequestQueue(this);
        String url = "http://192.168.1.18/repr/api/clientes/Y2FtcG9zKGlkLG5vbWUsc3RhdHVzKTppnaW1pdCgxMCk6b2Zmc2V0KDApOm9yZGVtKG5vbWVbYXNjXSk=";
        pd = ProgressDialog.show(this,"Please Wait...","Please Wait...");
        try{

            Thread.sleep(2000);
        }catch(Exception e){}

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                Log.d(TAG, response.toString());
                pd.hide();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                VolleyLog.d(TAG, "Error: " + error.getMessage());
                pd.hide();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {

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

                return params;
            }
        };

        mRequestQueue.add(jsonObjReq);
    }

3 个答案:

答案 0 :(得分:14)

JsonObjectRequest 扩展JsonRequest,直接覆盖getBody()方法,因此 getParam()永远不会调用,我建议你扩展< strong> StringRequest 而不是JsonObjectRequest。

您可以查看this答案了解详情。

顺便提一下,你有另一种选择:基于Volley的Netroid提供更多功能。

答案 1 :(得分:1)

如果要将Map作为Json对象发送(使用类JsonObjectRequest),则应将其放在JsonobjectRequest构造函数中:

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

JsonObjectRequest jsonObjReq = new JsonObjectRequest(url, new JSONObject(params), ..., ...);

请参阅this tuto

答案 2 :(得分:0)

这个对我有用(见Volley JsonObjectRequest Post parameters no longer work

String url = "https://www.youraddress.com/";

Map<String, String> params = new HashMap();
params.put("first_param", 1);
params.put("second_param", 2);

JSONObject parameters = new JSONObject(params);

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, parameters, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        //TODO: handle success
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        //TODO: handle failure
    }
});

Volley.newRequestQueue(this).add(jsonRequest);