Skip to content

Repository files navigation

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

importjava.util.HashMap;
importcc.udp.httpjson.HttpJson;
importcc.udp.httpjson.HttpJsonObject;
importcc.udp.httpjson.HttpJsonTask; importcc.udp.httpjson.HttpBytesTask;

JSON Parsing without params

파라미터 없이 JSON 파싱

Stringurl = "https://jsonplaceholder.typicode.com/posts/1";
newHttpJson(url, null, newHttpJsonTask() {
@Overridepublicvoiddone(HttpJsonObjectjson)
{
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 stringSystem.out.println("JSON: " + json.toString());
Stringid = json.getInt("id");
Stringtitle = json.getString("title");
System.out.println("id: " + id);
System.out.println("title: " + title);
}
}).get();
lambda
newHttpJson("https://jsonplaceholder.typicode.com/posts/1")
.then((HttpJsonObjectjson) -> {
// Check json stringSystem.out.println("JSON: " + json.toString());
Stringid = json.getInt("id");
Stringtitle = 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 값 가져오기

Stringurl = "https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";
newHttpJson(url, null, newHttpJsonTask() {
@Overridepublicvoiddone(HttpJsonObjectjson)
{
if (json == null)
{
return;
}
// {// "results" : [// {// "address_components" : [// {// "long_name" : "Google Building 41",// "short_name" : "Google Bldg 41",// "types" : [ "premise" ]// },// ...// ],// "status" : "OK"// }// Check json stringSystem.out.println("JSON: " + json.toString());
Stringstatus = json.getString("status");
StringaddressLongName = 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 파싱

Stringurl = "http://p.udp.cc/jsonPostTest.php";
HashMap<String, String> params = newHashMap<String, String>();
params.put("test-key", "A123456789B");
newHttpJson(url, params, newHttpJsonTask() {
@Overridepublicvoiddone(HttpJsonObjectjson)
{
if (json == null)
{
return;
}
// {// "test-key": "A123456789B"// }// Check json stringSystem.out.println("JSON: " + json.toString());
Stringvalue = json.getString("test-key");
System.out.println("test-key: " + value);
}
}).post();
lambda
newHttpJson("http://p.udp.cc/jsonPostTest.php")
.addField("test-key", "A123456789B")
.then((HttpJsonObjectjson) -> {
// Check json stringSystem.out.println("JSON: " + json.toString());
Stringvalue = 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 파싱

Stringurl = "http://p.udp.cc/jsonGetTest.php";
// http://p.udp.cc/jsonGetTest.php?test-key=A123456789BHashMap<String, String> params = newHashMap<String, String>();
params.put("test-key", "A123456789B");
newHttpJson(url, params, newHttpJsonTask() {
@Overridepublicvoiddone(HttpJsonObjectjson)
{
if (json == null)
{
return;
}
// {// "test-key": "A123456789B"// }// Check json stringSystem.out.println("JSON: " + json.toString());
Stringvalue = json.getString("test-key");
System.out.println("test-key: " + value);
}
}).get();
lambda
newHttpJson("http://p.udp.cc/jsonGetTest.php")
.addField("test-key", "A123456789B")
.then((HttpJsonObjectjson) -> {
// Check json stringSystem.out.println("JSON: " + json.toString());
Stringvalue = json.getString("test-key");
System.out.println("test-key: " + value);
})
.except((e) -> {
System.out.println(e.getMessage());
})
.get();

Just Call without params

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

Stringurl = "http://p.udp.cc/jsonPostTest.php";
newHttpJson(url, null, null).post();

Just Call with params

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

Stringurl = "http://p.udp.cc/jsonPostTest.php";
HashMap<String, String> params = newHashMap<String, String>();
params.put("test-key", "A123456789B");
newHttpJson(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의 기본값입니다.
newHttpJson("http://p.udp.cc/jsonPostTest.php")
.addHeader("Content-Type", "application/json")
.addField("test-key", "A123456789B")
.then((HttpJsonObjectjson) -> {
// Check json stringSystem.out.println("JSON: " + json.toString());
Stringvalue = json.getString("test-key");
System.out.println("test-key: " + value);
})
.except((e) -> {
System.out.println(e.getMessage());
})
.post();

Binary

바이너리 값 가져오기

publicstaticStringbyteArrayToHexString(byte[] bytes){ StringBuildersb = newStringBuilder(); for(byteb : bytes){ sb.append(String.format("%02X", b&0xff)); } returnsb.toString(); }
newHttpJson("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 typeMethod nameParameters
HttpJsonObjectgetObjectString key
ArrayList<HttpJsonObject>getArrayString key
StringgetStringString key
intgetIntString key
floatgetFloatString key
doublegetDoubleString key
byte[]getBytesString key
ObjectgetString key

Todo

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

About

Java module for parse http json more easy.

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages