Java getter和setter问题

时间:2017-01-25 17:17:50

标签: java android getter-setter

setData中设置值时,字符串returnDataTab1中的值为OK,但getId()表示该值为空。这个问题是我的表情。

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;
import java.net.URLEncoder;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.widget.Toast;


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

        Context context;
        String dataResponse = "";
        String returnDataTab1;


        //getter
        public String getId()
        {
//The system out below shows a value of null although the setData method set it ok.

            System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzzz : "+returnDataTab1);
            return returnDataTab1;

        }

        public void setData(String newResult)
        {
            returnDataTab1=newResult;

            System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzzz : "+returnDataTab1);

/// the returnDataTab value is set ok

        }






        BackgroundTask(Context ctx)
        {
            this.context = ctx;
        }

        @Override
        protected String doInBackground(String... params)
        {
            String urlLogin  = "";


            String task = params[0];
            String loginusername="";
            String loginpassword="";




            if (task.equals("login"))
            {          
              urlLogin  = "http://domain/LoginAndRegister-login.php";
              loginusername = params[1];
              loginpassword = params[2];
             //


                try 
                {
                    URL url = new URL(urlLogin);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);


                    //send the driver number to the database
                    OutputStream outputStream = httpURLConnection.getOutputStream();
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
                    BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

                    String myDatalogin = URLEncoder.encode("identifier_loginEmail","UTF-8")+"="+URLEncoder.encode(loginusername,"UTF-8")
                    +"&"+URLEncoder.encode("identifier_loginPassword","UTF-8")+"="+URLEncoder.encode(loginpassword,"UTF-8");
                    bufferedWriter.flush();
                    bufferedWriter.write(myDatalogin);
                    bufferedWriter.close();
                    outputStream.close();

                    //get response from the database
                    InputStream inputStream = httpURLConnection.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                    //String dataResponse = "";
                    String inputLine = "";

                     while((inputLine = bufferedReader.readLine()) != null)
                     {
                        dataResponse += inputLine;
                     }

                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();

                    return  dataResponse;
                } 
                catch (MalformedURLException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                } 

            }//end if statement






        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }



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


        /// gets value OK from background task
        @Override
        protected void onPostExecute(String result)

        {
            this.setData(result);       

        }

}

如果你们能看看并指出明显的错误,我们将不胜感激。

这是从标签

调用的代码
 BackgroundTask backgroundTaskLogin = new BackgroundTask(Tab1Activity.this);    
               backgroundTaskLogin.execute(task,username,password);

               String x;
               x=backgroundTaskLogin.getId();

               Toast.makeText(getApplicationContext(), "Data Response : "+x, Toast.LENGTH_SHORT).show();

值从

设置
 protected void onPostExecute(String result)

            {
                this.setData(result);   
// gets result value fine   

            }

0 个答案:

没有答案