无法跨多个活动实现单例

时间:2013-10-14 12:12:58

标签: android singleton

我正在使用类SIHttpClient来实现维护单DefaultHttpClient实例的单例。我这样做是因为我有learned维护DefaultHttpClient的单个实例将帮助我在后端保持PHP持久化。

public class SIHttpClient {
   private static SIHttpClient instance = null;
   private DefaultHttpClient client;
   private SIHttpClient() {
        this.client=new DefaultHttpClient();
   }
   public static SIHttpClient getInstance() {
      if(instance == null) {
         instance = new SIHttpClient();
      }
      return instance;
   }
   public SIResponse makePostRequest(String url,RequestObject request){
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
        nameValuePair.add(new BasicNameValuePair(request.getParam(), request.getValue()));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
          //error handling
        }
        try {
            HttpResponse response = this.client.execute(httpPost);
            //some parsing done to convert to SIResponse
            return new SIResponse().httpResponseToSIResponse(response);
        } catch (ClientProtocolException e) {
            //exception handling
        } catch (IOException e) {
            //exception handling
        }
   }

   public SIResponse makePostRequest(String url){
        HttpPost httpPost = new HttpPost(url);
        try {
            HttpResponse response = this.client.execute(httpPost);
            //some parsing done to convert to SIResponse
            return new SIResponse().httpResponseToSIResponse(response);
        } catch (ClientProtocolException e) {
            //exception handling
        } catch (IOException e) {
            //exception handling
        }
   }
}
LoginActivity
    public class LoginActivity() extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //setting request oject from some EditTextViews
        RequestObject request=new RequestObject("username","password")

        //set onclick listener code to login
        new UserLoginTask().execute(request);
        //
    }
    //
    private class UserLoginTask extends AsyncTask<RequestObject, Void, SIResponse> { 
        SIHttpClient httpClient=SIHttpClient.getInstance();
        protected Long doInBackground(RequestObject... request) {       
            //request to fetch user data 
            return httpClient.makePostRequest("http://www.example.com/login", request[0]);                     
        }
        protected void onPostExecute(SIResponse result) {
           if(result.getStatus()){
            //got to HomeActivity
           }
           else{
            //display erros
           }
        }
    }
}
  

HomeActivity

public class HomeActivity() extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        new FetchUserDataTask().execute();
    }
    //
    private class FetchUserDataTask extends AsyncTask<Void, Void, SIResponse> { 
        SIHttpClient httpClient=SIHttpClient.getInstance();
        protected Long doInBackground() {       
            //request to fetch user data 
            return httpClient.makePostRequest("http://www.example.com/fetchUser");                     
        }
        protected void onPostExecute(SIResponse result) {
           if(result.getStatus()){
            //populate views with returned data
           }
           else{
            //display erros
           }
        }
    }
}

现在我面临的问题,忘记持久性会话我甚至无法在每次调用SIHttpClient时为getInstance实现Singleton它创建新实例,我通过调试器得出结论。我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

从头开始初始化您的实例:

private static final SIHttpClient instance = new SIHttpClient();

然后返回该实例:

  public static SIHttpClient getInstance() {

  return instance;

}