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?
Related
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'm trying to connect my android app to Spotify APIS.
Now I'm able to login, and play some random music with URL songs. My problem is I'm not able to connect using GET. And my only one response is:
E/Volley: [7721] BasicNetwork.performRequest: Unexpected response code 401 for
StringRequest stringRequest = new StringRequest(Request.Method.GET, URLline,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
parseData(response);
System.out.println("respuesta: " + response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
params.put("Content-Type", "application/json");
params.put("Authorization", "Bearer BQCljGDNl4xBRbSnJgl8O8tZU-OUIGGpBFsYZTD_QxcrHI-JN8-cFbfiewHTmSIlzMMuvRaX5lsPtDFwFsx_pNq37DEH8jll8sd9jXs6gPXGGTON0LOlzbzPXhAVz6mG_Ta-6BakiVo5C-MAjVL8UC9r9Hl8xIR_en9-tmpfCTKccEKI");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
I think the problem is that you are sending the headers as GET parameters, you should override getHeaders().
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("xxx", "xxx");
return params;
}
I'm having a problem with java volley when I click login
private void Login(){
String url = "api.matraindonesia.com/login";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.trim().equals("success")){
Toast.makeText(getApplicationContext(),"Login Successfully!",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Login Failed!",Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"this error:"+ error.toString(),Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email",etEmail.getText().toString().trim());
params.put("password",etPassword.getText().toString().trim());
return super.getParams();
}
};
requestQueue.add(stringRequest);
}
com.android.volley.VolleyError: java.lang.RuntimeException: Bad URL
you need to use http:// or https:// at the beginning or the url
String url = "http://api.matraindonesia.com/login";
or if your server is on SSL
String url = "https://api.matraindonesia.com/login";
Edit (after your comment) :
typically MethodNotAllowedHttpException happen when, route method is not match.
Suppose you define POST request route file, but you sending GET Request to the route.
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
I want to know how to send volley login request if the login request url is
username:password http://login_url
in this format. It would be a great help if a sample code is available.
the purpose is to send login request to a django framework based website.
Thanks in advance
first create a string request ->>
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
});
then, you should override the getParams() method!
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,username);
params.put(KEY_PASSWORD,password);
params.put(KEY_EMAIL, email);
return params;
add the request to request queue!
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
three easy steps :)
feel free to ask any doubts
try this
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";
String url = "http:login_url";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//your response
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "username");
params.put("email", "abc#dffg.info");
params.put("password", "password123");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);