我試圖顯示一個對話框,當應用程序啓動時有一些遠程數據(歡迎消息)。由於某種原因,這是行不通的。我的代碼是這樣的。Android啓動對話框遠程數據
p.s.我有另一個啓動時加載的進度類型對話框。我試圖在它之後添加它。
任何人都可以告訴這段代碼是否正確?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.website.com/welcome.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("id", url));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
//String url=response;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
builder.setMessage(response);
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
} catch (ClientProtocolException e) {
//Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
//Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}
感謝您的代碼,得到了錯誤。對於一些愚蠢的錯誤,生產代碼有welcome.txt而不是welcome.php,並且你的代碼有助於獲得與http不允許相關的錯誤(就像我在做POST請求和.txt格式)。 – wmsgeek