We are currently implementing an application that can be translated into a toy project
If you enter Korean to translate and translate into English,
Save to database and
I'm making an application that brings the translated data into a recycling view.
All the work is done on the Fragment
Communication is using the Retrofit2 library
The database is using the Romm library
The problem is that the data received from Retrofit2 is not being inserted
The error currently appears this way.
java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:353) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) Caused by: android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: language_table.language (code 1299) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:789) at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:926) at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86) at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:79) at android.arch.persistence.room.EntityInsertionAdapter.insertAndReturnId(EntityInsertionAdapter.java:114) at com.example.toyproject.Model.LanguageDao_Impl.insert(LanguageDao_Impl.java:96) at com.example.toyproject.Model.LanguageRepository$1.doInBackground(LanguageRepository.java:35) at com.example.toyproject.Model.LanguageRepository$1.doInBackground(LanguageRepository.java:23) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764)
I use AsyncTask when I insert in Room, but I think something went wrong in doInbackground I don't know why.
It's the retrofit2 part!
public Translate(TotalPresent.GetData present) {
this.present = present;
languageData = new LanguageData();
}
@Override
public void translatedata(String change, String text){
retrofit = new Retrofit.Builder()
.baseUrl(PapagoService.URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
PapagoService papagoService = retrofit.create(PapagoService.class);
HashMap<String,Object> param = new HashMap<>();
param.put("source","ko");
param.put("target",change);
param.put("text",text);
papagoService.getResult(param).enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
if(response.isSuccessful()){
Result result = response.body();
Message message = result.getMessage();
DetailData detailData = message.getDetailData();
present.getData(detailData.getTranslatedText()); // Communicate to show the result of response to UI
languageData.postData(text,detailData.getTranslatedText()); // Communicate and hand over data to save as DB data
}
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
}
});
}
}
present.getData(detailData.getTranslatedText()); // Communicate to show the result of response to UI languageData.postData(text,detailData.getTranslatedText(); //TranslatedText()); //For communication and storage as DB data, hand over the data
public void setModel(RecyclerView recyclerView, RecyclerViewAdpater recyclerViewAdpater){
model = ViewModelProviders.of(recyclerView).get(Model.class);
model.getAllLanguage().observe(recyclerView, languages -> recyclerViewAdpater.setData(languages));
}
public void postData(String text,String text2){
This.text = text; // This part
This.text2 = text2; //This part
}
public void setLanguage() {
try {
Language language = new Language();
language.setLanguage(text); // Gets the taken over data
language.setLanguage2(text2);// Gets the passed data
model.insert(language);
}catch (Exception e){
e.printStackTrace();
}
}
The data transferred from Retrofit2 to DB was written to be transferred to the above source code
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater,R.layout.recycler_view,container,false);
languageData = new LanguageData();
recyclerViewAdpater = new RecyclerViewAdpater(getContext());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
binding.recyclerview.setLayoutManager(linearLayoutManager);
binding.recyclerview.setAdapter(recyclerViewAdpater);
languageData.setModel(this, recyclerViewAdpater);
languageData.setLanguage(); // Invoke the method you received the data from here
//There is an error in this part.
return binding.getRoot();
}
The above source code is for implementing RecyclerView.
The source code below is an error database insert statement
public void insert(Language language) {
new AsyncTask<Language, Void, Long>(){
@Override
protected void onPostExecute(Long aLong) {
super.onPostExecute(aLong);
Log.d(TAG, "insert : " + aLong);
}
@Override
protected Long doInBackground(Language... languages) {
if(languageDao == null)
return -1L;
return languageDao.insert(languages[0]);
}
}.execute(language);
}
The code below is the part of the Dao source code
@Dao
public interface LanguageDao {
@Insert
long insert(Language language);
@Update
int update(Language language);
@Query("DELETE FROM language_table")
int deleteAll();
@Query("DELETE FROM language_table WHERE id = :id")
int deleteLanguage(int id);
@Query("SELECT * from language_table ORDER BY language ASC")
LiveData<List<Language>> getAllLanguage();
}
The code below is the language source code
@Entity(tableName = "language_table")
public class Language {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id;
@NonNull
@ColumnInfo(name = "language")
private String language;
@NonNull
@ColumnInfo(name = "language2")
private String language2;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setLanguage(@NonNull String language) {
this.language = language;
}
public String getLanguage() {
return language;
}
@NonNull
public String getLanguage2() {
return language2;
}
public void setLanguage2(@NonNull String language2) {
this.language2 = language2;
}
}
I'm still touching this and that at this time, but it's not working out.
room android
Release all non-null constraints in language_table, implement them, and see how the data goes in
© 2024 OneMinuteCode. All rights reserved.