I am trying to establish connection with wit.AI using Volley . However, all i am getting is a 400 error.
public void makeRequest(String url) {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, "YOU'RE TOAST", Toast.LENGTH_SHORT).show();
Log.d("accessToken:", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("error:", volleyError.toString());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization:", "Bearer SAMPLETOKENHERE");
return headers;
}
};
queue.add(postRequest);
}
I found my issue . It was because I put a ":" in my authorization . I didn't know I did not needed it. It works now
Related
I have API in http://202.158.68.155:7890/cobra/api/getDataByNopol
if i test in postman its work, but in android volley error null
final RequestQueue requestQueue= Volley.newRequestQueue(getActivity());
StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://202.158.68.155:7890/cobra/api/getDataByNopol", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
hideDialog();
Log.e(TAG, "Response Detail -> "+response);
requestQueue.stop();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("ERROR", error.getMessage());
Toast.makeText(getActivity(),"ERROR "+error.getMessage(), Toast.LENGTH_LONG).show();
requestQueue.stop();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> param = new HashMap<String, String>();
param.put("nopol", "L 6967 TJ");
param.put("username", "uat2");
param.put("token", "02E184D3C82B6DA4750A214C4");
return param;
}
};
requestQueue.add(stringRequest);
AppController.getInstance().addToRequestQueue(stringRequest, tag_json);
}
how to handle this case ?
I am trying to send json data to a url in the format below.
{
“amount”:500,
“merchant_id”:”104”,
“narrative”:”John”,
“reference”:”Steven”
}
To send data to the url, one needs an authorization key as the header, I have already figured out how to set the authorization key as the header in Params, as shown below
#Override
public Map<String,String >getHeaders()throws AuthFailureError{
Map<String,String >params=new HashMap<String,String>();
params.put(“Content-Type”, “text/jsom;);
params.put(“AuthKey”, Auth);
return params;
but I do not know how to send the data in the particular format shown in the first instance to the url using params with volley. Please help, I am quite new to using Volley library .
This is the rest of code I am currently using it however does not return any response except for an invalid json error. Meaning the format sent is not corresponding to the one desired.
StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(Main9Activity.this, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Main9Activity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}){
#Override
protected Map<String, String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("amount","500");
params.put("merchant_id", "104");
params.put("narrative","John");
params.put("reference", "Steven");
return params;
}
#Override public Map<String,String>getHeaders()throws AuthFailureError{
Map<String,String>headers=new HashMap<>();
params.put("Content-Type","text/json");
params.put("Authorization",Auth);
return params;
}
};
RequestQueue requestQueue=Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
You can refer below code snipet to post data with custom header in volley.
JSONObject jsonobject = new JSONObject();
jsonobject.put("amount", "500");
jsonobject.put("merchant_id", "104");
jsonobject.put("narrative", "John");
jsonobject.put("reference", "Steven");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST,url, jsonobject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
Toast.makeText(Main9Activity.this, response.toString(), Toast.LENGTH_SHORT).show();
hideProgressDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(Main9Activity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
hideProgressDialog();
}
}) {
/**
* Passing some request headers
* */
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String >headers=new HashMap<String,String>();
headers.put(“Content-Type”, “application/json“);
headers.put(“AuthKey”, Auth);
return headers;
}
You can send as below:
The data you want to send:
Hashmap<String, Object> data = new HashMap<>();
data.put("amount", "500");
data.put("merchant_id", "104");
data.put("narrative", "John");
data.put("reference", "Steven");
RequestQueue request = Volley.newRequestQueue(context);
JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, "your url",new JSONObject(data),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
){
};
request.add(jsonobj);
I have installed a Let's Encrypt SSL certificate on my server. What are the required steps to perform an HTTPS request from my Android app using Volley. This is my current code:
RequestQueue queue = Volley.newRequestQueue(this);
String url = getString(R.string.server_url);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new com.android.volley.Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new 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("imgUrl", mEdit.getText().toString());
params.put("token", userIdToken);
return params;
}
};
queue.add(postRequest);
What should i add to this code?
i want to asking about request in android. I made a request, but when executed, it stores 2 data in the database. I have no idea why this doesn't work well.
For information, i use php to handle request. and not only on my web apps. When i try using another web apps, it still save 2 data.
this is my code, thanks.
public void sendToServer(){
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest URL = new StringRequest(Request.Method.POST, storeCC, new Response.Listener<String>(){
#Override
public void onResponse(String response) {
Log.d("SUKSES", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error gan", error.toString());
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("poin", point);
jsonParams.put("id_imei",id_imei);
jsonParams.put("billingNm", name);
jsonParams.put("harga", String.valueOf(amt));
return jsonParams;
}
};
RequestHandler.getInstance(this).addToRequestQueue(URL);
}
Hope you help me.
Your problem may arise if you use "https".Add Handler over there and delay the request as Https often response late, Caution you have to manage every request.
Handler handler= new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Your code after 5 seconds delay
sendToServer();
}
},5000);
Try this solution where setRetryPolicy of Volley Request:-
public void sendToServer(){
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, storeCC, new Response.Listener<String>(){
#Override
public void onResponse(String response) {
Log.d("SUKSES", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error gan", error.toString());
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("poin", point);
jsonParams.put("id_imei",id_imei);
jsonParams.put("billingNm", name);
jsonParams.put("harga", String.valueOf(amt));
return jsonParams;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(5*1000,-1,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
}
I'm trying to post data to website. It is login operation. I check form data hidden tag etc. In form data there is a hidden input _RequestVerificationToken. That's why first, I made get request to parse _RequestVerificationToken and for headers.
StringRequest _StringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Parsing Process
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
)
{
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
headers=response.headers;
return super.parseNetworkResponse(response);
}
};
After all i made post request with parameters and headers:
#Override
public Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("Password", password);
params.put("RememberMe", "false");
params.put("ReturnUrl", url_post);
params.put("UserName",username);
params.put(name,_RequestVerificationToken);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
I check form data by using fiddler extension in chrome. I checked parameters again and again but it returned code 400. Can you show me what is the problem?
Change Request.Method.GET to Request.Method.POST
StringRequest _StringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Parsing Process
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
)
{
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
headers=response.headers;
return super.parseNetworkResponse(response);
}
};
Or You can use this code:-
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
returnValue = response;
try {
parseFromUrl(response);
System.out.println(returnValue);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(app,error.toString(),Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Password", password);
params.put("RememberMe", "false");
params.put("ReturnUrl", url_post);
params.put("UserName",username);
params.put(name,_RequestVerificationToken);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(app);
requestQueue.add(stringRequest);
I solve the problem with this class and I added this:
CookieManager cookieManager = new CookieManager(new PersistentCookieStore(this), CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
before add request to queue