我正在尝试在应用启动时显示包含一些远程数据的对话框(欢迎消息)。由于某种原因,这是行不通的。我的代码就是这个。
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
}
答案 0 :(得分:0)
最好的测试你可以做的事情发生这个代码不起作用是: 尝试写e.printStackTrace();之间的关注:
try {
...
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
知道发生了什么事。
我认为你得到的错误是你无法在ui线程中访问io操作,你必须在其他线程中调用这个方法LIKE:
private void MyMethod() {
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();
final String response = httpclient.execute(httppost, responseHandler);
//String url=response;
runOnUiThread(
new Runnable() {
@Override
public void run() {
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
当想要调用此方法时:
new Thread(new Runnable() {
@Override
public void run() {
MyMethod();
}
}).start();
答案 1 :(得分:0)
句子是什么意思&#34;由于某种原因,这不起作用。&#34;?当我说对话没有出现时,我是否正确?
httpclient.execute()
存在问题。如果互联网连接存在问题,则不会创建对话框,因为已经抛出了初始化。你能在Logcat中看到一些警告/错误吗?NetworkOnMainThreadException
。答案 2 :(得分:0)
我要说不 - 该片段不正确。从API 11开始,所有IO功能都需要在后台线程上进行。后台线程无法触及UI。按照目前的情况,您在创建HttpPost
的方法中执行AlertDialog
。你需要做的是使用AsyncTask
来(1)在doInBackground方法中执行post(2)返回结果(3)使用onPostExecute方法中返回的结果来加载你的对话框(从doInBackground返回值)将自动发送到onPostExecute)。 onPostExecute在UI线程上运行,因此您可以从该方法创建AlertDialog
。