使用Parse发送和接收消息时遇到问题。该应用程序运行但发送的消息没有到达其他用户,并且消息未保存在Parse中。此外,用户名不会写在活动聊天的顶部。
这是我的聊天课:
package com.example.veto_uwk.chatapp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.provider.SyncStateContract;
import android.text.InputType;
import android.text.format.DateUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.example.veto_uwk.chatapp.CustomActivity;
import com.example.veto_uwk.chatapp.UserList;
import com.parse.FindCallback;
import com.parse.ParseAnalytics;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.example.veto_uwk.chatapp.R;
import com.example.veto_uwk.chatapp.CustomActivity;
import com.example.veto_uwk.chatapp.Conversation;
public class Chat extends CustomActivity
{
public static ParseUser Username;
/** The Conversation list. */
private ArrayList<Conversation> convList;
/** The chat adapter. */
private ChatAdapter adp;
/** The Editext to compose the message. */
private EditText txt;
/** The user name of buddy. */
private String buddy;
/** The date of last message in conversation. */
private Date lastMsgDate;
/** Flag to hold if the activity is running or not. */
private boolean isRunning;
/** The handler. */
private static Handler handler;
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
ParseAnalytics.trackAppOpened(getIntent());
convList = new ArrayList<Conversation>();
ListView list = (ListView) findViewById(R.id.list);
adp = new ChatAdapter();
list.setAdapter(adp);
list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
list.setStackFromBottom(true);
txt = (EditText) findViewById(R.id.txt);
txt.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_FLAG_MULTI_LINE);
setTouchNClick(R.id.buttonSend);
buddy = getIntent().getStringExtra("name");
/* getActionBar().setTitle(buddy);*/
handler = new Handler();
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onResume()
*/
@Override
protected void onResume()
{
super.onResume();
isRunning = true;
loadConversationList();
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onPause()
*/
@Override
protected void onPause()
{
super.onPause();
isRunning = false;
}
/* (non-Javadoc)
* @see com.socialshare.custom.CustomFragment#onClick(android.view.View)
*/
@Override
public void onClick(View v)
{
super.onClick(v);
if (v.getId() == R.id.buttonSend)
{
sendMessage();
}
}
/**
* Call this method to Send message to opponent. It does nothing if the text
* is empty otherwise it creates a Parse object for Chat message and send it
* to Parse server.
*/
private void sendMessage()
{
if (txt.length() == 0)
return;
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txt.getWindowToken(), 0);
String s = txt.getText().toString();
final Conversation c = new Conversation(s, new Date(),
UserList.user.getUsername());
c.setStatus(Conversation.STATUS_SENDING);
convList.add(c);
adp.notifyDataSetChanged();
txt.setText(null);
ParseObject po = new ParseObject("Chat");
po.put("sender", UserList.user.getUsername());
po.put("receiver", buddy);
po.put("createdAt", "");
po.put("message", s);
po.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e)
{
if (e == null)
c.setStatus(Conversation.STATUS_SENT);
else
c.setStatus(Conversation.STATUS_FAILED);
adp.notifyDataSetChanged();
}
});
}
/**
* Load the conversation list from Parse server and save the date of last
* message that will be used to load only recent new messages
*/
private void loadConversationList()
{
ParseQuery<ParseObject> q = ParseQuery.getQuery("Chat");
if (convList.size() == 0)
{
// load all messages...
ArrayList<String> al = new ArrayList<String>();
al.add(buddy);
al.add(UserList.user.getUsername());
q.whereContainedIn("sender", al);
q.whereContainedIn("receiver", al);
}
else
{
// load only newly received message..
if (lastMsgDate != null)
q.whereGreaterThan("createdAt", lastMsgDate);
q.whereEqualTo("sender", buddy);
q.whereEqualTo("receiver", UserList.user.getUsername());
}
q.orderByDescending("createdAt");
q.setLimit(30);
q.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> li, ParseException e)
{
if (li != null && li.size() > 0)
{
for (int i = li.size() - 1; i >= 0; i--)
{
ParseObject po = li.get(i);
Conversation c = new Conversation(po
.getString("message"), po.getCreatedAt(), po
.getString("sender"));
convList.add(c);
if (lastMsgDate == null
|| lastMsgDate.before(c.getDate()))
lastMsgDate = c.getDate();
adp.notifyDataSetChanged();
}
}
handler.postDelayed(new Runnable() {
public void run()
{
if (isRunning)
loadConversationList();
}
}, 1000);
}
});
}
/**
* The Class ChatAdapter is the adapter class for Chat ListView. This
* adapter shows the Sent or Receieved Chat message in each list item.
*/
private class ChatAdapter extends BaseAdapter
{
/* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
public int getCount()
{
return convList.size();
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
public Conversation getItem(int arg0)
{
return convList.get(arg0);
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId(int arg0)
{
return arg0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
public View getView(int pos, View v, ViewGroup arg2)
{
Conversation c = getItem(pos);
if (c.isSent())
v = getLayoutInflater().inflate(R.layout.chat_item_sent, null);
else
v = getLayoutInflater().inflate(R.layout.chat_item_rcv, null);
TextView lbl = (TextView) v.findViewById(R.id.lbl2);
lbl.setText(DateUtils.getRelativeDateTimeString(Chat.this, c
.getDate().getTime(), DateUtils.SECOND_IN_MILLIS,
DateUtils.DAY_IN_MILLIS, 0));
lbl = (TextView) v.findViewById(R.id.lbl1);
lbl.setText(c.getMsg());
if (c.isSent())
{
if (c.getStatus() == Conversation.STATUS_SENT)
lbl.setText("Delivered");
else if (c.getStatus() == Conversation.STATUS_SENDING)
lbl.setText("Sending...");
else
lbl.setText("Failed");
}
else
lbl.setText("");
return v;
}
}
/* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
这是我的对话课:
package com.example.veto_uwk.chatapp;
import java.util.Date;
public class Conversation {
public static final int STATUS_SENDING =0;
public static final int STATUS_SENT =1;
public static final int STATUS_FAILED = 2;
private String msg;
private int status = STATUS_SENT;
private Date date;
private String sender;
public Conversation(String msg ,Date date ,String sender)
{
this.msg=msg;
this.date=date;
this.sender =sender;
}
public Conversation()
{
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
public boolean isSent()
{
return UserList.user.getUsername().equals(sender);
}
public Date getDate()
{
return date;
}
public void setDate(Date date){
this.date =date;
}
public String getSender()
{
return sender;
}
public void setSender(String sender)
{
this.sender=sender;
}
public int getStatus()
{
return status;
}
public void setStatus(int status)
{
this.status = status;
}
}
答案 0 :(得分:0)
最好使用sinch库以及解析发送&amp; amp;收到消息。这篇文章给了我很多帮助: https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse/ 在文章中,他们有一个测试应用程序的链接,您可以下载和播放。玩得开心:))