티스토리 뷰
Retrofit : https://square.github.io/retrofit/
OKHttp : http://square.github.io/okhttp/
Manifast.xml
<uses-permission android:name="android.permission.INTERNET" /> |
gradle
// retrofit compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' // Ok http compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1' |
ISeoulOpenData.java ( Interface )
1 2 3 4 5 6 7 8 9 10 | interface ISeoulOpenData { @GET("/{key}/json/{serviceName}/{begin}/{end}/") Call<RemoteData> getData( @Path("key")String key, @Path("serviceName")String serviceName, @Path("begin")int begin, @Path("end")int end); } | cs |
RemoteData.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 | public class RemoteData { SeoulRoadNameInfo SeoulRoadNameInfo; public SeoulRoadNameInfo getSeoulRoadNameInfo() { return SeoulRoadNameInfo; } class SeoulRoadNameInfo{ String list_total_count; Result RESULT; List<Row> row; public String getList_total_count() { return list_total_count; } public Result getRESULT() { return RESULT; } public List<Row> getRow() { return row; } } class Result{ String CODE; String MESSAGE; public String getCODE() { return CODE; } public String getMESSAGE() { return MESSAGE; } } class Row{ String ROAD_NM; String ROAD_TYPE; String ROAD_FUNC; String ROAD_SCALE; String ROAD_WIDTH; String CGG_DIV; public String getROAD_NM() { return ROAD_NM; } public String getROAD_TYPE() { return ROAD_TYPE; } public String getROAD_FUNC() { return ROAD_FUNC; } public String getROAD_SCALE() { return ROAD_SCALE; } public String getROAD_WIDTH() { return ROAD_WIDTH; } public String getCGG_DIV() { return CGG_DIV; } } } | cs |
RestAdapter.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 | public class RestAdapter { public static final int CONNECT_TIMEOUT = 3; public static final int WRITE_TIMEOUT = 5; public static final int READ_TIMEOUT = 3; private static final String SERVER_URL = "http://openapi.seoul.go.kr:8088"; private static OkHttpClient client; private static ISeoulOpenData service; public synchronized static ISeoulOpenData getInstance(){ if(service == null) { // 통신 로그를 확인하기 위한 interceptor 설정 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); // 쿠키 매니저 설정 CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); //OK HTTP 설정 //SSL 통한 서버연결인 경우 인증서가 없으면 통신자체가 안된다. // 이럴 경우 우회하기 위한 인증서를 무시하는 셋팅. client = configureClient(new OkHttpClient().newBuilder()) .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS) .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS) .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) .cookieJar(new JavaNetCookieJar(manager)) //쿠키 저장 .addInterceptor(interceptor) // 로그를 출력(디버깅용) .build(); service = new Retrofit.Builder() .baseUrl(SERVER_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build() .create(ISeoulOpenData.class); } return service; } public static OkHttpClient.Builder configureClient(final OkHttpClient.Builder builder) { X509TrustManager x509TrustManager = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(final X509Certificate[] chain, final String authType) { } @Override public void checkClientTrusted(final X509Certificate[] chain, final String authType) { } }; final TrustManager[] certs = new TrustManager[]{x509TrustManager}; SSLContext ctx = null; try { ctx = SSLContext.getInstance("TLS"); ctx.init(null, certs, new SecureRandom()); } catch (final java.security.GeneralSecurityException ex) { ex.printStackTrace(); } try { final HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(final String hostname, final SSLSession session) { return true; } }; builder.sslSocketFactory(ctx.getSocketFactory(), x509TrustManager).hostnameVerifier(hostnameVerifier); } catch (final Exception e) { e.printStackTrace(); } return builder; } } | cs |
MainActivity.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 | public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); callHttp(); } public void callHttp(){ String key = "********************************************"; String serviceName = "SeoulRoadNameInfo"; int begin = 1; int end = 5; final Call<RemoteData> remoteData = RestAdapter.getInstance().getData(key, serviceName, begin, end); remoteData.enqueue(new Callback<RemoteData>() { @Override public void onResponse(Call<RemoteData> call, Response<RemoteData> response) { if(response.isSuccessful()) { RemoteData data = response.body(); List<RemoteData.Row> rows = data.getSeoulRoadNameInfo().getRow(); for(RemoteData.Row row : rows ){ Log.i("result : ", row.getROAD_NM()); } }else{ Log.e("ERROR : ",response.message()); } } @Override public void onFailure(Call<RemoteData> call, Throwable t) { } }); } } | cs |
결과 확인
|
'ANDROID > android' 카테고리의 다른 글
Android - Motion Sensors (0) | 2016.11.01 |
---|---|
android FireBase 사용하기 (0) | 2016.10.28 |
android Image library(Picasso, Glide) (0) | 2016.10.26 |
android HTTP Cookie 사용. (0) | 2016.10.25 |
android HTTP URL Connection (0) | 2016.10.25 |
Comments