我为消息传递应用程序设置了一个非常简单的设置。我只是从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");
}
}
我的function.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";
}
}
我该如何解决这个问题呢?
答案 0 :(得分:2)
您必须对邮件进行编码,使其“网址安全”。空格(和其他特殊字符)不能出现在网址中;这就是为什么如果您在地址栏中键入空格,您的浏览器将用%20
替换空格。在function.sendMessage()中尝试以下内容:
URI website = new URI("http://abc.com/user_send.php?username="+username+"&where="+where+"&message="+URLEncoder.encode(string, "UTF-8"));
请注意最后使用URLEncoder。