How to make GET and POST requests on android, with a quick and updated tool?

Asked

Viewed 855 times

3

I am currently using the volley for web requests. But I’m finding this tool too slow.

I googled looking for alternatives, but all results I find are old, 2017, 2016 down.

About the org.apache.http They say they are obsolete, among others also obsolete.

I found little information on the Gson and from what I understand, I don’t think you even make web requests.

Some people talk about Retrofit and Retrofit 2, I haven’t tested yet.

But after all, now in 2018, what is the best way to do this? There is a way to use the native tools of android studio without installing any library and that is not obsolete?

Sorry I didn’t post any code, as I said I didn’t like Volley for being too slow. So I would really like a help on a more up-to-date tool. If possible some documentation or tutorial to use.

If I didn’t ask the question correctly please excuse me, I don’t know any other way to ask about it here. And for me here is being the only place with professionals who really understand what they’re talking about and who could help me.

I’m using the android studio 3.1.2, with minSdkVersion 15 and targetSdkVersion 27

  • 1

    Use https://developer.android.com/reference/java/net/HttpURLConnection, it is native.

  • 1

    I disagree with some things. 2017, 2016 results are not that old. If Volley is slow, it would be good to examine in search of the cause of the slowness that might not be Volley itself. As for the native library, it’s very low-level, I suggest in its place Okhttp.

  • @Valdeirpsr Thanks, from what I checked it seems that in the update of android 6.0 they say to return and use the HttpURLConnection. I’ll try to see how does the POST and GET with it. Thank you.

  • @Piovezan I think Volley is too slow because of his cache system. For older phones and slower connection it takes a long time to return. Even if I start a new empty project only with it, the answer is very slow compared to other heavy applications I have installed on my mobile phone. About the OkHttp I’ll take a look, but because in your opinion the native is very low level??

  • 1

    @Samantasilva Compare a POST request on Okhttp and Httpurlconnection, the second has much more ceremony, both to assemble the connection and to read the received data.

  • @Piovezan Ahh yes, that I imagined, for being a native tool must need a lot more lines of code. My fear is more for speed, performance, and that works on all devices, from the most current, to the oldest. About the codes for what I will do, I will use only once. So even q is much more work is no problem. Since q works well this great. But I like tbm the OkHttp that you suggested. I’m reading his documentation here. Thank you.

  • 1

    @Ssmantasilva I’m not sure I understand the cache question. If on slower phones it disrupts performance, maybe it was the case to try to disable the cache. See here: https://stackoverflow.com/questions/25333351/disable-volley-cache-management

  • 1

    @Samantasilva "low level" = closer to the machine, less abstraction.

  • @Piovezan Ahh I didn’t know I could disable, but anyway I think I’ll use the OkHttp or HttpURLConnection, I think for my use, these will be much lighter for the app. But if in the future I go back to using Volley now I know I can disable the cache, thanks again for the answer

  • @bfavaretto Thanks for the explanation

  • 1

    Use for business projects Retrofit a few years ago, and before that I used to Volley. I never had problems with either of the two, always attended very well.

  • @Lmaker Even on older devices and slow connections you never had problems with them?? I was using Volley, and a simple request to return data in 2-line json was taking longer than receiving photos on Whatsapp, browsing facebook, or listing google photo files. I think the problem of Volley delay was because of the cache, above has a friend who taught me how to disable, but I find the native method much faster now and I’m enjoying it a lot.

  • 1
  • 1

    @Samantasilva never had any problems. I used a Pixi 3 for tests in my old company and I never went slow like this. Sure it was no problem in the backend?

  • 1

    Take a look here: https://answall.com/a/173757/35406

  • @Lmaker Guess it wasn’t the backend, I switched to the Okhttp using the same links and the same request logic, and the speed difference was very large. Much faster than Volley. I did not test with Retrofit, because I read the documentation and saw that the retrofit works with Okhttp, so I did it straight without having to go through this library.

  • @Allefsousa Thanks for the nomination, I already solved with Okhttp, and it was much better than Volley. Adoreiii.

  • @Viana I saw your link, on the comparison between Volley and retrofit I understood, but did not understand what the Ansytask is doing there. Because Ansytask has nothing to do with web requests, does it? As I understand it serves to run a thread in the background isn’t that right?? correct me if I’m wrong. I used Okhttp inside an Ansytask in my application and ameiii the result and speed.

Show 13 more comments

1 answer

1

in build.Radle add

dependencies {
    compile 'com.koushikdutta.ion:ion:2.+'
}
depois chame em sua Activity Ion.with(Context).load(url);

example

public class Login extends AppCompatActivity {

private String TAG = "Login";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_identidade_digital);
    EditText   mUsuarioView  = (EditText) findViewById(R.id.mUsuarioView);
    EditText   mPasswordView  = (EditText) findViewById(R.id.mPasswordView);
    Login(mUsuarioView.getText().toString(),mPasswordView.getText().toString());
}
public void Login(String user, String senha){

    Ion.with(this).load("https://wwww.meuwebservice.com.br")
            .setBodyParameter("user", user)
            .setBodyParameter("senha", senha)
            .asJsonObject().setCallback(new FutureCallback<JsonObject>() {
        @Override
        public void onCompleted(Exception e, JsonObject result) {
            if(e != null){
               // algo deu errado
                Log.d(TAG, e.toString());
            } else if (result.get("retorno").getAsString().equals("YES")) {
               //   tudo ceto trabalhe no seu retorno pegando o resultado
                //seu metodo aqui

            }

        }
    });

}

Asynchronous Android Network and Image Loading

  • Thanks for the reply Maycon, can you tell me what is in this library that you have indicated that can be better than Okhttp?? So far the library "Okhttp" that was indicated here in the reply, this seems to be the best method so far.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.