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);
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With