Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / retrofitsample / GisActivity.java
package com.example.cosmosclient.retrofitsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.cosmosclient.R;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class GisActivity  extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gis);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://cyberjapandata2.gsi.go.jp/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        GisService service = retrofit.create(GisService.class);
//        ObjectMapper mapper = new ObjectMapper();
//        try {
//            ArrayList<Ele> list = mapper.readValue((InputStream) service.listEles(140.08531, 36.103543, "JSON"), new TypeReference<ArrayList<Ele>>() {});
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        Call<Ele> call = service.listEles(140.08531, 36.103543, "JSON");
        call.enqueue(new Callback<Ele>() {
            @Override
            public void onResponse(Call<Ele> call, Response<Ele> response) {
                if (response.isSuccessful()) {
                    Ele result = response.body();
                    System.out.println(result.elevation);
                    System.out.println(result.hsrc);
                }
            }

            @Override
            public void onFailure(Call<Ele> call, Throwable t) {
                t.printStackTrace();
            }

        });
    }

}