2014-05-08 32 views
0

我需要用不同的URL调用asynctask。我有三个按钮,他们做一个HTTP POST与不同的URL服务器,现在我可以调用的AsyncTask使用同一个URL,但我怎么能叫有以下不同urls.the同样的功能是我的AsyncTask类:如何使用不同的URL调用异步任务

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(); 
+0

downloads.execute(url1)像这样你可以传递参数在执行 – Madhu

+0

雅我试过,但网址=新的URL(玩游戏); URLConnection conn = url.openConnection();给我错误 – user3472186

+0

这里玩的游戏是什么?你在哪里宣布它? – Madhu

回答

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 
0

将URL传递在异步任务的构造函数的参数。

+0

你能告诉我如何在我的代码中做到这一点 – user3472186

0

的AsyncTask < PARAMS,进展,结果>

在你的情况,你声明的的AsyncTask < URL,整型的JSONObject>是否意味着你会提供类型的

  • PARAMS:URL
  • 进展:整数
  • 结果:JSONObject

方法doInBackground(网址... PARAMS)接受multiplme参数, (网址... PARAMS)相当于(URL [] PARAMS),你可以使用它像:

URL[] urls= ....."many url here" ; 
Downloadfiles download = new Downloadfiles(urls); 
downloads.execute(); 

doInBackground循环内通过网址做工作

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();