[英]Android HTTP POST to get response without JSON, and put in Toast
我想測試我的Android應用程序是否可以成功從php頁面獲取響應,而不使用JSON。 php只包含echo "hello";
,但我沒有敬酒它。 我能找到的最近的解決方案是,我試圖將其中的一部分放入我的代碼中: 如何查看從httppost發送的字符串?
我的代碼如下(更新):
public void onClick(View v) {
Thread t = new Thread() {
@Override
public void run() {
Log.i("Threaded", "Inside thread!!!");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.10/fyp/test.php");
try {
HttpResponse response = httpclient.execute(httppost);
InputStream content = response.getEntity().getContent();
Log.i("Connect", content.toString());
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = buffer.readLine()) != null) {
sb.append(line + "\n");
}
Log.i("Connect", sb.toString());
handler.post(new Runnable() {
public void run() {
showToastMessage("hello");
}
});
} catch (Exception e) {
Log.e("Connect", "Fail");
e.printStackTrace();
}
}
};
}
線程似乎沒有運行,行Log.i("Threaded", "Inside thread!!!");
在LogCat中沒有顯示,我做錯了哪一部分? 注意:處理程序已在類中聲明。
確保在UI線程上執行toast,否則它將失敗。 使用Handler或將您的網絡操作包裝在AsyncTask中,您將結果傳遞給由UI線程處理的onPostExecute方法。
http://developer.android.com/reference/android/os/AsyncTask.html
如果這不是您的問題,請添加日志記錄以查看您是否從服務器獲得響應並檢查LogCat中的“hello”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.