textView = findViewById(R.id.textVieww);
String url = "https://zenquotes.io/api/random";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
final String myResponse = response.body().string();
try {
JSONArray jsonarray = new JSONArray(myResponse);
for(int i=0; i<jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
Quote.this.runOnUiThread(() ->
textView.setText(myResponse));
}
}
});
}
This is the part im stuck on i think im on the right track but not sure where to go from here im trying to get the "q" information from the returned url and the "a" information but it just outputs everything any suggestions?
What was your problem
Even when you parsed JSON string, you were still using the myResponse string in your textView.setText() method.
Continuing your code snippet
your code snippet is quite short, but i do think i can quite understand what you mean.
So let's say that we have Activity, which is called MainActivity and in that activity we have two views, one TextView called that has an id of tv_author_and_quote and one Button which has a xml id btn_request_quote.
The button has an OnClickListener which calls method requestForQuote().
Our onCreate + the variables of Button and TextView looks like this:
TextView tvAuthorAndQuote;
Button btnRequestQuote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvAuthorAndQuote = findViewById(R.id.tv_author_and_quote);
btnRequestQuote = findViewById(R.id.btn_request_quote);
btnRequestQuote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
requestForQuote();
}
});
}
And then we have a code itself for method requestForQuote():
public void requestForQuote() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(#NotNull Call call, #NotNull IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(#NotNull Call call, #NotNull Response response) throws IOException {
if (response.isSuccessful()) {
final String myResponse = Objects.requireNonNull(response.body()).string();
String myFormattedQuote = "";
try {
JSONArray jsonarray = new JSONArray(myResponse);
for(int i=0; i<jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
String quote = obj.getString("q");
String author = obj.getString("a");
Log.d(TAG, "onResponse: quote:" + quote);
Log.d(TAG, "onResponse: author:" + author);
myFormattedQuote = author + ": " + quote;
}
} catch (JSONException e) {
e.printStackTrace();
}
final String myFinalQuote = myFormattedQuote;
MainActivity.this.runOnUiThread(() -> {
if (!myFinalQuote.equals("")) {
tvAuthorAndQuote.setText(myFinalQuote);
} else {
tvAuthorAndQuote.setText(myResponse);
}
});
}
}
});
}
The code above basically uses your existing solution, but instead of setting the text of textView with myResponse string, it parses the json array and gets a quote and an author from it. Then it just logs it (just for testing purposes), then it constructs the string which gets displayed to the if there is any, otherwise it prints the response. That it is.
Using Gson library
import it into your gradle dependecies
implementation 'com.google.code.gson:gson:2.8.7'
Write short "holder" class called Quote
public class Quote {
public Quote() {
}
String q;
String a;
String h;
public String getQ() {
return q;
}
public void setQ(String q) {
this.q = q;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getH() {
return h;
}
public void setH(String h) {
this.h = h;
}
#NonNull
#NotNull
#Override
public String toString() {
return a + ": " + q;
}
}
Then the requestForQuote() method could look something like this:
public void requestForQuoteWithGson() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(#NotNull Call call, #NotNull IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(#NotNull Call call, #NotNull Response response) throws IOException {
if (response.isSuccessful()) {
final String myResponse = Objects.requireNonNull(response.body()).string();
Type listType = new TypeToken<ArrayList<Quote>>(){}.getType();
List<Quote> yourClassList = new Gson().fromJson(myResponse, listType);
if (yourClassList != null && yourClassList.size() > 0) {
final Quote quote = yourClassList.get(0);
if (quote != null) {
myQuotes.add(quote);
MainActivity.this.runOnUiThread(() ->
tvAuthorAndQuote.setText(quote.toString())
);
}
}
}
}
});
}
Related
I need to make about 60 HTTP requests.
In the first case, I did not use an asynchronous request and the speed was about 1.5 minutes.
In the second case, I used an asynchronous request and the speed did not change either and was about 1.5 minutes.
Please see my code. Maybe I'm not doing the asynchronous request correctly or is there some other way to quickly make a lot of HTTP requests?
public class Main {
public static int page = 0;
public static void main(String[] args) throws IOException {
int totalPages = Util.getTotalPages();
page = 0;
while(page < totalPages) {
// Function does not work
new GetAuctions();
page++;
}
}
}
public class Util {
public static final String API_KEY = "***";
public static final OkHttpClient httpClient = new OkHttpClient();
public static final List<JSONObject> auctions = new ArrayList<>();
public static int getTotalPages() throws IOException {
Request request = new Request.Builder().url("https://api.hypixel.net/skyblock/auctions?key=" + Util.API_KEY + "&page=0").build();
Response response = httpClient.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Error: " + response);
assert response.body() != null;
String jsonData = response.body().string();
JSONObject object = new JSONObject(jsonData);
return object.getInt("totalPages");
}
}
public class GetAuctions {
public static void main(String[] args) throws Exception {
new GetAuctions().run();
}
public void run() throws Exception {
Request request = new Request.Builder().url("https://api.hypixel.net/skyblock/auctions?key=" + Util.API_KEY + "&page=" + Main.page).build();
Util.httpClient.newCall(request).enqueue(new Callback() {
#Override public void onFailure(#NotNull Call call, #NotNull IOException e) {
e.printStackTrace();
}
#Override public void onResponse(#NotNull Call call, #NotNull Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
assert response.body() != null;
String jsonData = response.body().string();
JSONObject object = new JSONObject(jsonData);
JSONArray array = object.getJSONArray("auctions");
for (int i=0; i<array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
if (jsonObject.has("bin")) {
Util.auctions.add(jsonObject);
}
}
System.out.println(Util.auctions.size());
}
});
}
}
It doesn't look like your example is asynchronous at all. Look at the example from https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/AsynchronousGet.java and try with that.
Specifically you should be calling enqueue instead of execute.
client.newCall(request).enqueue(new Callback() {
#Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override public void onResponse(Call call, Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(responseBody.string());
}
}
});
}
In my app I have a spinner that includes some language abbreviation example (en, az, and others), I want to set selected language abbreviation as the request header and I need this in every request so I am saving abbreviation in shared preferences and get abbreviation in my ApiClient class,
every time that spinner selection has changed I change shared preferences abbreviation value but header only set the first time I select a language and when I change spinner selection header not change
This is my ApiClient class
private static final String BASE_URL = ApiUrls.server_url;
public static Retrofit getClient(Context context)
{
SharedPrefUtil sharedPrefUtil = new SharedPrefUtil(context);
String locale = sharedPrefUtil.getSelectedLanguage();
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient = httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException
{
Request request = chain.request().newBuilder().header("Accept-Language",
locale).build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).client(httpClient.build())
.build();
return retrofit;
}
This is my ApiRequester class method for sending a request to server
public static void sendLogin(final Context context, Map<String, String> params,
final HttpResponses.onLoginSuccess onLoginSuccess) {
DialogHelper.ProgressDialog progressDialog = showWaitDialog(context);
if (hasInternetConnection(context)) {
params.put("grant_type", "password");
params.put("client_id", "raymon-client");
params.put("client_secret", "raymon-secret");
ApiInterface apiService = ApiClient.getClient(context).create(ApiInterface.class);
Call<ResponseBody> call = apiService.loginRequest(params);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
progressDialog.dismiss();
//result ok
if (response.code() == 200)
{
try
{
onLoginSuccess.loginResponse(JsonSuccessParser.parseLoginResponse
(context, response.body().string()));
} catch (IOException e)
{
e.printStackTrace();
}
}
else if (response.code() == 403)
{
onLoginSuccess.loginError(response.code());
}
else
{
try
{
JsonErrorParser.parseServerError(context,response.errorBody().string());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
progressDialog.dismiss();
showNoConnectionDialog(context);
}
});
} else {
progressDialog.dismiss();
showNoConnectionDialog(context);
}
}
This is my code for setting language abbreviation in shared preferences
private void setUserLocale()
{
String selected_country = countryCodeAdapter.getItem(country_code_spinner.
getSelectedItemPosition()).language.abbreviation.toLowerCase();
LocaleHelper.setLocale(this,selected_country);
}
How can I change header immediately when user change spinner position?
In ApiClient class, you should initialize locale variable inside of interceptor methods to reflect your changes.
Example)
private static final String BASE_URL = ApiUrls.server_url;
public static Retrofit getClient(Context context)
{
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient = httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException
{
SharedPrefUtil sharedPrefUtil = new SharedPrefUtil(context);
String locale = sharedPrefUtil.getSelectedLanguage();
Request request = chain.request().newBuilder().header("Accept-Language",
locale).build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).client(httpClient.build())
.build();
return retrofit;
}
However, if the value changes constantly, it may be possible to explicitly pass the value of the Header as an argument to the Service instead of the Interceptor.
Example)
public interface ApiService {
Call<...> loginRequest(#Header("Accept-Language") String language, #QueryMap Map<String, String> params);
}
public static void sendLogin(final Context context, Map<String, String> params,
final HttpResponses.onLoginSuccess onLoginSuccess) {
DialogHelper.ProgressDialog progressDialog = showWaitDialog(context);
if (hasInternetConnection(context)) {
params.put("grant_type", "password");
params.put("client_id", "raymon-client");
params.put("client_secret", "raymon-secret");
SharedPrefUtil sharedPrefUtil = new SharedPrefUtil(context);
String locale = sharedPrefUtil.getSelectedLanguage();
ApiInterface apiService = ApiClient.getClient(context).create(ApiInterface.class);
Call<ResponseBody> call = apiService.loginRequest(locale, params);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
progressDialog.dismiss();
//result ok
if (response.code() == 200)
{
try
{
onLoginSuccess.loginResponse(JsonSuccessParser.parseLoginResponse
(context, response.body().string()));
} catch (IOException e)
{
e.printStackTrace();
}
}
else if (response.code() == 403)
{
onLoginSuccess.loginError(response.code());
}
else
{
try
{
JsonErrorParser.parseServerError(context,response.errorBody().string());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
progressDialog.dismiss();
showNoConnectionDialog(context);
}
});
} else {
progressDialog.dismiss();
showNoConnectionDialog(context);
}
}
This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Using GSON to parse a JSON with dynamic "key" and "value" in android
(2 answers)
Closed 5 years ago.
This is my JsonResponse , and since its not in array i am facing some difficulties , can any one help me out ? in android
{
"errorno": "0",
"responsemsg": "Login Success.",
"busid": "1234",
"returnmobileno": "1234567890"
}
try this
try {
JSONObject lJsonObject = new JSONObject(response);
String errorno = lJsonObject .getString("errorno");
String responsemsg = lJsonObject .getString("responsemsg");
String busid = response.lJsonObject ("busid");
String returnmobileno = lJsonObject .getString("returnmobileno");
} catch (JSONException e) {
e.printStackTrace();
}
Try this,
try {
String errorno = response.getString("errorno");
String responsemsg = response.getString("responsemsg");
String busid = response.getString("busid");
String returnmobileno = response.getString("returnmobileno");
Log.d(TAG, "errorno:" + errorno+" responsemsg:"+responsemsg+" busid:"+busid+" returnmobileno:"+returnmobileno);
} catch (JSONException e) {
e.printStackTrace();
}
use below code to pass your strong
serverData = gson.fromJson(response, ServerData.class);
in build.gradle -> dependencies
// retrofit, gson
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
public class ApiClient {
public static final String SERVER_BASE_URL = "http://example.com/abc/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(SERVER_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
public interface ApiInterface {
#POST("appServices/getData.php")
#FormUrlEncoded
Call<ResponseBody> getAllDataJSONFromServer(#Field("vcode") String vcode);
}
public class ServerData implements Parcelable {
public static final Creator<ServerData> CREATOR = new Creator<ServerData>() {
#Override
public ServerData createFromParcel(Parcel in) {
return new ServerData(in);
}
#Override
public ServerData[] newArray(int size) {
return new ServerData[size];
}
};
private static final int VERSION = 1;
#SerializedName("errorno")
private String errorno;
#SerializedName(responsemsg)
private String responsemsg;
#SerializedName("busid")
private String busid;
#SerializedName("returnmobileno")
private String returnmobileno;
private void readFromParcel(Parcel in) {
if (in.readInt() == VERSION) {
errorno = in.readString();
responsemsg = in.readString();
busid = in.readString();
returnmobileno = in.readString();
}
}
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(VERSION);
parcel.writeString(errorno);
parcel.writeString(responsemsg);
parcel.writeString(busid);
parcel.writeString(returnmobileno);
}
#Override
public int describeContents() {
return 0;
}
public String getErrorno() {
return errorno;
}
public void setErrorno(String errorno) {
this.errorno = errorno;
}
public String getResponsemsg() {
return responsemsg;
}
public void setResponsemsg(String responsemsg) {
this.responsemsg = responsemsg;
}
public String getBusid() {
return busid;
}
public void setBusid(String busid) {
this.busid = busid;
}
public String getReturnmobileno() {
return returnmobileno;
}
public void setReturnmobileno(String returnmobileno) {
this.returnmobileno = returnmobileno;
}
}
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
// get and save all data from server
Call<ResponseBody> call = apiService.getAllDataJSONFromServer(local_vcode, local_cvcode, pckgName);
call.enqueue(new Callback<ResponseBody>() {
#SuppressWarnings("ConstantConditions")
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> responsebody) {
try {
String response = responsebody.body().string();
serverData = gson.fromJson(response, ServerData.class); // this will fetch data to model class ServerData
if (serverData != null) {
// do the rest here...
String vcode = serverData.getVcode();
Log.e("~~~ vode = ", vcode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
progressDialog.dismiss();
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
try {
t.printStackTrace();
progressDialog.dismiss();
} catch (Exception ignored) {
}
}
});
public class VolleyStringRequest {
String url;
String body;
String value;
public VolleyStringRequest(String url, String body){
this.url = url;
this.body = body;
value= "";
}
public StringRequest createStringRequest(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Do something with the response
Log.e("Response", response);
try{
JSONObject o = new JSONObject(response);
JSONArray values=o.getJSONArray("response");
value += values.toString();
} catch (JSONException ex){}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
}) {
#Override
public byte[] getBody() throws AuthFailureError {
return body.getBytes();
};
#Override
public String getBodyContentType() {
return "application/json";
}
};
return stringRequest;
}
public String getValue() {
return value;
}
}
I wrote this code in a seperate class to prevent code repetition but when I run this inside a fragment like this:
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
String url= "http://grwn.ddns.net:1337/results";
final String body = "{\"id\":1}";
VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body);
rq.add(volleyStringRequest.createStringRequest());
volleyStringRequest.getValue();
And call the getValue() method. This method is always empty like: "". Does anyone know how I can enhance my class so this code will work? This issue is not because of a bad link or bad request. I can log the response and that does work (ofcourse inside VolleyStringRequest)
You run:
VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body);
rq.add(volleyStringRequest.createStringRequest());
volleyStringRequest.getValue();
But remember createStringRequest is async method and value is populated after some delay a.e. inside public void onResponse(String response)
So when you call volleyStringRequest.getValue(); you get empty string
To make it work you can write some interface as:
public interface RequestHandlerInterface(){
void onResponse(String resp);
}
And pass it to VolleyStringRequest constructor:
RequestHandlerInterface rh = this; //Your main class should implement this method
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
String url= "http://grwn.ddns.net:1337/results";
final String body = "{\"id\":1}";
VolleyStringRequest volleyStringRequest = new VolleyStringRequest(url, body, rh);
rq.add(volleyStringRequest.createStringRequest());
Next, change your VolleyStringRequest:
public class VolleyStringRequest {
String url;
String body;
String value;
public VolleyStringRequest(String url, String body, RequestHandlerInterface rh){
this.url = url;
this.body = body;
this.rh = rh;
value= "";
}
//...
}
And once you got response from POST, call the callback as:
#Override
public void onResponse(String response) {
// Do something with the response
Log.e("Response", response);
try{
JSONObject o = new JSONObject(response);
JSONArray values=o.getJSONArray("response");
value += values.toString();
if(this.rh != null){
this.rh.onResponse(value);
}
} catch (JSONException ex){}
}
So in bottom line instead to call volleyStringRequest.getValue();
you have:
#Override
void onResponse(String resp){
// here you go
}
that will be called when you get POST response
I know there are some identical questions but I just couldn't figure out what I'm doing wrong.
public class MainActivity extends AppCompatActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
new JsonHandler().execute(this, collection, gridArray, customGridAdapter);
...
}
}
So in my main activity I need to query an API which gives back JSON and I have to process that to build my database.
Then in doInBackground() I call getAllCards() which gets the first JSON. Because the JSON includes URLs for more JSON requests, I have a few methods each querying a more detailed JSON.
public final class JsonHandler extends AsyncTask {
private final String urlCards = "https://api.gwentapi.com/v0/cards/";
private final String urlSpecificCard = "https://api.gwentapi.com/v0/cards/:id";
private Context context;
private Collection collection;
private ArrayList<Card> gridArray;
private CustomGridViewAdapter customGridAdapter;
public JsonHandler(Context context, Collection collection, ArrayList<Card> gridArray, CustomGridViewAdapter customGridAdapter){
this.context = context;
this.collection = collection;
this.gridArray = gridArray;
this.customGridAdapter = customGridAdapter;
}
public JsonHandler(){
this.context = null;
this.collection = null;
this.gridArray = null;
this.customGridAdapter = null;
}
private void getAllCards() throws RuntimeException {
JsonObjectRequest arrayRequest = new JsonObjectRequest(Request.Method.GET, urlCards, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
generateCollection(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError e) {
throw new RuntimeException(e.getMessage());
}
});
Volley.newRequestQueue(context).add(arrayRequest);
}
private void getSpecificCard(final String cardURL) throws RuntimeException {
JsonObjectRequest arrayRequest = new JsonObjectRequest(Request.Method.GET, cardURL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
processCard(response, collection);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError e) {
throw new RuntimeException(e.getMessage());
}
});
Volley.newRequestQueue(context).add(arrayRequest);
}
private void generateCollection(JSONObject response) throws RuntimeException {
try {
JSONArray array = response.getJSONArray("results");
for(int i = 0; i < array.length();i++){
JSONObject object = array.getJSONObject(i);
String cardURL = object.getString("href");
getSpecificCard(cardURL);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
private void processCard(JSONObject response, Collection collection){
try {
String id = response.getString("id");
EnumFaction faction = EnumFaction.valueOf(response.getJSONObject("faction").getString("name").toUpperCase());
EnumType type = null;
EnumRarity rarity = null;
EnumLane lane = null;
EnumLoyalty loyalty = null;
String name = response.getString("name");
String text = response.getString("text");
String imagePath = "https://api.gwentapi.com/media/\" + id + \"_small.png";
URL url = new URL(imagePath);
InputStream inputStream = url.openConnection().getInputStream();
Bitmap image = BitmapFactory.decodeStream(inputStream);
Card card = new Card(id, faction, type, rarity, lane, loyalty, name, text, null, imagePath, 0);
collection.addCard(card);
gridArray.add(card);
customGridAdapter.notifyDataSetChanged();
} catch (Exception e){
throw new RuntimeException(e.getMessage());
}
}
#Override
protected Object doInBackground(Object[] params) {
context = (Context) params[0];
collection = (Collection) params[1];
gridArray = (ArrayList<Card>) params[2];
customGridAdapter = (CustomGridViewAdapter) params[3];
getAllCards();
return null;
}
}
So now on to the problem:
When the programm reaches processCard() when I've gathered enough information, I get a NetworkOnMainThreadException when I create the InputStream.
I've tried so many different methods to get a Bitmap from my URL and different methods to do an asynchronous task - all leading to the same result.
If you could show me how to resolve this issue, I'd be sooo happy.
Edit: Since it got marked as duplicate: I AM USING ASYNCTASK! I have looked at many questions and tried what they did there, it doesn't work!
Not really familiar with how volley works but onResponse but be on the main thread so you need to start a new thread to make that call too