Android httpconnection异步任务实现中的问题
我正在将一个表单发布到google服务器,并以html字符串的形式获得响应,最后我将该字符串放在webview上以显示结果……我使用异步任务来显示这个进度对话框,但有时它会向我显示“强制关闭”的消息我对代码没有任何更改…这意味着对输出的预测是意外的。。。我的代码是这样的Android httpconnection异步任务实现中的问题,android,http,android-asynctask,Android,Http,Android Asynctask,我正在将一个表单发布到google服务器,并以html字符串的形式获得响应,最后我将该字符串放在webview上以显示结果……我使用异步任务来显示这个进度对话框,但有时它会向我显示“强制关闭”的消息我对代码没有任何更改…这意味着对输出的预测是意外的。。。我的代码是这样的 public class Urlasync extends Activity { WebView engine=null; @Override public void onCreate(Bundle s
public class Urlasync extends Activity {
WebView engine=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AddStringTask().execute();
}
class AddStringTask extends AsyncTask<Void, Void, HttpResponse>
{
HttpResponse end = null;
String endResult = null;
public static final int TIMEOUT_MS=10000;
HttpClient client=null;
HttpPost post =null;
List<NameValuePair> pairs=null;
BasicResponseHandler myHandler=null;
private final ProgressDialog dialog = new ProgressDialog(Urlasync.this);
@Override
protected void onPreExecute() {
client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT_MS);
post = new HttpPost("http://www.google.com/m");
pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("hl", "en"));
pairs.add(new BasicNameValuePair("gl", "us"));
pairs.add(new BasicNameValuePair("source", "android-launcher-widget"));
pairs.add(new BasicNameValuePair("q", "persistent"));
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
SystemClock.sleep(400);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.dialog.setMessage("starts...");
this.dialog.show();
}
@Override
protected HttpResponse doInBackground(Void... arg0) {
try {
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
return response;
}
end = response;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) { //this exception is called
e.printStackTrace();
}
return end;
}
@Override
protected void onPostExecute(HttpResponse params) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if(params!=null)
{
String endResult=null;
BasicResponseHandler myHandler = new BasicResponseHandler();
try {
endResult = myHandler.handleResponse(params);
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
engine = (WebView)findViewById(R.id.webview);
engine.loadDataWithBaseURL("http://", endResult, "text/html", "UTF-8", null);
setContentView(R.layout.main);
engine.requestFocus(View.FOCUS_DOWN);
}
Toast.makeText(Urlasync.this, "Done!", Toast.LENGTH_SHORT).show();
}
}
}
公共类URLSync扩展活动{
WebView引擎=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
新建AddStringTask().execute();
}
类AddStringTask扩展了AsyncTask
{
HttpResponse end=null;
字符串endResult=null;
公共静态最终整数超时\u MS=10000;
HttpClient=null;
HttpPost=null;
列表对=空;
BasicResponseHandler myHandler=null;
private final ProgressDialog=新建ProgressDialog(Urlasync.this);
@凌驾
受保护的void onPreExecute(){
client=新的DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),超时);
HttpConnectionParams.setSoTimeout(client.getParams(),TIMEOUT_MS);
post=新的HttpPost(“http://www.google.com/m");
pairs=新的ArrayList();
添加(新的BasicNameValuePair(“hl”、“en”);
添加(新的BasicNameValuePair(“gl”、“us”);
添加(新的BasicNameValuePair(“源”、“android启动程序小部件”);
添加(新的BasicNameValuePair(“q”,“持久”));
试一试{
setEntity(新的UrlEncodedFormEntity(对));
系统时钟。睡眠(400);
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
this.dialog.setMessage(“启动…”);
this.dialog.show();
}
@凌驾
受保护的HttpResponse doInBackground(无效…arg0){
试一试{
HttpResponse response=client.execute(post);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC\u OK)
{
返回响应;
}
结束=响应;
}捕获(客户端协议例外e){
e、 printStackTrace();
}catch(IOException e){//调用此异常
e、 printStackTrace();
}
返回端;
}
@凌驾
受保护的void onPostExecute(HttpResponse参数){
if(this.dialog.isShowing()){
this.dialog.disclose();
}
如果(参数!=null)
{
字符串endResult=null;
BasicResponseHandler myHandler=新BasicResponseHandler();
试一试{
endResult=myHandler.HandlerResponse(参数);
}捕获(HttpResponseException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
引擎=(WebView)findViewById(R.id.WebView);
loadDataWithBaseURL(“http://”,endResult,“text/html”,“UTF-8”,null);
setContentView(R.layout.main);
engine.requestFocus(视图.焦点向下);
}
Toast.makeText(Urlasync.this,“Done!”,Toast.LENGTH_SHORT.show();
}
}
}
欢迎有代码的建议
谢谢你。你不能在
doInBackground
中使用UI(比如显示Toast
)。在onPostExecute
或onProgressUpdate
中执行此操作。在doInBackground
中不能对UI执行任何操作(如显示Toast
)。在onPostExecute
或onProgressUpdate
中执行此操作。发生了什么异常?请添加日志cat的输出。它告诉我“强制关闭”。它告诉我应用程序意外停止。阅读有关日志cat的内容,在这里可以找到导致问题的原因异常为NullPointerException。位于org.apache.http.impl.client.BasicResponceHandler.HandlerResponceWhat exception?请添加日志cat的输出。它说我要“强制关闭”。它说我应用程序意外停止..阅读关于LogCat的内容,在这里你可以找到导致问题的原因异常是NullPointerException..在org.apache.http.impl.client.BasicResponceHandler.handleresponcein doInbackground我得到了Httpresponce,然后在onPostExecute中我解析它,显示在webview中响应的toast和显示。。。但我读到一些异步任务需要UI线程的地方,所以我把它放在那里..从doInBackground
中删除显示toast或将其移动到onPostExecute
。例外是NullPointerException..在org.apache.http.impl.client.BasicResponceHandler.handleresponceYou也在doInBackground
中执行,但是您不能:受保护的HttpResponse doInBackground(Void…arg0){…Toast.makeText(Urlasync.this,“Done execute!”,Toast.LENGTH_SHORT.show()e> doInBackground
或将其移动到PostExecute上。异常为NullPointerException..org.apache.http.impl.client.BasicResponceHandler.HandlerResponce。您也可以在doInBackground
中执行,但不能:受保护的HttpResponse doIn