AsyncTask Cant在全局变量中设置值

时间:2015-09-21 18:28:36

标签: android android-asynctask

以下是我正在尝试实施AsyncTask的功能。我试图在全局变量中设置值,所以我可以在任何地方访问它,但我无法设置它。

private void doLogIn() {
        class SendPostReqAsyncTask extends AsyncTask<Void, Void, String> {

            ProgressDialog dialog;

            // Show Progress bar before downloading Music
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Shows Progress Bar Dialog and then call doInBackground method
                dialog = ProgressDialog.show(SignInActivity.this, "", "Loading. Please wait...", true);
            }

            @Override
            protected String doInBackground(Void... params) {
                HttpClient httpClient = new DefaultHttpClient();
                // In a POST request, we don't pass the values in the URL.
                //Therefore we use only the web page URL as the parameter of the HttpPost argument
                HttpPost httpPost = new HttpPost(MyAppUtil.API_URL + "auth/signIn");
                //Now we put those sending details to an ArrayList with type safe of NameValuePair
                List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();

                for (String key : inputValList.keySet()) {
                    BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, inputValList.get(key).toString());
                    nameValuePairList.add(basicNameValuePair);
                }
                try {
                    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
                    // setEntity() hands the entity (here it is urlEncodedFormEntity) to the request.
                    httpPost.setEntity(urlEncodedFormEntity);
                    try {
                        // HttpResponse is an interface just like HttpPost.
                        //Therefore we can't initialize them
                        HttpResponse httpResponse = httpClient.execute(httpPost);
                        // According to the JAVA API, InputStream constructor do nothing.
                        //So we can't initialize InputStream although it is not an interface
                        InputStream inputStream = httpResponse.getEntity().getContent();
                        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        StringBuilder stringBuilder = new StringBuilder();
                        String bufferedStrChunk = null;
                        while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                            stringBuilder.append(bufferedStrChunk);
                        }
                        return stringBuilder.toString();
                    } catch (ClientProtocolException cpe) {
                        System.out.println("First Exception caz of HttpResponese :" + cpe);
                        cpe.printStackTrace();
                    } catch (IOException ioe) {
                        System.out.println("Second Exception caz of HttpResponse :" + ioe);
                        ioe.printStackTrace();
                    }
                } catch (UnsupportedEncodingException uee) {
                    System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
                    uee.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                //close process dialog
                if (this.dialog != null) {
                    this.dialog.dismiss();
                }
                System.out.println(result);
                try {
                    JsonObjProfile = new JSONObject(result);
                    JSONObject user = JsonObjProfile.getJSONObject("postDataSet");
                    String id = user.getString("id");
                    MyAppUtil.user_id = id;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute();
    }

这里MyAppUtil.user_id = id;是静态类的变量,它将用于从任何地方访问值。

1 个答案:

答案 0 :(得分:0)

静态变量是一个可怕的想法。如果Android的任务管理器杀死了您的应用,那么重新启动它,例如,您的值将会丢失。请改用Shared Preferences

相关问题