0

我为消息传递应用程序设置了一个非常简单的设置。我只是从 EditText 框中获取文本并将其作为参数传递给将其添加到我的数据库的 php 页面。它非常适用于一个单词条目。我在 EditText 框中输入空格的那一刻它不起作用。我对 Android 还是很陌生。我真的不明白这怎么可能发生。有谁知道这怎么会发生?

这是我的 onClick 方法:

    public void sendMessage(View v) {
Log.d("tag", "XXXXXXXXXXXXXXXXXXXXXX");
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());

        username = prefs.getString("username", "null");



where = prefs.getString("chat", "null");
        message = (EditText) findViewById(R.id.inptbox);

        function = new Functions();
        Editable messagetext;
messagetext = message.getText();


                response = function.sendMessage(username, where, messagetext.toString());
String theresponse = "";
theresponse = response;
                if (theresponse.compareTo("0") == 0) {
                    Toast.makeText(getApplicationContext(), "Success!",
                            Toast.LENGTH_SHORT).show();
                    //message.setText(null);

                } else if (response.compareTo("9") == 0) {

                    // userent.setText("nine");

                }

    }

我的函数.sendMessage:

    public String sendMessage(String username, String where, String string){

        BufferedReader in = null;
        String data = null;
        try{


            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://abc.com/user_send.php?username="+username+"&where="+where+"&message="+string);

            HttpPost post_request = new HttpPost();
            post_request.setURI(website);


            HttpGet request = new HttpGet();

            request.setURI(website);
            //executing actual request

                        //add your implementation here
            HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) != null) {
                sb.append(l+nl);

            }
            in.close();
            data = sb.toString();


return data;
        }catch (Exception e){
            return "ERROR";

        }

        }

我应该如何解决这个问题?

4

1 回答 1

2

您必须对您的消息进行编码以使其“URL 安全”。空格(和其他特殊字符)不能出现在 URL 中;%20这就是为什么如果您在地址栏中键入空格,您的浏览器会将空格替换为。在 function.sendMessage() 中尝试以下操作:

URI website = new URI("http://abc.com/user_send.php?username="+username+"&where="+where+"&message="+URLEncoder.encode(string, "UTF-8"));

注意最后使用URLEncoder

于 2012-04-24T16:40:05.117 回答