0

バックグラウンドで可変温度をアップロードするための AsyncTask クラスを作成しようとしています。どのようにパラメータを挿入しますか? Url,variable?このコードを作成しましたが、エラーが発生しました...

public class SimpleHttpPut extends AsyncTask<Void, Void, Void> {


@Override
protected Void doInBackground(Void... params) {

//public static void main(String urlt,int t) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(urlt);
    try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("temp",String.valueOf(t)));
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      HttpResponse response = client.execute(post);
      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      String line = "";
      while ((line = rd.readLine()) != null) {
        System.out.println(line);
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
 // }
 return null;
}

protected void onPostExecute(Void result) {


    }
4

1 に答える 1

3

この SO 投稿に対する私の回答を参照してください...doInBackground関数と Aynctask クラス自体にパラメーターを直接渡すこと、および呼び出し元アクティビティでコールバック関数を呼び出すことについて説明しています。

あなたの場合の要点の答えは、パラメータの文字列配列をdoInBackgroundに渡すことです

通話中:

//params to pass to doInBackground
private String[] params= {"mynamespace", "mymethods", "mysoap", "myuser", "mypass"}; 

//Pass your args array and the current activity to the AsyncTask
new MyTask("my arg1", 10).execute(params);

Asynctask で:

public class MyTask extends AsyncTask<String, Void, String>{
    private String stringArg;
    private int intArg;

    public MyTask(String stringArg, int intArg){
        this.stringArg = stringArg;
        this.intArg = intArg;
    }
    @Override
    protected Void doInBackground(String... params) {
        //These are params local to this function
        String _NAMESPACE = params[0];
        String _METHODNAME = params[1];
        String _SOAPACTION = params[2];
        String _USER_NAME = params[3];
        String _USER_PASS= params[4];

        //intArg & stringArg are now available throughout the class

        //Do background stuff
    }
}
于 2012-12-18T18:31:11.637 に答える