如何使用不同的URL调用异步任务

时间:2014-05-08 10:08:57

标签: java android android-asynctask

我需要使用不同的url调用asynctask。我有三个按钮,他们用不同的网址对服务器进行http发布,现在我可以用一个网址调用asynctask但是如何用不同的url调用相同的函数。以下是我的asynctask类:

private class Downloadfiles extends AsyncTask<URL, Integer, JSONObject>{

    @Override
    protected void onPostExecute(JSONObject jo) {
        try {
            cards = jo.getInt("cards");
            points = jo.getInt("points");
            cash = jo.getInt("cash");
        } catch (JSONException e1) {            
            e1.printStackTrace();
        }
        //cardsv.startAnimation(textio);
        cardsv.setText(""+cards);
        cashv.setText(""+cash);

        Log.e("wintype", "msg" + wintype);  
        super.onPostExecute(jo);
    }



    @Override
    protected JSONObject doInBackground(URL... params) {
        Log.e("msg++--", "play game method called");

        BufferedReader reader=null;
        data_to_send = "userId=" + userId ;

        try
        { 
           Log.e("inside try block playgame", "get text" + data_to_send);
          // Defined URL  where to send data
      URL url = new URL(playgame);
   // Send POST data request
    URLConnection conn = url.openConnection(); 
    conn.setDoOutput(true);                   
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
    wr.write(data_to_send);
    wr.flush();     

    // Get the server response 
  reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  StringBuilder sb = new StringBuilder();
  String line = null;

  // Read Server Response
  while((line = reader.readLine()) != null)
      {
             // Append server response in string
             sb.append(line + "\n");
             Log.e("inside playgame", "while loop");
      }


  play_response = sb.toString();
  }
  catch(Exception ex)
  {
      Log.e("MyTag ", "Failure to get json data in playgame--1--", ex);
  }
  finally
  {
      try
      {

          reader.close();
      }

      catch(Exception ex) {
          Log.e("MyTag", "Failure to get json data in playgame--2--", ex);
      }
  }
  Log.e("play response from the server in ", "play game" + play_response);

  JSONObject jo = null;
try {
    jo = new JSONObject(play_response);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


    return jo;
}

}

我用我的一个按钮点击

来调用它
Downloadfiles download = new Downloadfiles();
 downloads.execute(); 

4 个答案:

答案 0 :(得分:0)

将url作为参数传递给异步任务的构造函数。

答案 1 :(得分:0)

AsyncTask&lt;参数,进度,结果&gt;

在您的情况下,您声明了AsyncTask&lt; URL,整数,JSONObject&gt;这意味着你要提供

  • 类型的参数:URL
  • 进度:整数
  • 结果:JSONObject

方法doInBackground(URL ... params)接受multiplme参数, (URL ... params)相当于(URL [] params),您可以像:

一样使用它
URL[] urls= ....."many url here" ; 
Downloadfiles download = new Downloadfiles(urls);
downloads.execute();

在doInBackground内循环遍历网址

答案 2 :(得分:0)

将url作为参数传递给异步任务的构造函数。

更改

 private class Downloadfiles extends AsyncTask<URL, Integer, JSONObject>

private class Downloadfiles extends AsyncTask<String, Integer, JSONObject>

并改变

protected JSONObject doInBackground(URL... params)

protected JSONObject doInBackground(String... params)



 private class Downloadfiles extends AsyncTask<String, Integer, JSONObject>{
       String URL;
      Context _context;

        public Downloadfiles (Context context, String URL)
    {
         this.URL = URL;
         _context = context;
    }
 }

从此类活动中调用

按钮1

new  Downloadfiles (MainActivity.this, "URL1").execute(); 

按钮2

new  Downloadfiles (MainActivity.this, "URL2").execute();

答案 3 :(得分:0)

更改

private class Downloadfiles extends AsyncTask<URL, Integer, JSONObject>{

private class Downloadfiles extends AsyncTask<String, Integer, JSONObject>{

并改变

@Override
    protected JSONObject doInBackground(URL... params) {
        Log.e("msg++--", "play game method called");

@Override
    protected JSONObject doInBackground(String... urls) {
       String url= urls[0];// get the URL String here

最后拨打 AsyncTask

new Downloadfiles (this).execute(URL1);// For Button1

new Downloadfiles (this).execute(URL2);//For Button2 and so on