How to make volley post string request with attaching headers? - android

I google and I tried to post string request by attaching headers. But I always get auth failure error.It seems my header is not setting properly. Here is my code
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("response",response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (error!=null){
error.printStackTrace();
}
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("client_id=",getString(R.string.client_id));
params.put("&client_secret=",getString(R.string.client_secret));
params.put("&grant_type=","authorization_code");
params.put("&code=",accessToken);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("Authorization", "Basic " + base64);
params.put("Content-Type", "application/x-www-form-urlencoded");
params.put("Accept", "*/*" );
return super.getHeaders();
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
I attached headers in getHeaders() and stream writer in getParams().Can somebody help me to resolve this.Thanks in advance.

Dont put params like this
params.put("&grant_type=","authorization_code");
replace like this
params.put("grant_type", authorization_code);
Dont add & and = sign it will automatically included.
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("client_id",getString(R.string.client_id));
params.put("client_secret",getString(R.string.client_secret));
params.put("grant_type","authorization_code");
params.put("code",accessToken);
return params;
}
try header like this
#Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap();
headers.put("appId", "MYAPP_ID_HERE");
return headers;
}

Related

Volley is not working with SOAP api in Android

Hi All I am using Volley and trying to parse data from SOAP api. Following is my snippet code, when I try to parse data it is giving error. Can any one help me with this? I am getting following error every time.
E/Volley: [7440] BasicNetwork.performRequest: Unexpected response
code 400 for
public void HttpPOSTRequestWithParam() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://www.myweb.co.ke/Wt/webtask.asmx?op=GetProductListing";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
Log.e("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR","error => "+error.toString());
}
}
) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("UserName", "John");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("User Name", "1234");
params.put("Password", "4321");
return params;
}
};
queue.add(postRequest);}
You may need to tell it how the Content-Type and Accept headers are encoded. Here the parameter string should look like:
'grant_type=password&username=apikey&password=apipassword'
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", "apikey");
params.put("password", "apipassword");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/x-www-form-urlencoded; charset=UTF-8");
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
return headers;
}

How to debug "Your User ID is invalid. Kindly use a valid API Key and User ID combination" in Android?

I am new to Android, I am getting this in my logcat:
"status":false,"msg":"Your User ID is invalid. Kindly use a valid API Key and User ID combination"
Code:
public class MainActivity extends AppCompatActivity {
String url = "https://json.astrologyapi.com/v1/monthly_panchang";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", error.toString());
}
}
){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
params.put("username","602845");
params.put("password","dea8c55922286904e4c34757ed1fcf09");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Authorization", "Token <token>");
return params;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(postRequest);
}
}
You need to put your API auth token here:
params.put("Authorization", "Token <token>");
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Username", "602826");
headers.put("Password", "cb");
String credentials = "602826" + ":" + "cb";
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(),
Base64.NO_WRAP);
headers.put("Authorization", auth);
// headers.put("Content-Type", "application/json");
return headers;
}
i have solved my problem.

Sending form data in volley post request

The postman response is this image:
This is the code i am using to send the data in the post request. Although i am getting a 400 response code from this.
StringRequest stringRequest = new StringRequest(Request.Method.POST,
API.ADD_PAYMENT,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
onBackPressed();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(AddPaymentActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("amount", "123");
params.put("description", "Not Paid");
params.put("customer", "1");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=UTF-8");
headers.put("Authorization", "token 0ee1248c5a84e8b1e36a8a15da48c0bb7580926c");
return headers;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(AddPaymentActivity.this);
requestQueue.add(stringRequest);
First of all why don't you use JsonObjectRequest instead of StringRequest when you're using application/json as your talking language with server? Second have you tried passing your parameters to the other request constructor like below?
Map<String, Object> params = new HashMap<>();
params.put("amount", 123);
params.put("description", "Not Paid");
params.put("customer", 1);
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.POST, API.ADD_PAYMENT, new JSONObject(params), new Response.Listener<JSONObject>() {
...
Passing your headers are essential so the rest of you code remains as it is.

volley unable to send data to server

I am using this code
StringRequest stringRequest = new StringRequest(Request.Method.POST,
uploadUrl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("sfhyoutubeghhj",response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//loading.dismiss();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> prams = new HashMap<>();
prams.put("aaaa", "1111");
prams.put("bbbb", "2222");
return prams;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
};
RequestQueue requestQueuea = Volley.newRequestQueue(this);
requestQueuea.add(stringRequest);
Request is going to server and response is also coming but its not sending any variable from android either in GET or POST method
and i m just using
print_r($_REQUEST);
at PHP end
I think there was a problem with Request queue only
working fine with this code # Ali Azhar's Answer
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, ""+error.getMessage()+","+error.toString());
}
}){
#Override
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("id", "28");
params.put("value", "1");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("abc", "value");
return headers;
}
};
AppController.getInstance().addToRequestQueue(sr);

Volley Post Request for a file that is need password

I set a password on my prefer directory on server with an username that anyone can not open and read that file. For opening and reading that file, web browser prompt a for that requests username and password. How i can set username and password in Volley Post Params?
StringRequest sr = new StringRequest(Method.POST,url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
pages = PageParser.parse(response);
adapter = new DailyReportAdapter(MenuDailyReport.this,
pages);
lv.setAdapter(adapter);
pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// error.printStackTrace();
pDialog.hide();
Toast.makeText(MenuDailyReport.this,
"Could not connect to server"+error.getMessage(), Toast.LENGTH_LONG)
.show();
finish();
}
})
{
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username","mohammad" );
params.put("password","mohammad123" );
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
;
};
Create your HashMap and return that from getParams()
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
Map params = new HashMap<String, String>();
params.put("username", userName);
params.put("password", password);
return params;
};

Categories

Resources