我想测试我的 Android 应用程序是否可以在没有 JSON 的情况下成功地从 php 页面获取响应。php 只包含echo "hello";
,但我没有 Toast 它。我能找到的最接近的解决方案是,我试图将其中的一些部分放入我的代码中:如何查看从 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 中没有显示该行,我做错了哪一部分?注意:处理程序已在类中声明。