반응형

RetrofitClient.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example;
 
import com.google.gson.GsonBuilder;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
 
 
public class RetrofitClient {
    private static RetrofitClient instance = null;
    private static RetrofitInterface retrofitInterface;
    private static String baseUrl = "https://kox947ka1a.execute-api.ap-northeast-2.amazonaws.com/prod/users/";
 
    private RetrofitClient(){
 
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
                .build();
 
        retrofitInterface = retrofit.create(RetrofitInterface.class);
    }
 
    public static RetrofitClient getInstance(){
        if(instance == null){
            instance = new RetrofitClient();
        }
 
        return instance;
    }
 
    public static com.example.RetrofitInterface getRetrofitInterface() {
        return retrofitInterface;
    }
}
cs

RetrofitInterface.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example;
 
import com.google.gson.GsonBuilder;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
 
 
public class RetrofitClient {
    private static RetrofitClient instance = null;
    private static RetrofitInterface retrofitInterface;
    private static String baseUrl = "https://kox947ka1a.execute-api.ap-northeast-2.amazonaws.com/prod/users/";
 
    private RetrofitClient(){
 
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
                .build();
 
        retrofitInterface = retrofit.create(RetrofitInterface.class);
    }
 
    public static RetrofitClient getInstance(){
        if(instance == null){
            instance = new RetrofitClient();
        }
 
        return instance;
    }
 
    public static com.example.RetrofitInterface getRetrofitInterface() {
        return retrofitInterface;
    }
}
cs

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.exmaple;
 
import com.example.Data.Commands;
import com.example.Data.LocationData;
import com.example.Data.TrucksData;
import com.exmaple.Data.*;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
 
import java.util.*;
 
public class Main {
    private static String x_auth_token = "";
    private static String auth_key = "";
    private static com.example.RetrofitClient retrofitClient;
    private static com.example.RetrofitInterface retrofitInterface;
    public static void main(String[] args){
        System.out.println("Test");
 
        //StartAPI();
        //LocationAPI();
        //TruckAPI();
        //SimulateAPI();
        ScoreAPI();
    }
 
    public static void StartAPI() {
        try {
            retrofitClient = com.example.RetrofitClient.getInstance();
            retrofitInterface = com.example.RetrofitClient.getRetrofitInterface();
            retrofitInterface.StartAPI(x_auth_token,1).enqueue(new Callback<StartData>() {
                @Override
                public void onResponse(Call<StartData> call, Response<StartData> response) {
                    StartData result = response.body();
                    auth_key = result.getAuth_key();
                    System.out.println(result.getAuth_key());
                    System.out.println(result.getProblem());
                    System.out.println(result.getTime());
                    LocationAPI();
                    TruckAPI();
 
                }
 
                @Override
                public void onFailure(Call<StartData> call, Throwable t) {
                    System.out.println(t.getMessage());
                }
            });
 
        }catch(Exception e) {
            System.out.println(e.getMessage());
            System.out.println("!!");
        }
    }
 
    public static void LocationAPI(){
        try{
            retrofitClient = com.example.RetrofitClient.getInstance();
            retrofitInterface = com.example.RetrofitClient.getRetrofitInterface();
            retrofitInterface.LocationAPI(auth_key).enqueue(new Callback<LocationData>() {
                @Override
                public void onResponse(Call<LocationData> call, Response<LocationData> response) {
                    LocationData result = response.body();
                    //System.out.println("LOCATION" + response.code());
 
                    for(int i = 0; i < result.getLocations().size(); i++){
                        System.out.println("Locations : " + result.getLocations().get(i).getId() + " " + result.getLocations().get(i).getLocatedBikesCount());
                    }
 
                }
 
                @Override
                public void onFailure(Call<LocationData> call, Throwable t) {
                    System.out.println(t.getMessage());
                }
            });
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
 
    public static void TruckAPI(){
        try{
            retrofitClient = com.example.RetrofitClient.getInstance();
            retrofitInterface = com.example.RetrofitClient.getRetrofitInterface();
            retrofitInterface.TruckAPI(auth_key).enqueue(new Callback<TrucksData>() {
                @Override
                public void onResponse(Call<TrucksData> call, Response<TrucksData> response) {
                    TrucksData result = response.body();
                    //System.out.println("TRUCK" + response.code());
                    for(int i = 0; i < result.getTrucks().size(); i++){
                        System.out.println("Trucks : " + result.getTrucks().get(i).getId() + " " + result.getTrucks().get(i).getLoadedBikesCount() + " " + result.getTrucks().get(i).getLocationId());
                    }
                }
 
                @Override
                public void onFailure(Call<TrucksData> call, Throwable t) {
                    System.out.println(t.getMessage());
                }
            });
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
 
    public static void SimulateAPI(){
        try{
            retrofitClient = com.example.RetrofitClient.getInstance();
            retrofitInterface = com.example.RetrofitClient.getRetrofitInterface();
            com.example.Data.Commands commands = new Commands();
            List<Commands.Command> lists = new ArrayList<>();
 
 
 
            for(int i = 0; i < 5; i++){
                Commands.Command command = new Commands.Command();
                command.setCommand(new ArrayList<>());
                command.setTruckId(i);
                lists.add(command);
            }
            commands.setCommands(lists);
 
            retrofitInterface.SimulateAPI(auth_key,commands).enqueue(new Callback<SimulateData>() {
                @Override
                public void onResponse(Call<SimulateData> call, Response<SimulateData> response) {
                    SimulateData result = response.body();
                    //System.out.println(result.getTime());
                    System.out.println("Simulate : " + result.getStatus() + " " + result.getTime() + " " + result.getFailed_requests_count() + " " + result.getDistance());
                    if(result.getStatus().charAt(0== 'r')SimulateAPI();
                }
 
                @Override
                public void onFailure(Call<SimulateData> call, Throwable t) {
                    System.out.println(t.getMessage());
                }
            });
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
 
    public static void ScoreAPI(){
        try{
            retrofitClient = com.example.RetrofitClient.getInstance();
            retrofitInterface = com.example.RetrofitClient.getRetrofitInterface();
            retrofitInterface.ScoreAPI(auth_key).enqueue(new Callback<ScoreData>() {
                @Override
                public void onResponse(Call<ScoreData> call, Response<ScoreData> response) {
                    ScoreData result = response.body();
                    //System.out.println("SCORE : " + response.code());
                    System.out.println("Score : " + result.getScore());
                }
 
                @Override
                public void onFailure(Call<ScoreData> call, Throwable t) {
                    System.out.println(t.getMessage());
                }
            });
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
 
cs

Readme.md

1
2
3
4
5
6
## Retrofit
version 2.9.0
## Okhttp
version 4.9.0
## Gson-converter
version 2.9.0
cs

build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
ext {
    retrofitVersion = '2.9.0'
    okhttpVersion = '4.9.0'
}
 
dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
 
    compile "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
    compile "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
    compile "com.squareup.okhttp3:okhttp:$rootProject.okhttpVersion"
}
cs

JSON -> JAVA
https://www.jsonschema2pojo.org/

POSTMAN
https://www.postman.com/

반응형

+ Recent posts