我有一個應用程序,有4個activities.in第一個活動我想從我的網站(PHP文件)請求一些文本。對於第一次,我按下按鈕,得到它工作正常數據,但在我按下按鈕,今後一段時期,它返回一個正確的文本,但與以前的text.see串接它的例子:URLConnection從php服務器收到錯誤的文本
First press: returns: "abc"
Second press: returns: "abcabc" (must be return abc)
Third press: returns: "abcabcabc" (must be return abc)
.
- 我清晰的文本每次查看。
- 我每次關閉連接。
- 我每次關閉
BufferedReader
。 - 我關閉。
活動1:
try{
URL u = new URL(uu);
HttpURLConnection con = (HttpURLConnection) u.openConnection(Proxy.NO_PROXY);
con.setDoOutput(true);
con.setUseCaches(false);
PrintStream pr = new PrintStream(con.getOutputStream());
pr.print("code=3&arg1=" + str);
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String l;
while ((l=br.readLine())!=null){
z = z + "\n" + l;
}
br.close();
pr.close();
con.disconnect();
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tx = (TextView) findViewById(R.id.textView2);
tx.setMovementMethod(new ScrollingMovementMethod());
tx.setText("");
tx.setText(z);
}
});
}
catch (final Exception ex){
}
在while循環之前添加'z =「」;;' –
您可能需要在獲取輸入之前刷新'PrintStream'。爲此,我會使用write()而不是print()。 – EJP