gpt4 book ai didi

android - 在 HttpAsyncTask().execute() 中传递参数

转载 作者:行者123 更新时间:2023-11-30 02:47:40 25 4
gpt4 key购买 nike

我的 android 应用程序使用 json 和 Http post 向 Web 服务器发布一个字符串,并使用了以下代码结构,但我想通过 HttpAsyncTask().execute("from here") 将几个参数传递给 AsyncTask<> 类。任何人都可以帮助我如何去做..你的帮助对我来说非常感谢提前感谢

btn_send.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String strData = "Some String to post";
String strURL = "http://My-Url/";
String reqTimeOut = "30000";
String Code = "9990001" ;
String webRequest = SendWebRequest(strURL,strData, reqTimeOut, Code);// method to send HTTpPost request

WriteToFile(webRequest);//writing response to file



private String SendWebRequest(String urlStr, String Data,String reqTimeOut, String Code)

{
// TODO Auto-generated method stub

String result="";
try
{
/*

Some mandatory operations on Data
*/


// Here i want to pass parameters: url, reqTimeout, Data, text and value(for setting header) to POST method.
new HttpAsyncTask().execute(urlStr);

}catch(Exception e){}

return result;
}
public class HttpAsyncTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return POST(params[0]);
}

// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}

private String POST(final String url, final String postData,String text, String value) {
// TODO Auto-generated method stub

InputStream inputStream ;
String result = "";


try {



// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();

// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "2. url is "+url,
Toast.LENGTH_LONG).show();
}
});

String json=postData ;

// 5. set json to StringEntity
StringEntity se = new StringEntity(json);

// 6. set httpPost Entity
httpPost.setEntity(se);
// HttpConnectionParams.setConnectionTimeout(null, 300000);

// 7. Set some headers to inform server about the type of the content
// httpPost.setHeader("Accept", "application/json");
httpPost.setHeader(text, value);

// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);

// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();

// 10. convert inputstream to string
if(inputStream != null){
result = convertInputStreamToString(inputStream);

}
else
result = "Did not work!";

} catch (Exception e) {


Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}

最佳答案

为您的 HttpAsyncTask 类编写参数化构造函数。添加要在 HttpAsyncTask 类中使用的私有(private)字段。然后用所需的参数实例化 HttpAsyncTask 类对象。

你的类结构如下:

public class HttpAsyncTask extends AsyncTask<String, Void, String> {

private String url,reqTimeout,data,text,value;
public HttpAsyncTask(String url,String reqTimeout,String data, String text, String value){
this.url = url;
this.reqTimeout = reqTimeout;
this.data = data;
this.text = text;
this.value = value;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return POST(params[0]);
}

// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}

private String POST(final String url, final String postData,String text, String value) {
// TODO Auto-generated method stub

InputStream inputStream ;
String result = "";


try {



// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();

// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "2. url is "+url,
Toast.LENGTH_LONG).show();
}
});

String json=postData ;

// 5. set json to StringEntity
StringEntity se = new StringEntity(json);

// 6. set httpPost Entity
httpPost.setEntity(se);
// HttpConnectionParams.setConnectionTimeout(null, 300000);

// 7. Set some headers to inform server about the type of the content
// httpPost.setHeader("Accept", "application/json");
httpPost.setHeader(text, value);

// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);

// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();

// 10. convert inputstream to string
if(inputStream != null){
result = convertInputStreamToString(inputStream);

}
else
result = "Did not work!";

} catch (Exception e) {


Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}

然后调用HttpAsyncTask类的execute方法时,调用方法如下:

HttpAsyncTask httpAsyncTask = new HttpAsyncTask(url,reqTimeout,data,text,value); httpAsyncTask().execute(urlStr);

关于android - 在 HttpAsyncTask().execute() 中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24752274/

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