Android API 14-网络 *** 作 AsyncTask

Android API 14-网络 *** 作 AsyncTask,第1张

概述我最近基于对POST数据进行HTTP请求提出了一个问题(在这里找到:AndroidAPI14–POSTDatatoHTTP),并被告知我需要尝试类似AsyncTask的 *** 作,因为我无法在主线程中执行网络 *** 作.简而言之,我不知道该怎么做.任何帮助表示赞赏!这是我的代码:packageme.babblebox.application;imp

我最近基于对POST数据进行http请求提出了一个问题(在这里找到:Android API 14 – POST Data to HTTP),并被告知我需要尝试类似AsyncTask的 *** 作,因为我无法在主线程中执行网络 *** 作.

简而言之,我不知道该怎么做.任何帮助表示赞赏!这是我的代码:

package me.babbleBox.application;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.http.httpResponse;import org.apache.http.nameValuePair;import org.apache.http.clIEnt.ClIEntProtocolException;import org.apache.http.clIEnt.httpClIEnt;import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;import org.apache.http.clIEnt.methods.httpPost;import org.apache.http.impl.clIEnt.DefaulthttpClIEnt;import org.apache.http.message.BasicnameValuePair;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;public class BabbleBoxActivity extends Activity {    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);    }    public voID check_login() {        // Create a new httpClIEnt and Post header        httpClIEnt httpclIEnt = new DefaulthttpClIEnt();        httpPost httppost = new httpPost("http://babbleBox.me/androID/test.PHP");        try {            // Add your data            List<nameValuePair> nameValuePairs = new ArrayList<nameValuePair>(2);            nameValuePairs.add(new BasicnameValuePair("ID", "12345"));            nameValuePairs.add(new BasicnameValuePair("stringdata", "Hi"));            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));            // Execute http Post Request            httpResponse response = httpclIEnt.execute(httppost);        } catch (ClIEntProtocolException e) {            // Todo auto-generated catch block        } catch (IOException e) {            // Todo auto-generated catch block        }    }    public voID check_login_button(VIEw v) {           check_login();    }}

解决方法:

阅读有关Processes and threads和AsyncTask的信息.

以下内容远非完美,但您可以尝试类似方法以开始使用…

public class BabbleBoxActivity extends Activity {    // Leave onCreate() as it is    public voID check_login_button(VIEw v) {        PostTask postTask = new PostTask();        postTask.execute();    }    // EDITED THE liNE BELOW TO INCLUDE 'class'    private class PostTask extends AsyncTask<VoID, VoID, VoID> {        // Move the code that was in check_login to the        // doInBackground method below        protected VoID doInBackground(VoID... params) {            httpClIEnt httpclIEnt = new DefaulthttpClIEnt();            httpPost httppost = new httpPost("http://babbleBox.me/androID/test.PHP");            try {                // Add your data                List<nameValuePair> nameValuePairs = new ArrayList<nameValuePair>(2);                nameValuePairs.add(new BasicnameValuePair("ID", "12345"));                nameValuePairs.add(new BasicnameValuePair("stringdata", "Hi"));                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                // Execute http Post Request                httpResponse response = httpclIEnt.execute(httppost);            } catch (ClIEntProtocolException e) {                // Todo auto-generated catch block            } catch (IOException e) {                // Todo auto-generated catch block            }            return null;        }    }}

实际上,您将需要学习如何将参数传递给AsyncTask并获取它以返回有效结果.

编辑:

出于兴趣,我整理了您的原始代码(没有AsyncTask,但在AndroID v2.2上允许在主线程上进行网络 *** 作).我添加了一些日志记录代码只是为了检查连接是否正常…

httpResponse response = httpclIEnt.execute(httppost);header[] headers = response.getAllheaders();Log.d("BabbleBox", "header count: " + headers.length);for (int c = 0; c < headers.length; c++)    Log.d("BabbleBox", "header: name=" + headers[c].getname() + " value=" + headers[c].getValue());

…而我得到了以下响应头.

header count: 6header: name=Date value=Sun, 11 Dec 2011 16:38:19 GMTheader: name=Server value=liteSpeedheader: name=Connection value=closeheader: name=X-Powered-By value=PHP/5.3.8header: name=Content-Type value=text/HTMLheader: name=Content-Length value=4

来自任何网络“请求”(GET,POST,PUT)的响应将根据您正在与之交谈的远程服务而有所不同.您需要确定服务器将要返回的内容,然后相应地处理响应.

总结

以上是内存溢出为你收集整理的Android API 14-网络 *** 作/ AsyncTask全部内容,希望文章能够帮你解决Android API 14-网络 *** 作/ AsyncTask所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1093161.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存