POST没有执行多个帖子同时发生android

时间:2016-04-06 07:26:01

标签: android

我是android开发的新手。我正在尝试创建一个应用程序,它需要同时执行“发布”请求和“获取”请求,我正在使用asynctask。它工作正常,但有时发生问题是当应用程序尝试执行两个或多个“发布”请求时,它只执行一个“帖子”。但我需要同时或逐步执行所有“发布”请求。下面给出的代码:

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;
        }


}

1 个答案:

答案 0 :(得分:0)

将Executer与线程池一起使用以启动AsyncTask。在你的情况下:

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

AsyncTask.THREAD_POOL_EXECUTOR是预先声明的Executer。当然,您可以自定义Executer(在您的情况下可能不需要)。如果你想使用java.util.concurrent.Executers helper方法,请注意你必须保持执行者的应用程序生命周期,并且不要随时创建执行者想要执行AsyncTask。(你可以将你的执行者留在静态字段中)。 / p>