【问题标题】:stringbuffer toString does not match with same static stringstringbuffer toString 与相同的静态字符串不匹配
【发布时间】:2013-11-28 09:08:27
【问题描述】:

我遇到了一个问题,我收到一个字符串“插入成功”作为我的 httprequest 的响应,我的目标是将它与本地字符串匹配(与上面相同)但是当我尝试匹配它们时不匹配因此不进入我的条件。

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
request.setEntity(entity);             
HttpResponse response= httpClient.execute(request);
bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String LineSeparator = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator); 
}
bufferedReader.close();
String q = "INSERTED SUCCESSFULLY";
StringBuffer got = new StringBuffer(q);
Log.d("Response", stringBuffer.toString());//MY logcat shows "INSERTED SUCCESSFULLY"
if(stringBuffer.equals(got))
   {
               // Does not enter here
       Log.d("xyz", "Getting Response" + stringBuffer.toString());
      //some TASK
   }

这与编码有关。 注意:- 我的 php 代码只是回显“插入成功”。

我也尝试了以下代码,但没有成功

if(stringBuffer.toString().equals("INSERTED SUCCESSFULLY")

【问题讨论】:

  • 问题是 LineSeparator,因为您在第一个 StringBuffer 中添加新行,但不是在第二个中添加新行,所以总是得到 FALSE。在第二个字符串中添加行分隔符或在两个字符串上调用 trim() 进行比较

标签: php android http-post httpresponse stringbuffer


【解决方案1】:
if(stringBuffer.toString().trim().equals("INSERTED SUCCESSFULLY")  

试试这个可能有一些开始或结束的空间

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多