Giter VIP home page Giter VIP logo

httpagent's Introduction

HttpAgent

super simple library to manage http requests.

Gradle

dependencies {
    implementation 'com.studioidan.httpagent:httpagent:1.0.16@aar'
}

Now see how easy it becomes with HttpAgent!

Get Request

HttpAgent.get("www.example.com/api/books")
                    .goJson(new JsonCallback() {
                        @Override
                        protected void onDone(boolean success, JSONObject jsonObject) {
                            
                        }
                    });

or you can go jsonArray like so

HttpAgent.get("www.example.com/api/books")
                    .goJsonArray(new JsonArrayCallback() {
                        @Override
                        protected void onDone(boolean success, JSONArray jsonArray) {
                            
                        }
                    });

Need to add url parameters? no problem!

HttpAgent.get("www.example.com/api/books")
                    .queryParams("key_1","value_1","key_2","value_2","key_N","value_N")
                    .goJsonArray(new JsonArrayCallback() {
                        @Override
                        protected void onDone(boolean success, JSONArray jsonArray) {
                            
                        }
                    });

Post? Put? Delete? of course...

HttpAgent.post("www.example.com/api/books");
HttpAgent.put("www.example.com/api/books");
HttpAgent.delete("www.example.com/api/books")

Adding body is also simple...

HttpAgent.post("www.example.com/api/books")
                    .queryParams("key_1","value_1","key_2","value_2","key_N","value_N")
                    .withBody("{name:popapp ,age:27}")
                    .goJsonArray(new JsonArrayCallback() {
                        @Override
                        protected void onDone(boolean success, JSONArray jsonArray) {

                        }
                    });

Or even more simple...

HttpAgent.post("www.example.com/api/books")
                    .queryParams("key_1","value_1","key_2","value_2","key_N","value_N")
                    .withBody("key_1","value_1","key_2","value_2","key_N","value_N") // will be converted to json
                    .goJsonArray(new JsonArrayCallback() {
                        @Override
                        protected void onDone(boolean success, JSONArray jsonArray) {

                        }
                    });

Don't forget the headers...

HttpAgent.get("http://192.168.88.253/Video/inputs/channels/1")
                        .headers("Authorization", "Basic YWRtaW46P3V5YFZhNzAw", "Content-Type", "application/json")
                        .goString(new StringCallback() {
                            @Override
                            protected void onDone(boolean success, String stringResults) {
                                Log.d(TAG, stringResults);
                            }
                        });

Any request can be made with one of the following callbacks:

Get a string results

goString(new StringCallback() {
                        @Override
                        protected void onDone(boolean success, String results) {
                            
                        }
                    })

Get Json results

goJson(new JsonCallback() {
                        @Override
                        protected void onDone(boolean success, JSONObject jsonObject) {
                            
                        }
                    })

Get JsonArray results

goJsonArray(new JsonArrayCallback() {
                        @Override
                        protected void onDone(boolean success, JSONArray jsonArray) {

                        }
                    });

Get no results, Just send the request

go(new SuccessCallback() {
                        @Override
                        protected void onDone(boolean success) {
                            
                        }
                    })

You also have access to those on any callback

HttpAgent.post("www.example.com/api/books")
                    .queryParams("key_1","value_1","key_2","value_2","key_N","value_N")
                    .withBody("{name:popapp ,age:27}")
                    .goJson(new JsonCallback() {
                        @Override
                        protected void onDone(boolean success, JSONObject jsonObject) {
                            getErrorMessage(); //returns error message if exists.
                            getResponseCode(); // well, it's obvious...
                            getStringResults(); // returns results as as string.
                        }
                    });

httpagent's People

Contributors

studioidan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

httpagent's Issues

Source code and return statements

Hi,

I like this implementation - it is very quick to implement. I have trouble finding source code on this github repo - it looks like that you have deleted it. Please make it public, so we can contribute to this project.

We would like to add the possibility to return status booleans on onDone :)

File Upload

HI

How can I upload file using this? Can you create demo for this?

Regards,

Source Code and Post method

Hi,

Thanks for your contribution.

Firstly, we don't have any access to your code or you have deleted it ? I can't see any source code when I fork your project.

Secondly, I think there is a bug in your project (that's why I wanted to fork it). In case of POST Method, if you had values in "body" (.withBody("{name:popapp ,age:27}")), this is not working ! The request doesn't have the body params.

TY

Posting Body (without converting request body in to json)

Hi

    HttpAgent.post("http://192.168.1.101:8002/dummy_file/generate_oauthSignature")
    .withBody("key_1","value_1","key_2","value_2","key_N","value_N") // will be converted to json
   .headers("Content-Type", "application/x-www-form-urlencoded")

I do not want to convert body in to json as server side tech. requires key_1 = value_1 param as a post.

error message ..

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/os/AsyncTaskCompat;
at com.studioidan.httpagent.HttpAgent.executeSelf(HttpAgent.java:173)
at com.studioidan.httpagent.HttpAgent.goJson(HttpAgent.java:158)
at com.example.snapgroup6.myapplication.MainActivity.onCreate(MainActivity.java:22)

Force wifi

Hey! I'm trying to create an app to login to a wifi hotspot but I have a problem, when I want to submit the form the mobile data is used because the wifi connection is not "working" until you submit the form. With your library can I choose to use wifi connection to post the form instead of mobile data?

Thanks!

Two onDone occur in one HttpAgent.post

When I use one HttpAgent.post then two onDone occur. I use JsonCallback. First onDone return empty jsonResponse and the second onDone return filled jsonResponse from remote server API.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.