GithubHelp home page GithubHelp logo

setreuid / httpjson Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 0.0 78 KB

Java module for parse http json more easy.

License: Apache License 2.0

Java 100.00%
json-parsing http-json java post-json httpjson json request fetch android parse

httpjson's Introduction

httpjson

  • (en) Java library for parse http json more easy.
  • (ko) Url과 파라미터를 넘기고 콜백 함수로 Json 결과를 받는 라이브러리 입니다.

Setup

Gradle

Edit root/app/build.gradle like below.

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    compile 'com.github.setreuid:httpjson:1.0.12'
}
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.setreuid</groupId>
    <artifactId>httpjson</artifactId>
    <version>1.0.12</version>
</dependency>

Usage

Imports

import java.util.HashMap;

import cc.udp.httpjson.HttpJson;
import cc.udp.httpjson.HttpJsonObject;
import cc.udp.httpjson.HttpJsonTask;  
import cc.udp.httpjson.HttpBytesTask;

JSON Parsing without params

파라미터 없이 JSON 파싱

String url = "https://jsonplaceholder.typicode.com/posts/1";

new HttpJson(url, null, new HttpJsonTask() {
    @Override
    public void done(HttpJsonObject json)
    {
        if (json == null)
        {
            return;
        }
        
        // {
        //   "userId": 1,
        //   "id": 1,
        //   "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        //   "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
        // }
        
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String id    = json.getInt("id");
        String title = json.getString("title");
        
        System.out.println("id: " + id);
        System.out.println("title: " + title);
    }
}).get();
lambda
new HttpJson("https://jsonplaceholder.typicode.com/posts/1")
    .then((HttpJsonObject json) -> {
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String id    = json.getInt("id");
        String title = json.getString("title");
        
        System.out.println("id: " + id);
        System.out.println("title: " + title);
    })
    .except((e) -> {
        System.out.println(e.getMessage());
    })
    .get();

Access JSON value with method chaining

메서드 체인을 이용한 JSON 값 가져오기

String url = "https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";

new HttpJson(url, null, new HttpJsonTask() {
    @Override
    public void done(HttpJsonObject json)
    {
        if (json == null)
        {
            return;
        }
        
        // {
        //     "results" : [
        //     {
        //         "address_components" : [
        //         {
        //            "long_name"  : "Google Building 41",
        //            "short_name" : "Google Bldg 41",
        //            "types"      : [ "premise" ]
        //         },
        //         ...
        //     ],
        //     "status" : "OK"
        // }
        
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String status          = json.getString("status");
        String addressLongName = json.getArray("results").get(0)
                                     .getArray("address_components").get(0)
                                     .getString("long_name");
        
        System.out.println("status: " + status);
        System.out.println("addressLongName: " + addressLongName);
    }
}).get();

POST JSON Parsing with params

파라미터 포함하여 POST JSON 파싱

String url = "http://p.udp.cc/jsonPostTest.php";

HashMap<String, String> params = new HashMap<String, String>();
params.put("test-key", "A123456789B");

new HttpJson(url, params, new HttpJsonTask() {
    @Override
    public void done(HttpJsonObject json)
    {
        if (json == null)
        {
            return;
        }
        
        // {
        //  "test-key": "A123456789B"
        // }
        
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String value = json.getString("test-key");
        System.out.println("test-key: " + value);
    }
}).post();
lambda
new HttpJson("http://p.udp.cc/jsonPostTest.php")
    .addField("test-key", "A123456789B")
    .then((HttpJsonObject json) -> {
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String value = json.getString("test-key");
        System.out.println("test-key: " + value);
    })
    .except((e) -> {
        System.out.println(e.getMessage());
    })
    .post();

GET JSON Parsing with params

파라미터 포함하여 GET JSON 파싱

String url = "http://p.udp.cc/jsonGetTest.php";

// http://p.udp.cc/jsonGetTest.php?test-key=A123456789B
HashMap<String, String> params = new HashMap<String, String>();
params.put("test-key", "A123456789B");

new HttpJson(url, params, new HttpJsonTask() {
    @Override
    public void done(HttpJsonObject json)
    {
        if (json == null)
        {
            return;
        }
        
        // {
        //  "test-key": "A123456789B"
        // }
        
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String value = json.getString("test-key");
        System.out.println("test-key: " + value);
    }
}).get();
lambda
new HttpJson("http://p.udp.cc/jsonGetTest.php")
    .addField("test-key", "A123456789B")
    .then((HttpJsonObject json) -> {
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String value = json.getString("test-key");
        System.out.println("test-key: " + value);
    })
    .except((e) -> {
        System.out.println(e.getMessage());
    })
    .get();

Just Call without params

파라미터 포함하지 않고 단순 호출

String url = "http://p.udp.cc/jsonPostTest.php";
new HttpJson(url, null, null).post();

Just Call with params

파라미터 포함하여 단순 호출

String url = "http://p.udp.cc/jsonPostTest.php";

HashMap<String, String> params = new HashMap<String, String>();
params.put("test-key", "A123456789B");

new HttpJson(url, params, null).post();

With headers

헤더 추가하기

  • (en) If Content-Type is specified as application/json, parameters are sent in JSON format.
  • (en) Unless otherwise specified, application/x-www-form-urlencoded is the default value ofContent-Type.
  • (ko) Content-Typeapplication/json으로 지정하면 JSON 형태로 파라미터를 전송합니다.
  • (ko) 특별히 지정하지 않을때에는 application/x-www-form-urlencodedContent-Type의 기본값입니다.
new HttpJson("http://p.udp.cc/jsonPostTest.php")
    .addHeader("Content-Type", "application/json")
    .addField("test-key", "A123456789B")
    .then((HttpJsonObject json) -> {
        // Check json string
        System.out.println("JSON: " + json.toString());
        
        String value = json.getString("test-key");
        System.out.println("test-key: " + value);
    })
    .except((e) -> {
        System.out.println(e.getMessage());
    })
    .post();

Binary

바이너리 값 가져오기

public static String byteArrayToHexString(byte[] bytes){  
    StringBuilder sb = new StringBuilder();  
	for(byte b : bytes){  
        sb.append(String.format("%02X", b&0xff));  
  }  
    return sb.toString();  
}
new HttpJson("https://s.yimg.com/rz/p/yahoo_frontpage_en-US_s_f_p_205x58_frontpage_2x.png")
    .then((byte[] binary) -> {
        System.out.println("binary: " + byteArrayToHexString(binary));
        // binary: 89504E470D0A1A0A0000000D494844520000019A0000007408030...
    })
    .except((e) -> {
        System.out.println(e.getMessage());
    })
    .get();

Class

HttpJsonObject

Return type Method name Parameters
HttpJsonObject getObject String key
ArrayList<HttpJsonObject> getArray String key
String getString String key
int getInt String key
float getFloat String key
double getDouble String key
byte[] getBytes String key
Object get String key

Todo

  • GET
  • POST
  • Return 결과를 byte[]로 받기
  • PUT, PATCH, DELETE
  • byte[]형 필드 추가

httpjson's People

Contributors

setreuid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.