2016-04-06 34 views
0

Android geliştirme konusunda yeni. Ben "post" isteği ve "aynı anda" isteği yürütmek için gerekli olduğu bir uygulama yapmaya çalışıyorum ve bunun için asynctask kullanıyorum. iyi çalışıyor ama bazen bir sorun iki veya daha fazla "post" isteği yürütmek için çalışıyor iken uygulamalar sadece bir "post" yürütme iken. ama tüm "post" isteklerini aynı zamanda veya kademeli olarak yürütmem gerekiyor. verilen kod:POST aynı anda birden çok mesaj için yürütme çalışmıyor android

import android.content.Context; 

import android.location.LocationManager; 
import android.os.AsyncTask; 

import android.util.Log; 

import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 

import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 


public class GetPostAsyncTask extends AsyncTask<String, Void, String> { 

// HttpURLConnection httpURLConnection; 


    private static String responseStr = ""; 
    private static String responseStrLogin = ""; 
    private static String responseStrSignUp = ""; 
    private static String responseStrFriendList = ""; 
    private static String responseStrUpdateLocation = ""; 
    private static String responseStrSettings = ""; 
    private static String responseStrSearchFriendForRequest = ""; 
    private static String responseStrSendFriendRequest = ""; 
    private static String responseStrUserFriendRequestList = ""; 
    private static String responseStrApproveFriendRequest = ""; 
    private static String responseStrProfileDetails = ""; 
    private static String responseStrCarDetails=""; 
    private static String responseStrCorporateDetails=""; 
    private static String responseStrPasswordChange=""; 
    private static String responseStrLocationShareToggle=""; 
    private static String responseStrToggleStatus=""; 
    private static String responseStrForgetPassword=""; 
    private static String responseStrSearchResult=""; 
    private static String responseStrDeleteFriend=""; 
    private static String responseStrSearchResultFriendList=""; 
    private static String responseStrShowFriendList=""; 
    private static String responseStrShowFriendProfileDetails=""; 
    private static String responseStrTimeSetForIndividuals=""; 
    private static String responseStrFriendListPeriod=""; 
    private static String responseStrFriendsRouteLatLng=""; 
    private static String responseStrDangerNotification=""; 
    private static String responseStrDangerNotificationPost=""; 



     private final String baseUrl = UserInfo.getSiteUrl(); 

     Context context; 


     GetPostAsyncTask(Context context) { 
      this.context = context; 
     } 

     @Override 
     protected void onPreExecute() { 
      //Toast.makeText(context,"Loading..",Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     protected String doInBackground(String... args) { 

      try { 
       // setting the URL 
       URL url = new URL(baseUrl+args[1]); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.addRequestProperty("User-Agent", "RealTimeApps/1.0"); 
       // setting the method type 
       httpURLConnection.setRequestMethod(args[0]); 
//    httpURLConnection.setChunkedStreamingMode(0); 
       httpURLConnection.setDoInput(true); 
       httpURLConnection.setDoOutput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 

       Log.v("Url",args[2]); 
       // setting the identification key name with query params 
       bufferedWriter.write(args[2]); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 

       Log.v("GetPostA", url.toString()); 
       httpURLConnection.setReadTimeout(15*1000); 
       httpURLConnection.connect(); 
       int getPostStatus = httpURLConnection.getResponseCode(); 

       Log.v("GetPostSts", String.valueOf(getPostStatus)); 



       String line = ""; 
       String res = ""; 
//    if(getPostStatus == 200){ 

        // prepare the output buffer 
        InputStream inputStream = httpURLConnection.getInputStream(); 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 

        while ((line = bufferedReader.readLine()) != null) { 

         res += line; 

        } 

        inputStream.close(); 

//    } 

       httpURLConnection.disconnect(); 

       switch (args[3]){ 
        case "1": 
         responseStrLogin = res.toString(); 
         break; 
        case "2": 
         responseStrSignUp = res.toString(); 
         break; 
        case "3": 
         responseStrFriendList = res.toString(); 
         break; 
        case "4": 
         responseStrUpdateLocation = res.toString(); 
         break; 
        case "5": 
         responseStrSettings = res.toString(); 
         break; 
        case "6": 
         responseStrSearchFriendForRequest = res.toString(); 
         break; 
        case "7": 
         responseStrSendFriendRequest = res.toString(); 
         break; 
        case "8": 
         responseStrUserFriendRequestList = res.toString(); 
         break; 
        case "9": 
         responseStrApproveFriendRequest = res.toString(); 
         break; 
        case "10": 
         responseStrProfileDetails = res.toString(); 
         break; 
        case "11": 
         responseStrCarDetails = res.toString(); 
         break; 
        case "12": 
         responseStrCorporateDetails=res.toString(); 
         break; 
        case "13": 
         responseStrPasswordChange=res.toString(); 
         break; 
        case "14": 
         responseStrLocationShareToggle=res.toString(); 
         break; 
        case "15": 
         responseStrToggleStatus=res.toString(); 
         break; 
        case "16": 
         responseStrForgetPassword=res.toString(); 
         break; 
        case "17": 
         responseStrSearchResult=res.toString(); 
         break; 
        case "18": 
         responseStrDeleteFriend=res.toString(); 
         break; 
        case "19": 
         responseStrSearchResultFriendList=res.toString(); 
         break; 
        case "20": 
         responseStrShowFriendList=res.toString(); 
         break; 
        case "21": 
         responseStrShowFriendProfileDetails=res.toString(); 
         break; 
        case "22": 
         responseStrTimeSetForIndividuals=res.toString(); 
         break; 
        case "23": 
         responseStrFriendListPeriod=res.toString(); 
         break; 

        case "24": 
         responseStrFriendsRouteLatLng=res.toString(); 
         break; 

        case "25": 
         responseStrDangerNotification=res.toString(); 
         break; 

        case "26": 
         responseStrDangerNotificationPost=res.toString(); 
         break; 

        default: 
         responseStr = res.toString(); 
         break; 
       } 
//    Log.v("ResD", res.toString()); 
       return res.toString(); 

      } catch (MalformedURLException e) { 

       e.printStackTrace(); 
       Log.v("GetPostCatchMal",e.toString()); 

      } catch (IOException e) { 
       e.printStackTrace(); 
       Log.v("GetPostCatchIOE", e.toString()); 
      } 

      return null; 

     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 


     @Override 
     protected void onPostExecute(String result) { 

     } 

     public String getResponse(int param){ 
      String tmpStr = "Error"; 
      switch (param){ 
       case 1: 
        tmpStr = responseStrLogin; 
        break; 
       case 2: 
        tmpStr = responseStrSignUp; 
        break; 
       case 3: 
        tmpStr = responseStrFriendList; 
        break; 
       case 4: 
        tmpStr = responseStrUpdateLocation; 
        break; 
       case 5: 
        tmpStr = responseStrSettings; 
        break; 
       case 6: 
        tmpStr = responseStrSearchFriendForRequest; 
        break; 
       case 7: 
        tmpStr = responseStrSendFriendRequest; 
        break; 
       case 8: 
        tmpStr = responseStrUserFriendRequestList; 
        break; 
       case 9: 
        tmpStr = responseStrApproveFriendRequest; 
        break; 
       case 10: 
        tmpStr = responseStrProfileDetails; 
        break; 
       case 11: 
        tmpStr = responseStrCarDetails; 
        break; 
       case 12: 
        tmpStr=responseStrCorporateDetails; 
        break; 
       case 13: 
        tmpStr=responseStrPasswordChange; 
        break; 
       case 14: 
        tmpStr=responseStrLocationShareToggle; 
        break; 
       case 15: 
        tmpStr=responseStrToggleStatus; 
        break; 
       case 16: 
        tmpStr=responseStrForgetPassword; 
        break; 
       case 17: 
        tmpStr=responseStrSearchResult; 
        break; 
       case 18: 
        tmpStr=responseStrDeleteFriend; 
        break; 
       case 19: 
        tmpStr=responseStrSearchResultFriendList; 
        break; 
       case 20: 
        tmpStr=responseStrShowFriendList; 
        break; 
       case 21: 
        tmpStr=responseStrShowFriendProfileDetails; 
        break; 
       case 22: 
        tmpStr=responseStrTimeSetForIndividuals; 
        break; 
       case 23: 
        tmpStr=responseStrFriendListPeriod; 
        break; 

       case 24: 
        tmpStr=responseStrFriendsRouteLatLng; 
        break; 

       case 25: 
        tmpStr=responseStrDangerNotification; 
        break; 

       case 26: 
        tmpStr=responseStrDangerNotificationPost; 
        break; 

       default: 
        tmpStr = responseStr; 
        break; 
       }; 

      return tmpStr; 
     } 


} 
+0

Lütfen bu GetPostAsyncTask uygulamasını nasıl başlattığınızı ekleyin. Async-Task'i başlatmak için Serial-Executer'ı kullanır mısın yoksa anında çalıştırıp yürütücüyü kullanmıyor musunuz? –

+0

backgroundTask.execute ("POST", "main/getFriendInDanger", dangerQuery, "25"); Dize sonucu = backgroundTask.getResponse (25); – Imranrana07

cevap

0

AsyncTask'ınızı başlatmak için Executer'ı iş parçacığıyla birlikte kullanın. Senin durumunda:

backgroundTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"POST","main/getFriendInDanger",dangerQuery,"25"); 

AsyncTask.THREAD_POOL_EXECUTOR Tabii ki özel yürütücüsü (sizin durumunuzda muhtemelen gerek) yapabilir yürütücüsü .Of predeclared edilir. java.util.concurrent.Executers yardımcı yöntemini kullanmak istiyorsanız, uygulama yaşam süresi için yürütücüyü saklamanız gerektiğini ve herhangi bir zamanda yürütme istemediğinizi AsyncTask'ı çalıştırmayacağınızı unutmayın (yürütücünüzü statik alanda tutabilirsiniz).

İlgili konular