Java 无法接收聊天信息

Java 无法接收聊天信息,java,php,android,Java,Php,Android,我有问题,需要帮助,当我收到聊天信息时,我只成功发送信息并显示我发送的信息,但我未能接收并显示我从另一方收到的信息 我无法从数据库中检索消息,并且总是空的,当消息出现时,代码没有检查是否有任何消息和空的,我认为它停止了 当我检查流时,流无法获取内容 我不明白怎么了,所以任何人都请帮助我。多谢各位 chatroom.php <?php include "config.php"; $idu= $_REQUEST['idu']; $idch= $_REQUEST['idch

我有问题,需要帮助,当我收到聊天信息时,我只成功发送信息并显示我发送的信息,但我未能接收并显示我从另一方收到的信息
我无法从数据库中检索消息,并且总是空的,当消息出现时,代码没有检查是否有任何消息和空的,我认为它停止了
当我检查流时,流无法获取内容
我不明白怎么了,所以任何人都请帮助我。多谢各位

chatroom.php

    <?php
 include "config.php";
   $idu= $_REQUEST['idu'];
   $idch= $_REQUEST['idch'];

    if($idu && $idch){
       $sqlString = " SELECT a.id, a.message, a.system, b.id, b.name, b.course, c.id, d.firstname FROM mdl_chat_messages 
                    as a inner join mdl_chat as b on b.name=a.chatid inner join mdl_course as c on c.id=b.course 
                    inner join mdl_user as d on d.id=a.userid and a.system = 0 and a.userid='".$_GET['idu']."' 
                    and a.chatid='".$_GET['idch']."' and a.id='".$_GET['idcm']."'";}

    $res = mysql_query($sqlString);
        if(mysql_num_rows($res)>0)
        {
            while($data = mysql_fetch_array($res))
            {
                $msg = $data["message"];
                echo "$msg";
            }
        }


?>

再次在SQL语句中进行字符串连接。。。用事先准备好的陈述,该死的。将原始字符串传递到SQL API应该受到法律惩罚。@fge,对不起,您能解释一下您的意思吗?我不明白..在PHP代码中,当您生成SQL语句时,您直接连接来自请求参数的参数。这是典型的SQL注入攻击向量。使用类似PDO的东西(这是我唯一知道的)来构建准备好的语句,这样可以避免此类攻击。哦,就是这样,对不起,我只是个新手,我只知道如何编写这样的代码。。谢谢你的建议……老实说,我真的不能怪你;网络上演示用于生成SQL的字符串连接的教程太多,而演示使用准备好的语句的教程太少:/
package mobile.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.client.ClientProtocolException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.*;

import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import mobile.config.CourseHttpClient;
import mobile.config.Koneksi;

import com.karismaelearning.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
/*import android.util.Log;*/
/*import android.os.Handler;*/
import android.view.*;
import android.widget.*;

public class ChatRoom extends Activity {
    public Koneksi linkurl;
    String SERVER_URL;
    private EditText messageText;
    private TextView meLabel;
    private TextView friendLabel;
    private ViewGroup messagesContainer;
    private ScrollView scrollContainer;
/*    private Handler handler = new Handler();*/

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

        messagesContainer = (ViewGroup) findViewById(R.id.messagesContainer);
        scrollContainer = (ScrollView) findViewById(R.id.scrollContainer);

        Button sendMessageButton = (Button) findViewById(R.id.sendButton);

        Bundle bundle = this.getIntent().getExtras();
        final String paramnama = bundle.getString("nama");
        messageText = (EditText) findViewById(R.id.messageEdit);
        meLabel = (TextView) findViewById(R.id.meLabel);
        friendLabel = (TextView) findViewById(R.id.friendLabel);
        meLabel.setText(paramnama + " (me)");


        final String param1 = bundle.getString("keyCourseId");
        final String param2 = bundle.getString("keyUserId");
        final String param3 = bundle.getString("keyChatsId");
        final String param4 = bundle.getString("keyMessagesId");

        receiveMsg();
        sendMessageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("messages", messageText.getText().toString()));

                String response = null;

                try {
                    linkurl = new Koneksi(ChatRoom.this);
                    SERVER_URL = linkurl.getUrl();
                    SERVER_URL += "/mobile/ChatKirimTeks.php?idu="+param2+"&idch="+param3;
                   response = CourseHttpClient.executeHttpPost(SERVER_URL, postParameters);

                   String res = response.toString();

                   res = res.trim();

                   res = res.replaceAll("\\s+","");
                   if(res.equals("1")){
                       String messageString = messageText.getText().toString();
                       showMessage(messageString, true);
                       messageText.getText().clear();
                   }else
                   {
                       createDialog("Maaf", "Messages Anda Gagal Terkirim");
                   }
                }

                catch (Exception e) {

                    messageText.setText(e.toString());  

                }

             }

        });}


        HttpURLConnection connection;
    URL url = null;
    try{
        linkurl = new Koneksi(this);
        SERVER_URL = linkurl.getUrl();
        SERVER_URL += "/mobile/ChatRoom.php?idu="+param2+"&idch="+param3+"&idcm="+param4;
        url = new URL(SERVER_URL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestMethod("POST");    

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(SERVER_URL);
        //ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
        //add parameter
            //httpPost.setEntity(new UrlEncodedFormEntity(param));

            HttpResponse httpRespose = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpRespose.getEntity();

            //read content
            InputStream in = httpEntity.getContent();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));
            String msg = "";
            while(true)
            {

                try {
                    msg = read.readLine();
                    Log.d("","MSGGG:  "+ msg);

                    //msgList.add(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.getMessage();
                }
                if(msg == null)
                {
                    break;
                }
                else
                {
                    showMessage(msg, false);
                }
            }}
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        }


    public void showMessage(String message, boolean leftSide) {
        final TextView textView = new TextView(ChatRoom.this);
        textView.setTextColor(Color.BLACK);
        textView.setText(message);

        int bgRes = R.drawable.left_message_bg;

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        if (!leftSide) {
            bgRes = R.drawable.right_message_bg;
            params.gravity = Gravity.RIGHT;
        }

        textView.setLayoutParams(params);

        textView.setBackgroundResource(bgRes);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                messagesContainer.addView(textView);

                // Scroll to bottom
                if (scrollContainer.getChildAt(0) != null) {
                    scrollContainer.scrollTo(scrollContainer.getScrollX(), scrollContainer.getChildAt(0).getHeight());
                }
                scrollContainer.fullScroll(View.FOCUS_DOWN);
            }
        });
    }

    private void createDialog(String title, String text) {
        AlertDialog ad = new AlertDialog.Builder(this)
        .setPositiveButton("Ok", null)
        .setTitle(title)
        .setMessage(text)
        .create();
        ad.show();
    }
}
    06-04 17:42:55.932: I/ActivityManager(61): Starting: Intent { cmp=com.karismaelearning/mobile.chat.ChatDetail (has extras) } from pid 410
06-04 17:42:56.762: I/ActivityManager(61): Displayed com.karismaelearning/mobile.chat.ChatDetail: +816ms
06-04 17:42:57.392: I/ActivityManager(61): Starting: Intent { cmp=com.karismaelearning/mobile.chat.ChatRoom (has extras) } from pid 410
06-04 17:42:57.802: D/(410): MSGGG:  null
06-04 17:42:58.334: I/ActivityManager(61): Displayed com.karismaelearning/mobile.chat.ChatRoom: +862ms