用于httpconnection的android asynctask

发布于 2024-11-28 06:33:58 字数 1971 浏览 0 评论 0原文

谁能告诉我如何解决这个问题....为什么它不起作用 我试图异步连接到服务器,这样它就可以在单独的线程上

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-12-05 06:33:58

如果您使用的是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文