0
我在一個問題的中間,我得到一個字符串「INSERTED SUCCESSFULLY」作爲來自我的httprequest的響應,我的目標是匹配它與本地字符串(與上面相同)但是當我嘗試匹配它們時,它們不匹配,因此不會進入我的狀態。stringbuffer toString與相同的靜態字符串不匹配
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
}
這與編碼有關。 012B: N.B: - 我的PHP代碼只是回聲「插入成功」。
我嘗試沒有成功
if(stringBuffer.toString().equals("INSERTED SUCCESSFULLY")
問題是'LineSeparator'因爲要添加新在第一個StringBuffer行,但不是在第二個,所以總是得到'FALSE'。或者在第二個字符串中添加行分隔符或者在兩個字符串上調用'trim()'來比較 –