我的Android应用程序使用json和Http post将字符串发布到Web服务器并使用了以下代码结构,但我想将几个参数传递给AsyncTask<> class通过HttpAsyncTask()。execute(“from here”)。任何人都可以帮助我如何做到这一点..提前感谢你的帮助对我很有帮助
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;
}
答案 0 :(得分:1)
为您的HttpAsyncTask类编写参数化构造函数。添加要在HttpAsyncTask类中使用的私有字段。然后只需使用必需参数实例化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()执行(urlStr);