我为消息传递应用程序设置了一个非常简单的设置。我只是从 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";
}
}
我应该如何解决这个问题?