在领域数据库中插入数据时得到错误响应

时间:2019-06-12 08:54:58

标签: android mvvm realm-database

当我在领域数据库中插入数据时,它会响应输入字符串的Realm.Transaction.OnError

在此应用程序中,我使用Realm以脱机模式存储数据,但它在Realm.Transaction.OnError()中响应“对于输入字符串” 邮件列表

public class messagelist extends AppCompatActivity {

private RecyclerView recyclerView;
private ActivityMessagelistBinding activityMessagelistBinding;
private Postmessagemodel postmessagemodel;
private RecyclerView.Adapter adapter;
private LinearLayoutManager linearLayoutManager;
private DividerItemDecoration dividerItemDecoration;
private messageadapter customAdapter;
private messagelist_datamanager dataManger;
private postmessage_datamanager postmessage_datamanager;
private List<messagemodel> newsList;

String tokens;
String sendmsg;
Realm realm;
String db_msg,db_auto;
int db_touserid;

 int toid;

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

    toid=getIntent().getExtras().getInt("clickid");
    Log.e("toid", String.valueOf(toid));

    SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

    tokens = pref.getString("sherdtoken", "");

    realm = Realm.getDefaultInstance();


    activityMessagelistBinding = DataBindingUtil.setContentView(this,R.layout.activity_messagelist);



    recyclerView = (RecyclerView) findViewById(R.id.recycle1);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    newsList = new ArrayList<>();

    customAdapter = new messageadapter(this, newsList);
    recyclerView.setAdapter(customAdapter);



    postmessagemodel = new Postmessagemodel();
    postmessage_datamanager = new postmessage_datamanager(this);

    activityMessagelistBinding.setPostmsg(postmessagemodel);




    dataManger = new messagelist_datamanager(this);


    activityMessagelistBinding.setPostbtn(new Post() {
        @Override
        public void onclick() {
            sendmsg = postmessagemodel.getMSG();
            Log.e("token", tokens+"message"+postmessagemodel.getMSG());

            if (isNetworkAvailable())
            {
                postmessage();
                Log.e("connected", String.valueOf(true));

                RealmResults<offlinemsg> allofflinemsg = realm.where(offlinemsg.class).findAll();

                if(allofflinemsg != null)
                {
                    for(offlinemsg f : allofflinemsg)
                    {
                        db_msg=f.getOfflinemsg_msg();
                        db_auto=f.getOfflinemsg_auth();
                        db_touserid=f.getOfflinemsg_touserid();

                    }
                }
            }

            else
            {
                writedb(sendmsg.toString(),tokens.toString(),toid);
            }


        }
    });



    getmessage();




}

private void writedb(final String sendmsg, final String tokens, final int toid)
{
    Log.e("msg",sendmsg+"autho"+tokens+"touserid"+toid);
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            offlinemsg off_msg = bgRealm.createObject(offlinemsg.class,UUID.randomUUID().toString());
            off_msg.setOfflinemsg_msg(sendmsg);
            off_msg.setOfflinemsg_auth(tokens);
            off_msg.setOfflinemsg_touserid(toid);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            Log.v("Database","data enterd");
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            Log.e("error",error.getMessage());
        }
    });
}

private boolean isNetworkAvailable() //check app is online or not
{
    ConnectivityManager manager =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    boolean isAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()) {
        // Network is present and connected
        isAvailable = true;
    }
    return isAvailable;
}



public void getmessage()
{
    dataManger.sendVolleyRequest2(tokens,toid,messagelist.this, new messagelist_datavalue() {

        @Override
        public void setJsonDataResponse(JSONArray response) {


            try {


                messagemodel userModel = new messagemodel();

                for (int i = 0; i < response.length(); i++) {

                    JSONObject jsonObject = response.getJSONObject(i);
                    // Log.e("final", String.valueOf(i));
                    userModel.setFromUserId(jsonObject.getInt("fromUserId"));
                    userModel.setMessage(jsonObject.getString("message"));
                    userModel.setToUserId(jsonObject.getInt("toUserId"));
                    Log.e("message",userModel.getMessage());
                    newsList.add(userModel);
                }


            } catch (JSONException jsonDataResponse) {
                Log.e("error", String.valueOf(jsonDataResponse));

            }

            customAdapter.notifyDataSetChanged();


        }

        @Override
        public void setVolleyError(VolleyError volleyError) {
            Log.e("Volley", volleyError.toString());
        }
    });


}


private void postmessage() {

    postmessage_datamanager.sendVolleyRequest3(sendmsg,toid,tokens,messagelist.this,new postmessage_datavalue() {
        @Override
        public void setJsonDataResponse(JSONObject response) {
        }

        @Override
        public void setVolleyError(VolleyError volleyError) {

        }
    });


}

@Override
protected void onDestroy() {
    super.onDestroy();
    realm.close();
}

}

MyApplication(realm)

public class MyApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    Realm.init(this);
    RealmConfiguration configuration = new RealmConfiguration.Builder().name("realmdata.realm").build();
    Realm.setDefaultConfiguration(configuration);

}
}

offlinemsg(模型)

public class offlinemsg extends RealmObject {

@PrimaryKey
private int offlinemsg_id;

private String Offlinemsg_msg;
private String Offlinemsg_auth;
private int Offlinemsg_touserid;

public int getOfflinemsg_id() {
    return offlinemsg_id;
}

public void setOfflinemsg_id(int offlinemsg_id) {
    this.offlinemsg_id = offlinemsg_id;
}

public String getOfflinemsg_msg() {
    return Offlinemsg_msg;
}

public void setOfflinemsg_msg(String offlinemsg_msg) {
    Offlinemsg_msg = offlinemsg_msg;
}

public String getOfflinemsg_auth() {
    return Offlinemsg_auth;
}

public void setOfflinemsg_auth(String offlinemsg_auth) {
    Offlinemsg_auth = offlinemsg_auth;
}

public int getOfflinemsg_touserid() {
    return Offlinemsg_touserid;
}

public void setOfflinemsg_touserid(int offlinemsg_touserid) {
    Offlinemsg_touserid = offlinemsg_touserid;
}
}

当我离线时在应用程序中插入数据时,它总是响应“针对输入字符串”的事务错误

logcat

06-12 14:11:33.650 27543-27543/com.example.mayurpancholi.chat_mvvm E/MSG: k
06-12 14:11:34.884 27543-27543/com.example.mayurpancholi.chat_mvvm E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-12 14:11:34.884 27543-27543/com.example.mayurpancholi.chat_mvvm E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-12 14:11:35.248 27543-27543/com.example.mayurpancholi.chat_mvvm E/MSG: o
06-12 14:11:35.350 27543-27543/com.example.mayurpancholi.chat_mvvm E/MSG: ok
06-12 14:11:38.346 27543-27543/com.example.mayurpancholi.chat_mvvm E/token: 586ab021-b51a-46e2-95c9-1f54e5030267messageok
06-12 14:11:38.346 27543-27543/com.example.mayurpancholi.chat_mvvm E/msg: okautho586ab021-b51a-46e2-95c9-1f54e5030267touserid12258
06-12 14:11:38.354 27543-27543/com.example.mayurpancholi.chat_mvvm E/error: For input string: "2aa59e2d-6e82-4bf2-bcda-4758940d9eb5"

0 个答案:

没有答案