Fast-android-networking: how to get return value onResponse or onError ?

Created on 14 Feb 2017  路  11Comments  路  Source: amitshekhariitbhu/Fast-Android-Networking

i must admit im new in java...
sorry for including this in issues hope it would help others inexperience people who using this library.
how to get return value in void onResponse Method ?

i have already using OptimusHttp that using Volley in their base connection library. as i know on that library it would support to return any value from OnSuccess method by using Interface Callback.

can u help me how to accomplish it using this library ?


 api.get(this.host+"login")
                .setPriority(Priority.HIGH)
                .build()
                    .getAsJSONObject(new JSONObjectRequestListener() {
                        @Override
                        public void onResponse(JSONObject response) {

                            try {
                                     // need to return value here such as 
                                   **return response;  --> error**                      
                           } catch (Exception e) {
                                System.out.println(e.toString());
                            }
                        }

                        @Override
                        public void onError(ANError anError) {
                                messageToast("error");
                        }

                });

thanks u

question

Most helpful comment

This library always does the network in the background thread and return in the main thread by default.

All 11 comments

As the network call happens in another thread, you can not pass like that.
Suppose you have the class Networking

public class Networking{
   public static void makeRequest(JSONObjectRequestListener listener){
      AndroidNetworking.get(url)
                                      .build()
                                      . getAsJSONObject(listener);
   }
}

Let say you are calling from another class.

public class YourClass{
  void doSomething(){
   Networking.makeRequest(new JSONObjectRequestListener() {
                        @Override
                        public void onResponse(JSONObject response) {

                        }
                        @Override
                        public void onError(ANError anError) {

                        }
                });
  }
}

_"As the network call happens in another thread, you can not pass like that.
Suppose you have the class Networking"_ thanks for your explanation on the thread..

it works...

is it by using this code :


`public class YourClass{
  void doSomething(){
   Networking.makeRequest(new JSONObjectRequestListener() {
                        @Override
                        public void onResponse(JSONObject response) {

                        }
                        @Override
                        public void onError(ANError anError) {

                        }
                });
  }
}`

using above code u recommend am i doing http request on the main thread ?? is it good practice ?

This library always does the network in the background thread and return in the main thread by default.

oke thanks u so much...

sorry to interrupt you, and sorry to open this again...

this not really problem with this library but im using this library to doing http. i got problem when trying to place the return value from onResponse method here the screen shot, the error in the yellow colored pen circle. here i let you check my screenshot. please give me any suggestion to solve this. thank

cc

thanks for your support

If you are using static, use FirstTab.jadwalist, by the way use default parser refer this link
https://amitshekhariitbhu.github.io/Fast-Android-Networking/get_request.html#getting-parsed-object-list-as-response

thanks for your response:
the method that your referring me seem work if using this json format :

{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",

},

{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "[email protected]",

}

but in my case i should extract the model from data array format :

{
"page": "2",
"per_page": 3,
"total": 12,
"total_pages": 4,
"data": [
{
"id": 4,
"first_name": "eve",
"last_name": "holt",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
},
{
"id": 5,
"first_name": "gob",
"last_name": "bluth",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
}
]

}

how to get those data array if using this method : https://amitshekhariitbhu.github.io/Fast-Android-Networking/get_request.html#getting-parsed-object-list-as-response

thank u for your support sir amitshekhariitbhu 馃憤

class Person{
 long id;
 String first_name;
 String last_name;
 String avatar;
}

class Response{
 int total;
 List<Person> data;
}

and use getAsObject(Response.class, new Parse)

and onResponse Method in this library seems like still in background Thread sir...

how can i move the result from onResponse method (that done in background thread) the the UI or Main thread. your solution to converting my variabel to static is not work when i want to use it outside public void onResponse(JSONObject response) scope.

seem like this library can transport from background process to main process. Should i use this library to make any variabel in background thread available to main thread ?

https://github.com/Arasthel/AsyncJobLibrary

thanks u

Create and setAdapter inside the onResponse as onResponse returns in main thread after doing work in background.

thank u sir.
_"onResponse returns in main thread after doing work in "_
this line help me understand the thread involved on onResponse method
i really appreciate for all your answer and your time sir @amitshekhariitbhu

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SunryTeang picture SunryTeang  路  4Comments

Patrick-Wallin picture Patrick-Wallin  路  8Comments

rezasadeghi93 picture rezasadeghi93  路  8Comments

Ali-Khazaee picture Ali-Khazaee  路  5Comments

ahaneef29 picture ahaneef29  路  7Comments