android - MYSQL PHP Android聊天无效

时间:2015-11-05 16:03:08

标签: android listview

我正在为我的网站开发一个Android聊天应用程序。在我的网站上,我可以实时聊天,但在我的Android应用程序中,当我收到新消息时,我不知道如何更新Listview。此外,当应用程序打开并收到新消息时,我希望在设备上收到通知。

至少我有一个带有聊天概述的应用程序。我也可以聊天,阅读旧消息并发送新消息。但在发送新邮件后,列表视图尚未更新。

以下是我发送/接收消息的一些步骤:

发送消息

void send_message(final String message) {
    Thread t = new Thread() {
        private String result = "";
        public void run() {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://mywebsite.com/app/send_message.php?email="+ MainActivity.user_email+"&with="+ chat_with+"&message="+message);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }
            catch(Exception e){
                showAlert("ERROR", "No connection to Internet.");
            }
        }
    };
    t.start();
    getData(chat_with);
    listview = (ListView) findViewById(R.id.listView_chat);
    while(!this.dataComplete) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    this.updateData();

}
  1. 从editText
  2. 获取新邮件内容
  3. 将数据发布到我的网络服务器上的PHP文件
  4. PHP脚本插入消息并将新消息的聊天状态从朋友更改为1
  5. 更新Listview(尚未使用)
  6. 收到消息

    public void get_messages(final String with_user) {
        Thread t = new Thread() {
            private String result = "";
            public void run() {
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://mywebsite.com/app/get_messages.php?email="+ MainActivity.user_email+"&with="+ with_user);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                }
                catch(Exception e){
                    showAlert("ERROR", "No connection to Internet.");
                }
                try{
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "n");
                    }
                    is.close();
                    result=sb.toString();
                }
                catch(Exception e){
                    showAlert("ERROR","Can't parse data.");
                }
                try{
                    JSONArray jArray = new JSONArray(result);
                    for(int i=0;i<jArray.length();i++){
                        json_data = jArray.getJSONObject(i);
                        results_messages.add((String) json_data.get("message"));
                    }
                    dataComplete = true;
                }
                catch(JSONException e){
                    showAlert("ERROR","No connection to Internet.");
                }
            }};
        t.start();
    }
    
    1. 登录用户信息和朋友信息

    2. 将数据发布到我的网络服务器上的PHP文件

    3. 以json响应的方式获取消息

    4. 将json转换为listview

    5. 如果收到新消息(尚未正常工作),则自动刷新Listview

    6. 为了填写Listview,我使用:

      public void fillList() {
          listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results_messages));
      }
      

      我希望得到一些有用的答案。 提示:message_adapter.notifyDataSetChanged();我测试但不能正常工作

0 个答案:

没有答案