如何使用异步任务来操作httppost android

时间:2012-03-27 06:28:23

标签: android android-asynctask http-post

我是异步任务的新手。我需要在我的应用程序中使用httppost。请帮我使用异步任务操作以下代码。请给我结构化代码

                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();
                }

2 个答案:

答案 0 :(得分:0)

使用此代码

private class VerticalChannelTask extends AsyncTask<String, Void, ArrayList<MyChannelItem>> {       

    @Override
    protected void onPreExecute() { 
        super.onPreExecute();   
        show();
    }

    @Override
    protected ArrayList<MyChannelItem> doInBackground(String...params) { 
    /// place your code here i.e Http code..

        return m_orders;
    }       
    protected void onPostExecute(ArrayList<MyChannelItem> result) { 

        hide();         

    }
}

这里我使用的输入参数是String,并且Asynctask的返回值是ArrayList。 根据您的要求改变

答案 1 :(得分:0)

请参阅此http://developer.android.com/reference/android/os/AsyncTask.html。在doInBackground()方法中编写用于操作HTTP Post的代码。在执行线程之前的preexecute()处理UI和在线程完成后的onPostExecute()处理UI中。不要在doinBackground()中编写UI相关代码。

调用AsynTask线程如下

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

SerivceCalling课程:

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();
                }

}