用于httpconnection的android asynctask
谁能告诉我如何解决这个问题....为什么它不起作用 我试图异步连接到服务器,这样它就可以在单独的线程上
private class SticketFunctionTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... string) {
try {
//public void SticketFunction(){
//HttpClient
HttpClient SpeedSticket = new DefaultHttpClient();
//Response handler
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://www.mysite.php");
//Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Done: HTTP Post Sending Stickit to Server");
//Log.v("log_tag", "+-+-+-+-+-+-+-+-+- sTICKET: " + z.getString(1) + " " + z.getString(2) + " " + z.getString(3) + " " + z.getString(4));
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("bookName", z.getString(1)+""));
nameValuePairs.add(new BasicNameValuePair("bookAuth", z.getString(2)+""));
nameValuePairs.add(new BasicNameValuePair("ISBN", z.getString(3)+""));
nameValuePairs.add(new BasicNameValuePair("LibNumber", z.getString(4)+""));
//Encode and set entity
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
//Execute
String response = SpeedSticket.execute(postMethod, res).replaceAll("<(.|\n)*?>","");
if (response.equals("Done")){
//Log.v("log_tag", "!!!!!!!!!!!!!!!!!! SticketFunction got a DONE!");
}
else Log.v("log_tag", "!!!!!!!?????????? SticketFunction Bad or no response: " + response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//Log.v("log_tag", "???????????????????? SticketFunction Client Exception");
} catch (IOException e) {
// TODO Auto-generated catch block
//Log.v("log_tag", "???????????????????? IO Exception");
}
}
}
}
can any one please tell me how to fix this problem .... why is it not working
im trying to asynctask the connection to a server so it can be on separate thread
private class SticketFunctionTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... string) {
try {
//public void SticketFunction(){
//HttpClient
HttpClient SpeedSticket = new DefaultHttpClient();
//Response handler
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://www.mysite.php");
//Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Done: HTTP Post Sending Stickit to Server");
//Log.v("log_tag", "+-+-+-+-+-+-+-+-+- sTICKET: " + z.getString(1) + " " + z.getString(2) + " " + z.getString(3) + " " + z.getString(4));
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("bookName", z.getString(1)+""));
nameValuePairs.add(new BasicNameValuePair("bookAuth", z.getString(2)+""));
nameValuePairs.add(new BasicNameValuePair("ISBN", z.getString(3)+""));
nameValuePairs.add(new BasicNameValuePair("LibNumber", z.getString(4)+""));
//Encode and set entity
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
//Execute
String response = SpeedSticket.execute(postMethod, res).replaceAll("<(.|\n)*?>","");
if (response.equals("Done")){
//Log.v("log_tag", "!!!!!!!!!!!!!!!!!! SticketFunction got a DONE!");
}
else Log.v("log_tag", "!!!!!!!?????????? SticketFunction Bad or no response: " + response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//Log.v("log_tag", "???????????????????? SticketFunction Client Exception");
} catch (IOException e) {
// TODO Auto-generated catch block
//Log.v("log_tag", "???????????????????? IO Exception");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 Eclipse,请选择所有代码并转到 Source ->正确的缩进(或按 Ctrl-I)。然后你会看到有两个try块,但是两个catch块属于第二个try。因此,第一次尝试时需要 catch 或 finally 块。
我认为最简单的解决方法是在第二次尝试时删除该行。
If you're using Eclipse, select all of the code and go to Source -> Correct Indentation (or press Ctrl-I). Then you will see that you have a two try blocks, but the two catch blocks belong to the second try. So you need a catch or finally block for the first try.
I think the easiest fix is to delete the line with the second try.