我成功地可以使用AsyncTask连接到我的servlet服务器,但现在我决定我想有一个进度对话框,让用户知道正在进行网络操作。 (使用2G,有时3G我可以注意到取得结果的延迟)。 当AsyncTask在尝试执行此操作时崩溃时,我的问题就出现了:
private class GetQinfo extends AsyncTask<String, Void,String> {
ProgressDialog progDailog ;
protected void onPreExecute() {
super.onPreExecute();
progDailog= new ProgressDialog(GetTicket.this);
progDailog.setMessage("pls wait");
progDailog.setIndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDailog.setCancelable(true);
progDailog.show();
}
protected String doInBackground(String... arg0) {...}
protected void onPostExecute(String branchQ){
...
progDailog.dismiss();
}
}
但这给了我一个例外
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
所以我决定停止使用AsyncTask,因为我真的不需要它(我想)。由于我不允许用户在UI上做任何事情,使用AsyncTask是否有意义? 我选择了线程解决方案:
new Thread(new Runnable() {
public void run() {
String line=null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("command", "GetQ"));
nameValuePairs.add(new BasicNameValuePair("city", city));
nameValuePairs.add(new BasicNameValuePair("type", type));
nameValuePairs.add(new BasicNameValuePair("org", org));
nameValuePairs.add(new BasicNameValuePair("branch", spinner.getSelectedItem().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
line = r.readLine();
nameValuePairs.clear();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
line="-1";
}
String[] info=line.split(";");
if(info[1].equals("-1")){
Toast.makeText(getApplicationContext(), "Some msg", Toast.LENGTH_LONG).show();
}
else{
....
//Update the UI
}
}
}).start();
但由于某种原因,这引发了一个java.net.SocketException:Socket在此行上关闭 HttpResponse response = httpclient.execute(httppost); 有任何想法吗?感谢
编辑: @Ahmad Dwaik'术士' 我从主线程调用它,我有一个微调器和一个方法
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(arg2==0){
//no selection
}
else{
new GetQinfo().execute(city,type,org,spinner.getSelectedItem().toString());
}
通过执行此操作,我得到java.lang.RuntimeException:无法在未调用Looper.prepare()错误的线程内创建处理程序。日志是
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.os.Handler.<init>(Handler.java:200)
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.os.Handler.<init>(Handler.java:114)
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.app.Dialog.<init>(Dialog.java:109)
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.app.AlertDialog.<init>(AlertDialog.java:114)
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.app.AlertDialog.<init>(AlertDialog.java:98)
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
12-07 10:16:19.285: E/AndroidRuntime(1929): at com.example.quecustomer.GetTicket$GetQinfo.onPreExecute(GetTicket.java:872)
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
12-07 10:16:19.285: E/AndroidRuntime(1929): at android.os.AsyncTask.execute(AsyncTask.java:535)
12-07 10:16:19.285: E/AndroidRuntime(1929): at com.example.quecustomer.GetTicket$1.run(GetTicket.java:781)
12-07 10:16:19.285: E/AndroidRuntime(1929): at java.lang.Thread.run(Thread.java:841)
GetTicket.java:872指的是这一行:
progDailog= new ProgressDialog(GetTicket.this);
我的doInBackground: @
Override
protected String doInBackground(String... arg0) {
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("command", "GetQ"));
nameValuePairs.add(new BasicNameValuePair("city", arg0[0]));
nameValuePairs.add(new BasicNameValuePair("type", arg0[1]));
nameValuePairs.add(new BasicNameValuePair("org", arg0[2]));
nameValuePairs.add(new BasicNameValuePair("branch", arg0[3]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String line = r.readLine();
nameValuePairs.clear();
return line;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
编辑2 事实证明我发现了一种方法,但不确定它是否正确。它有效,所以必须是:)
我使用onProgressUpdate(Void ... none)来创建ProgressDialog并在onPostExecute()上将其关闭。 你是怎么做到的?
答案 0 :(得分:0)
关于此错误,您没有提供太多详细信息
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
您似乎无法在Main&#34; UI&#34;中调用您的AsyncTask new GetQinfo().execute(args);
。线程。
再次考虑使用AsyncTask,实现它并直接从主线程中调用它。如果那没有帮助显示你的完整AsyncTask子类和完整的logcat
答案 1 :(得分:0)
您的堆栈跟踪显示您从另一个AsyncTask
呼叫Thread
:
12-07 10:16:19.285: E/AndroidRuntime(1929): at com.example.quecustomer.GetTicket$1.run(GetTicket.java:781)
12-07 10:16:19.285: E/AndroidRuntime(1929): at java.lang.Thread.run(Thread.java:841)
查看GetTicket
的第781行。在那里你可能会调用new GetQinfo.execute()
来带你去:
12-07 10:16:19.285: E/AndroidRuntime(1929): at com.example.quecustomer.GetTicket$GetQinfo.onPreExecute(GetTicket.java:872)