2015-07-10 92 views
0

我是Android新手,我正在使用按鈕單擊以使用GCM發送推送通知。我有執行GCM推送的功能。我需要將它從函數轉換爲AsyncTask類。我在互聯網上看到了很多,但仍然面臨困境。任何人都可以幫我轉換它嗎?如何在AsyncTask中執行網絡操作 - Android

推送功能

public void sendPush(int position){ 
    pushProgressBar.setVisibility = (View.VISIBLE) 

     try { 
      // Prepare JSON containing the GCM message content. What to send and where to send. 
      JSONObject jGcmData = new JSONObject(); 
      JSONObject jData = new JSONObject(); 
      jData.put("message", "Hello"); 
      // Where to send GCM message. 
      TinyDB tinyDB = new TinyDB(mContext); 
      ArrayList<String> userToken = tinyDB.getListString("tokenList"); 
      String tokenToSend = userToken.get(position); 
      jGcmData.put("to", tokenToSend); 
      // What to send in GCM message. 
      jGcmData.put("data", jData); 

      // Create connection to send GCM Message request. 
      URL url = new URL("https://android.googleapis.com/gcm/send"); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestProperty("Authorization", "key=" + R.string.apikey); 
      conn.setRequestProperty("Content-Type", "application/json"); 
      conn.setRequestMethod("POST"); 
      conn.setDoOutput(true); 

      // Send GCM message content. 
      OutputStream outputStream = conn.getOutputStream(); 
      outputStream.write(jGcmData.toString().getBytes()); 

      // Read GCM response. 
      InputStream inputStream = conn.getInputStream(); 
      String resp = IOUtils.toString(inputStream); 
      System.out.println(resp); 
      System.out.println("Check your device/emulator for notification or logcat for " + 
        "confirmation of the receipt of the GCM message."); 

      pushProgressBar.setVisibility = (View.GONE) 
     } catch (IOException e) { 
      System.out.println("Unable to send GCM message."); 
      System.out.println("Please ensure that API_KEY has been replaced by the server " + 
        "API key, and that the device's registration token is correct (if specified)."); 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


    } 

在此先感謝。希望我從中學習。

+0

使用齊射。 https://developer.android.com/training/volley/index.html – ElDuderino

+0

我不想使用它,我只是想將此功能轉換爲AsyncTask。如果你知道它。請幫助。不要誤導我。 –

+0

那麼......你的asynctask問題是什麼?試過什麼......?把網絡部分放在doInBackground中......就是這樣。 – ElDuderino

回答

1

例基本的AsyncTask:

class PushTask extends AsyncTask<Integer, Integer, Integer> { 


     @Override 
     protected void onPreExecute() { 
        pushProgressBar.setVisibility = (View.VISIBLE); 
     } 

     @Override 
     protected Integer doInBackground(Integer... position) { 
       int post = position[0]; 
       int respCode = 0; 

       try { 
       //your sending code here.. 
       //got gcm code 
       respCode = 1; 

       } catch (IOException e) { 
       //json IOException 
       //cannot send gcm 
       respCode = 2; 
       } catch (JSONException e) { 
       //json Exception 
       respCode = 3; 
       } 


       return respCode; 
     } 

     @Override 
     protected void onPostExecute(Integer response) { 

       switch(response) 
        case 1: 
         System.out.println("Check your device/emulator for notification or logcat for " + 
         "confirmation of the receipt of the GCM message."); 
        break; 
        case 2: 
         System.out.println("Please ensure that API_KEY has been replaced by the server " + 
         "API key, and that the device's registration token is correct (if specified)."); 
        break; 
        case 3: 
         //print json Exception error 
        break; 
        default: 
        break;      
       pushProgressBar.setVisibility = (View.GONE); 
     } 
    } 

您可以通過執行該類執行

new PushTask().execute(1 or your position); 

這是僅作爲例子,你可以修改根據您的要求的代碼。

+0

好吧,我有一個疑問,我應該在哪裏放置響應處理?在onPostExecute或? –

+0

根據您的代碼,您可以在doInBackground中分配響應代碼並傳遞給onPostExecute來處理響應代碼。我會更新我的答案 – zzas11

+0

謝謝。這將是很好的,如果你用代碼填寫塊,我已經在下面給出。我更糟糕的是搞亂了。請。 –