Volley bad URL - how to fix that? - android

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.

Related

Upload some string to server and download with volley

In my app, I need some string to be downloaded from server to use in the app. How can I upload the strings to the server?.
I've uploaded a text file at first but when sending request I received this error in logcat:
Unexpected response code 307 for:
Also, I uploaded my text in a web page body same error happened.
Please help me how to upload some text or an ArrayList to the server and download with volley and use in the app.
This is my volley request method:
private void getOnlinePrice (){
StringRequest request=new StringRequest(Request.Method.GET,URI_SHOW_PARAMS, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
String s=response;
txtinfo.setText(s);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
You can do this:
String HTTP_URL = "YOUR URL";
RequestQueue requestQueue = Volley.newRequestQueue(your_activity.this);
// sends data using POST method
StringRequest postRequest = new StringRequest(Request.Method.POST, HTTP_URL,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
String resp = response;
if (!TextUtils.isEmpty(resp))
{
Toast.makeText(getApplicationContext(), "my response is" + resp,
Toast.LENGTH_SHORT).show();
}
else{
// if the response if empty
Toast.makeText(getApplicationContext(), "my response is empty",
Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error
}
}
) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("POST_VARIABLE_1", YourNamestring);
params.put("POST_VARIABLE_2", YourNameString2);
return params;
}
};
requestQueue.add(postRequest);

Make HTTPS request on Android with Volley

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?

Android Communicate with wit.ai

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

Android Make one request with post method, but it save 2 data in database

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);
}

Android Volley POST string in body

I'm trying to use Volley library to communicate with my RESTful API.
I have to POST string in the body, when I'm asking for the bearer Token. String should look like this:
grant_type=password&username=Alice&password=password123
And header:
Content-Type: application/x-www-form-urlencoded
More info about WebApi Individual Accounts:
http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
Unfortunately I can't figure out how can I solve it..
I'm trying something like this:
StringRequest req = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
VolleyLog.v("Response:%n %s", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", "User0");
params.put("password", "Password0");
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");
return headers;
}
};
I'm getting 400 Bad Request all the time.
I think that I'm actually sending request like this:
grant_type:password, username:User0, password:Password0
instead of:
grant_type=password&username=Alice&password=password123
I would be very grateful if anyone has any ideas or an advice..
To send a normal POST request (no JSON) with parameters like username and password, you'd usually override getParams() and pass a Map of parameters:
public void HttpPOSTRequestWithParameters() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = 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) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
// volley will escape this for you
params.put("randomFieldFilledWithAwkwardCharacters", "{{%stuffToBe Escaped/");
params.put("username", "Alice");
params.put("password", "password123");
return params;
}
};
queue.add(postRequest);
}
And to send an arbitary string as POST body data in a Volley StringRequest, you override getBody()
public void HttpPOSTRequestWithArbitaryStringBody() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = 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) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
#Override
public byte[] getBody() throws AuthFailureError {
String httpPostBody="grant_type=password&username=Alice&password=password123";
// usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it
try {
httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8");
} catch (UnsupportedEncodingException exception) {
Log.e("ERROR", "exception", exception);
// return null and don't pass any POST string if you encounter encoding error
return null;
}
return httpPostBody.getBytes();
}
};
queue.add(postRequest);
}
As an aside, Volley documentation is non-existent and quality of StackOverflow answers is pretty bad. Can't believe an answer with an example like this wasn't here already.
First thing, I advise you to see exactly what you're sending by either printing to the log or using a network sniffer like wireshark or fiddler.
How about trying to put the params in the body? If you still want a StringRequest you'll need to extend it and override the getBody() method (similarly to JsonObjectRequest)
I know this is old, but I ran into this same problem and there is a much cleaner solution imo found here: How to send a POST request using volley with string body?

Categories

Resources