读取从 PHP 回显的字符串到 java 中?
我正在尝试使用以下代码来检索通过 PHP 回显的服务器的响应。执行此代码时,字符串比较方法compareTo() 和(...).equals(..) 无法正常工作。我尝试了各种选项,并且确信尽管似乎将响应转换为字符串格式,但“responseText”并不具有典型的字符串属性。如果我有 3 个字符串文字语句,其中之一是从 findUser.php 中回显的。我怎样才能将它读入java以允许我确定字符串的内容,就像我在这里尝试做的那样?我发现很多关于需要创建 BufferedReader 对象的讨论,但我不明白如何实现它。如果有人愿意为我列出步骤,我将非常感激。
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(".../findUser.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String responseText = EntityUtils.toString(response.getEntity());
if(responseText.compareTo("Not Registered") == 0 || responseText.compareTo("Error") == 0) {
Log.i("KYLE","TRUE");
// DISPLAY ERROR MESSAGE
TextView loginError = (TextView)findViewById(R.id.loginErrorMsg);
loginError.setVisibility(View.VISIBLE);
loginError.setText(responseText);
}
else {
GlobalVars.username = userEmail;
Login.this.finish();
Intent intent = new Intent(Login.this,Purchase.class);
startActivity(intent);
}
catch(Exception e) {Log.e("log_tag", "Error in http connection"+e.toString());}
}
I am trying to use the following code to retrieve a response from a server echoed through PHP. The string compare methods compareTo() and (...).equals(..) do not act properly when this code is executed. I have tried all sorts of options and I'm convinced that in spite of appearing to convert the response to string format, "responseText" does not have the typical string properties. If I have 3 string literal statements, one of which is echoed from findUser.php. How can I read it into java in a way that will allow me to determine the contents of the string as i am trying to do here? I have found a lot of discussion about needing to create BufferedReader object, but I do not understand how to implement that. If someone would please lay out the steps for me, I would be very appreciative.
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(".../findUser.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String responseText = EntityUtils.toString(response.getEntity());
if(responseText.compareTo("Not Registered") == 0 || responseText.compareTo("Error") == 0) {
Log.i("KYLE","TRUE");
// DISPLAY ERROR MESSAGE
TextView loginError = (TextView)findViewById(R.id.loginErrorMsg);
loginError.setVisibility(View.VISIBLE);
loginError.setText(responseText);
}
else {
GlobalVars.username = userEmail;
Login.this.finish();
Intent intent = new Intent(Login.this,Purchase.class);
startActivity(intent);
}
catch(Exception e) {Log.e("log_tag", "Error in http connection"+e.toString());}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
responseText
日志消息看起来正确,但比较方法返回 false,则您可能存在字符集问题。在许多情况下,看似相同的字符出现在不同字符集的不同代码点上。EntityUtils.toString() 指出,当未指定字符集时,它会尝试分析实体或只是回退到 ISO-8859-1。
UTF-8 通常是安全的默认值。尝试将其添加到 PHP 脚本的顶部:
EntityUtils 应该会选择它,如果没有,您可以将“utf-8”传递给 toString() 方法以强制它使用相同的字符集。
If your
responseText
log message looks correct but the comparison methods are returning false, then you likely have a character set issue. There are many cases of what appears to be the same character appearing at different code points of different character sets.The documentation for EntityUtils.toString() states that when no character set is specified, it tries to analyse the entity or just falls back to ISO-8859-1.
UTF-8 is generally a safe default to use. Try adding this to the top of your PHP script:
EntityUtils should pick that up, if not you can pass "utf-8" to the toString() method to force it to use the same character set.
以下是从响应中读取数据的更多方法。
(方法1)使用缓冲读取器一次读取一行数据
(方法2)一次读取一个字节,然后转换为字符串
Here are a couple more ways to read data from a response.
(Method 1) Read data one line at a time using a buffered reader
(Method 2) Read data one byte at a time, then convert to string