gpt4 book ai didi

Android API 14 - 网络操作/AsyncTask

转载 作者:行者123 更新时间:2023-11-29 22:15:50 24 4
gpt4 key购买 nike

我最近提出了一个基于使用 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 。

Header count: 6
Header: name=Date value=Sun, 11 Dec 2011 16:38:19 GMT
Header: name=Server value=LiteSpeed
Header: name=Connection value=close
Header: name=X-Powered-By value=PHP/5.3.8
Header: name=Content-Type value=text/html
Header: name=Content-Length value=4

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

关于Android API 14 - 网络操作/AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8464744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com