SIHttpClient
の単一インスタンスを維持するためにシングルトンを実装するためにクラスを使用していますDefaultHttpClient
。私がこれを行っているのは、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
}
}
}
}
ホームアクティビティ
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
デバッガーを介して結論付けた新しいインスタンスを作成します。ここで私が間違っていること。