Android Developer Console How to send a HTTP Request? - android

I have a little app with a leaderboard and i want to hide players with fake scores. I read about it at https://developers.google.com/games/services/management/api/players/hide#request
The Problem is, that i have no idea from http Requests and that things.
So how do i send a HTTP Request? Is there a Terminal or something in the Developer Console from Google, where i put my command in?
Or what do i need to do, to send an Request like this?

I recommend that you use Volley
Add Volley to your project through Gradle
compile 'com.android.volley:volley:1.0.0'
Add the android.permission.INTERNET permission to your app's manifest.
The code is taken from 1
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com"; //set your web call here
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//handle success
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//handle error
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

Related

Android JSON: Unable to get response

I am trying to fetch a JSON response from OpenWeatherAPI to incorporate the current weather in my app. I have used volley to make a simple request to fetch the JSON response, but every time, I do not get the response. Instead, it always triggers the onErrorResponse method. What do I change to make this work?
I have added the uses Internet Permission in my manifest.
I have tried the solution to fetch JSON responses from many sources including the Official Android Developers Documentation, Other Questions from Stack Overflow, etc, but all of them failed.
I first used JSONObjectRequest instead of StringRequest, but even that did not provide me the results I required.
/*
Create a request queue to fetch the JSONObject response.
*/
RequestQueue queue = Volley.newRequestQueue(Objects.requireNonNull(getContext()));
/*
JSON Object request.
*/
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "No JSON", Toast.LENGTH_SHORT).show();
}
});
queue.add(request);
I expect the Toast to show the response, but The Toast shows "No JSON".
Add "https://" before your URL. You will get a response on your browser, but will not in Android.

Volley JSONObjectRequest returns nothing

I want to use Volley to get a JSON response from some website, so I started testing it. Here is my code plain and simple:
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, "http://api.androidhive.info/volley/person_object.json", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
json = response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
error.printStackTrace();
}
});
int x = json.length();
After the request is made the response is always null. Neither an error is raised neither the request succeeds. Which is really confusing. As you can see I am assigning the value of the response to a variable named json which is of the same type. When I debug the application by putting a breakpoint on the onResponse method, onErrorResponse method and on the last line, the debugger only hits the last line the variables watches indicate that the value of the response is null.
I have tried more than one URL
http://simplifiedcoding.16mb.com/UserRegistration/json.php
https://androidtutorialpoint.com/api/volleyString
I have added Volley via gradle
compile 'com.android.volley:volley:1.0.0'
Put it in a RequestQueue
RequestQueue queue = Volley.newRequestQueue(this);
Then
queue.add(jsObjRequest);
Or
ApplicationController.getInstance().addToRequestQueue(jsObjRequest);
You need to add the request to the queue for the asynchronous request to work.
RequestQueue requestQueue= Volley.newRequestQueue(this)
requestQueue.add(jsObjRequest);

Volley 301/302 redirects, use okhttp or modify Volley Library

I am having an issue with Volley and 301/302 redirects, specifically from http -> https. I am making API calls to a server that in the near future will be issuing a 301 redirect when calling http instead of https.
I am getting a ServerError on Response.ErrorListener.
I have read some solutions here, but most of them are 1+ years old.
What is the best option?
Modify the Volley code to handle the 301/302 in the BasicNetwork class and Request class.
Use an OkHttp3 stack instead of the default HttpStack of Volley. If so which one is recommended. I have seen multiple implementations.
Code Sample
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://www.server.com/api-path, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
VolleySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
I am getting VolleyError of com.android.volley.ServerError
with networkResponse.statusCode = 301. The server is returning the 301 with the location of https://www.server.com/api-path

How to handle the response of Volley in Android

I use volley in the Android Activity, and make a request and got the response, but I want to handle the response maybe in an another method,but it won't work, what should i do ?
public class TestActivity extends Activity {
RequestQueue queue;
private String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "www.google.com/something/I/need";
queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Do something with the response
Log.i("resp", response);
// I want to do sth with the response out of here
// maybe like this, let result = response
// and see the log at the end of the code
// but it failed, what should I do?
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
Log.e("error", error.toString());
}
});
queue.add(stringRequest);
Log.e("result", result);
}
The Volley requests are asynchronous, so the program after sending the request, continues execution without waiting for the answer. So the code that processes the result is inserted into the OnResponse method. For more precise help explain why you would like to log out of the method OnResponse
Think about what you're doing: You're creating a StringRequest, then you add it to the request queue, but then you immediately try to check the result. Obviously, this won't work because the request hasn't been processed yet.
Your response will arrive in the onResponse method, and only then you'll be able to do something with it. You can set result = response here, but you'll only be able to see the value when the onResponse is called which could take some time.
Hope this clarifies things.

Rest Service Access inside fragment class

In my application i need to display the data in list view from rest service . I have gone through many samples but am not satisfied with that so can somebody help me ? Need sample or explanation . Thanks in advance!
In android you can use libraries to consuming REST
Volley
Retrofit
Your question is very general. Try one from above.
Sample use Volley:
in gradle: compile 'com.mcxiaoke.volley:library:1.0.19'
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
you can call api in fragments.
if you are new to android,
1)first fetch the data from api with a asynctask(check android json parsing with url in learn2crack).
2)after step 1 you have your data to populate to your list,if you want to make a custom list the same site also gives an example for android custom list(learn2crack)
you can call json on asynctask class.
call the async task in onCrateView of your fragment

Categories

Resources