[Java] Retrofit2
build.gradle (Module: app)
dependencies {
// Retrofit2
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.3.1'
}
AndroidManifast.xml
<uses-permission android:name="android.permission.INTERNET"/>
인터넷 권한 추가.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jwsoft.javaproject">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
RetrofitAPI.java
package com.jwsoft.javaproject;
import com.google.gson.JsonObject;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface RetrofitAPI {
@GET("/users/{user}/repos")
Call<List<JsonObject>> getUserRepos(@Path("user") String userName);
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(@NotNull String s) {
Log.e("", s);
}
});
interceptor.level(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
retrofitAPI.getUserRepos("jwsoft91").enqueue(new Callback<List<JsonObject>>() {
@Override
public void onFailure(Call<List<JsonObject>> call, Throwable t) {
Log.e("응답 실패 : ", "응답 실패");
}
@Override
public void onResponse(Call<List<JsonObject>> call, Response<List<JsonObject>> response) {
List<JsonObject> jsonArray = response.body();
JsonObject jsonObject = jsonArray.get(0);
Log.e("응답 성공 : ", jsonObject.get("name").toString());
}
});
}
}
build.gradle (Module: app)
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
위에 코드 추가하면 에러 해결됨.
stackoverflow.com/questions/59448845/no-static-method-metafactory
No static method metafactory
I have an issue with my app that when I log in, the app crashes and I get the error: java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/Str...
stackoverflow.com
실행 후
위 에러 발생 시 앱 삭제 후 다시 실행하면 됨.
java.net.SocketException: socket failed: EPERM (Operation not permitted)
I am working on an Android Studio project with several activities. I am currently trying to read the output from a Java Servlet on localhost but it seems to be crashing due to a socket permission. ...
stackoverflow.com
최종 실행 결과