如何使用异步任务用于操纵httppost机器人 [英] How to use async task for manipulating the httppost android

查看:148
本文介绍了如何使用异步任务用于操纵httppost机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的异步任务。我需要在我的应用程序中使用httppost。请帮我处理下code。使用异步任务。请给我结构性code

 的HttpClient HttpClient的=新DefaultHttpClient();
                HttpPost httpPost =新HttpPost(URL这里);
                httpPost.addHeader(内容类型,应用程序/ XML);
                尝试{
                    HTT presponse响应= httpClient.execute(httpPost);
                    读者的BufferedReader =新的BufferedReader(
                            新的InputStreamReader(response.getEntity()
                                    .getContent(),UTF-8));
                    StringBuffer的responseString =新的StringBuffer();
                    串线;
                    而((行= reader.readLine())!= NULL){
                        responseString.append(线);
                    }
                    的System.out.println(respose QQQQQQQQQQQ);
                    的System.out.println(11response
                            + responseString.toString());                }赶上(ClientProtocolException E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }


解决方案

看到这个 http://developer.android.com/reference/android/os/AsyncTask.html 。在doInBackground()方法中写你的code操纵HTTP Post.In preexecute()正在运行的线程前及onPostExecute处理UI()后,线程completed.Don't写UI相关型号code处理UI doinBackground()。

通话AsynTask线程如下:

  ServerCalling任务=新ServerCalling();
            task.execute(新的String [] {门票});

SerivceCalling类:

 公共类ServerCalling扩展的AsyncTask<弦乐,太虚,太虚> {
              @覆盖
        在preExecute保护无效(){        }/ **
         *在最新进展。
         *
         * @参数未使用
         *未使用
         * /
        保护无效onProgressUpdate(虚空未使用){
            大号og.d(HIIIIIIIIIIIIIIIIIIIIIIII,ONPROGRESS更新被称为........................);
        }             @覆盖
        保护无效onPostExecute(虚空未使用){
}    @覆盖
        保护无效doInBackground(字符串... PARAMS){
      HttpClient的HttpClient的=新DefaultHttpClient();
                HttpPost httpPost =新HttpPost(URL这里);
                httpPost.addHeader(内容类型,应用程序/ XML);
                尝试{
                    HTT presponse响应= httpClient.execute(httpPost);
                    读者的BufferedReader =新的BufferedReader(
                            新的InputStreamReader(response.getEntity()
                                    .getContent(),UTF-8));
                    StringBuffer的responseString =新的StringBuffer();
                    串线;
                    而((行= reader.readLine())!= NULL){
                        responseString.append(线);
                    }
                    的System.out.println(respose QQQQQQQQQQQ);
                    的System.out.println(11response
                            + responseString.toString());                }赶上(ClientProtocolException E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }}

I am new to async task . I need to use httppost in my application. Please help me to manipulate the following code using async task. Please give me structured code

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("url here");
                httpPost.addHeader("Content-Type", "application/xml");
                try {
                    HttpResponse response = httpClient.execute(httpPost);
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    StringBuffer responseString = new StringBuffer("");
                    String line;
                    while ((line = reader.readLine()) != null) {
                        responseString.append(line);
                    }
                    System.out.println("respose QQQQQQQQQQQ");
                    System.out.println("11response "
                            + responseString.toString());

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

解决方案

see this http://developer.android.com/reference/android/os/AsyncTask.html. In doInBackground() method write your code for manipulating HTTP Post.In preexecute() handle UI before running thread and in onPostExecute() handle UI after thread completed.Don't write UI releated code in doinBackground().

call AsynTask thread as follows

ServerCalling  task = new ServerCalling();
            task.execute(new String[] { "Tickets" });

SerivceCalling class:

public  class ServerCalling extends  AsyncTask<String, Void, Void> {
              @Override
        protected void onPreExecute() {

        }

/**
         * On progress update.
         * 
         * @param unused
         *            the unused
         */
        protected void onProgressUpdate(Void unused) {
            L

og.d("HIIIIIIIIIIIIIIIIIIIIIIII","ONPROGRESS UPDATE IS CALLED ........................");
        }

             @Override
        protected void onPostExecute(Void unused) {
}

    @Override
        protected Void doInBackground(String... params) {
      HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("url here");
                httpPost.addHeader("Content-Type", "application/xml");
                try {
                    HttpResponse response = httpClient.execute(httpPost);
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    StringBuffer responseString = new StringBuffer("");
                    String line;
                    while ((line = reader.readLine()) != null) {
                        responseString.append(line);
                    }
                    System.out.println("respose QQQQQQQQQQQ");
                    System.out.println("11response "
                            + responseString.toString());

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

}

这篇关于如何使用异步任务用于操纵httppost机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆