Android: Volley HTTP Request custom header
Asked Answered
E

3

6

I'm getting the following error when I run the app: BasicNetwork.performRequest: Unexpected response code 401

I need to pass an email, a password and a token to access the URL, but it's not working

I started learn android last week, I don't know much

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", "[email protected]");
                params.put("senha", "test");
                params.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0afd19MfacGf3FFm8CM1hG0eDiIk8");

                return params;
            }
        };

        mRequestQueue.add(jsonObjReq);
    }
Epiphyte answered 18/6, 2014 at 12:24 Comment(0)
S
15

The JsonObjectRequest is extended JsonRequest which override getBody() method directly, so your getParam() would never invoke, I recommend you extend StringRequest instead of JsonObjectRequest.

your can check this answer for more details.

by the way, you've another choice : Netroid, that based Volley, offered more features at all.

Sweetie answered 25/6, 2014 at 7:14 Comment(1)
same prm for me also, but i must wants to send the JSON object to server . so how can i solve this problem??Emia
K
1

If you want to send your Map as a Json object (you are using the class JsonObjectRequest), you should put it in the JsonobjectRequest constructor :

HashMap<String, String> params = new HashMap<String, String>();
params.put("email", "[email protected]");
params.put("senha", "test");
params.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0afd19MfacGf3FFm8CM1hG0eDiIk8");

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

See this tuto

Kilgore answered 18/6, 2014 at 12:41 Comment(0)
P
0

this one worked for me (seen on 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);
Picoline answered 4/4, 2017 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.