Android:Volley HTTP Request自定义标题

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

我需要传递一个电子邮件,一个密码和一个令牌来访问这个URL,但是它不起作用

我上周开始学习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); } 

JsonObjectRequest是扩展的JsonRequest ,它直接覆盖了getBody()方法,所以你的getParam()永远不会调用,我推荐你扩展StringRequest而不是JsonObjectRequest。

你可以检查这个答案的更多细节。

顺便说一句,你有另一种select:基于Volley的Netroid提供了更多的function。

如果你想发送你的地图作为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), ..., ...); 

看到这个tuto

这一个为我工作(看到Volley JsonObjectRequest后参数不再工作 )

 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);