AS Jackson編⑤解答~ObjectMapper(Jackson)を使う~APIレスポンス(一つの場合)をJSON形式で受け取り、それをログに通常表示する~

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ApiClient apiClient = new ApiClient();
try {
apiClient.doInSchedule("https://official-joke-api.appspot.com/jokes/random");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center_horizontal">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PUSH"
android:layout_marginTop="80sp"/>

</LinearLayout>

ApiClient.java

import android.util.Log;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ApiClient {
void doInSchedule(String url) throws IOException{
Request request = new Request.Builder().url(url).build();
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String msg = e.getMessage();
Log.v("MY_LOG","メッセージ:"+msg);

}

@Override
public void onResponse(Call call, Response response) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String jsonText = response.body().string();
JsonNode node = mapper.readTree(jsonText);
String setupText = node.get("setup").asText();
Log.v("MY_LOG","問いかけ:" + setupText);

}
});
}
}

build.gradle(app)に下記追加

dependencies {
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.14.0'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.0'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.14.0'

implementation 'com.squareup.okhttp3:okhttp:3.4.2'

 

AndroidManifest.xmlに下記の記載を追加を忘れずに。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>