2012-07-24 20 views
0

我試圖從一個靜態函數執行doInBackground AsycTask,但是當我嘗試輸入Void的parms時,出現Java錯誤,說'Void'不是適用於任務 '字符串,太虛,布爾'在靜態函數中執行AsyncTask的問題

總之,這裏是我的代碼:

public class RequestHandler extends AsyncTask<URL, Void, Boolean> { 

    //atributes for Async 
    private static Context context; 
    private RequestHandler origin; 

// Constructor 
    public RequestHandler(Context c) { 

     origin = this; 
     context = c; 

    } 

    public static boolean doLogin(String username, String password) { 
     String URL = "http://127.0.0.1:1337"; 
new RequestHandler(context).execute(URL, Void, false); // what do I put here? 
    } 


    @Override 
    protected Boolean doInBackground(URL... params) { 
     // TODO Auto-generated method stub 
     URL url = null; 
     try { 
      HttpURLConnection urlConnection = null; 
     try { 
      urlConnection = (HttpURLConnection) url.openConnection(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
      try { 
      InputStream in = null; 
      try { 
       in = new BufferedInputStream(urlConnection.getInputStream()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      readStream(in); 
      } finally { 

      } 
     return false; 

      } 
      private String readStream(InputStream is) { 
       try { 
        ByteArrayOutputStream bo = new ByteArrayOutputStream(); 
        int i = is.read(); 
        while(i != -1) { 
        bo.write(i); 
        i = is.read(); 
        } 
        return bo.toString(); 
       } catch (IOException e) { 
        return ""; 
       } 
      } 
} 

回答

1
  1. 把「doLogin方法的AsyncTask類外
  2. new RequestHandler(context).execute(URL);
  3. protected Boolean doInBackground(String... params) { ....}
+0

什麼!?把DoLogin放在課堂外面? !?? – TheBlueCat 2012-07-24 14:51:22

+0

是的,爲什麼你把我鏈接到一個Async教程?我知道所有變量傳遞到哪裏。 – TheBlueCat 2012-07-24 14:52:39

+0

嗯,如果你知道它非常好,那麼不需要問這裏... – 2012-07-24 15:05:18

0

如果你打電話給你RequestHandler在doLogin方法,你正試圖用void作爲一個對象,但它是一個類型,嘗試改變該行:

new RequestHandler(context).execute(URL, null, false); 

並讓您的AsyncTask返回null。