한 걸음 두 걸음
Retrofit expected begin_object but was string at line 1 column 1 path $ 에러 해결 본문
Retrofit expected begin_object but was string at line 1 column 1 path $ 에러 해결
언제나 변함없이 2021. 2. 23. 05:27expected begin_object but was string at line 1 column 1 path $
expected a string but was begin_object at line 1 column 3 path $
내가 작성한 data structure로 데이터가 expected begin_object but was string at line 1 column 1 path $ 뜨면서 안들어오니까 String으로 바꿔서 작업해봤지만 expected a string but was begin_object at line 1 column 3 path $에러코드가 뜨며 진전이 없었음.
원인은 List<>였음. List
예제코드 복붙
동작방식 : 가운데에 있는 Hello World! 텍스트를 누르면 레트로핏2로 통신해서 데이터가져온거 표시.
코드.
- 이전작업 gradle, internet permission, manifests application에 추가해주기(
<application
android:usesCleartextTraffic="true">
</application>
일정 버젼 이상으로는 앱 통신시 http:// 주소로는 통신이 불가능하고 htttps://로만 통신이 가능하게 제약이 걸려있기 때문에 만약 사용하려는 api 주소가 http://면 해당 문구를 넣어줌으로서 접속이 가능하게 해주는 설정
1. MainActivity
package com.onsemiro.hanpinetree.chickeniot;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.gson.JsonArray;
import java.io.IOException;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
TextView retrofitTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrofitTextView = findViewById(R.id.retrofit_textview);
//Retrofit 호출
Call<List<ChickenDTO>> call = RetrofitClient.getApiService().postOverlapCheck();
call.enqueue(new Callback<List<ChickenDTO>>() {
@Override
public void onResponse(Call<List<ChickenDTO>> call, Response<List<ChickenDTO>> response) {
if(!response.isSuccessful()){
retrofitTextView.setText("연결이 비정상적 : "+ "error code : " + response.code());
return;
}
retrofitTextView.setText("연결이 성공적 : "+ response.body().toString());
}
@Override
public void onFailure(Call<List<ChickenDTO>> call, Throwable t) {
retrofitTextView.setText("연결실패"+ t.getMessage());
}
});
}
}
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<TextView
android:id="@+id/retrofit_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
3. RetrofitClient
package com.onsemiro.hanpinetree.chickeniot;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static final String BASE_URL = "http://내IP:8080/";
public static RetrofitAPI getApiService(){return getInstance().create(RetrofitAPI.class);}
private static Retrofit getInstance(){
Gson gson = new GsonBuilder().setLenient().create();
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
}
4.RetrofitAPI
package com.onsemiro.hanpinetree.chickeniot;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
public interface RetrofitAPI {
@GET("aws/chicken/select")
Call<List<ChickenDTO>> postOverlapCheck(); //이건 바디 요청시 사용하는거
}
5. ChickenDTO
package com.onsemiro.hanpinetree.chickeniot;
public class ChickenDTO {
int no;
int illumination;
String time;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public int getIllumination() {
return illumination;
}
public void setIllumination(int illumination) {
this.illumination = illumination;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Override
public String toString() {
return no+" "+illumination+" "+time;
}
}
ㅇ참고하면 좋은 레퍼런스
http://devflow.github.io/retrofit-kr/
https://morm.tistory.com/296
'FrontEnd > Android' 카테고리의 다른 글
MPandroidchart 간단 예제 / 코드 첨부 (4) | 2021.03.25 |
---|---|
Android MPAndroid Chart Circle 삭제하기 (0) | 2021.03.24 |
no usb devices or running emulators detected (0) | 2021.02.21 |
개발 트랜드 (0) | 2020.01.01 |
안드로이드 Android ] overridePendingTransition 으로 애니메이션 효과가 있는 화면이동 구현하기 (0) | 2019.12.30 |