将 DefaultHttpClient 作为单例调用
我想将 DefaultHttpClient 转换为单例类,
public class HttpClient {
private static final String TAG = "&&----HTTPClient-----**";
public static void SendHttpPost (String URL, JSONObject jsonObjSend){
try{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
}
我该怎么做?实际上我尝试了一些实现它们的方法,但对我来说并没有成功。
下面是我的单例的外观示例,
public class DefaultHttpClient {
static DefaultHttpClient instanceCE;
public static DefaultHttpClient getInstance(){
if(instanceCE == null){
instanceCE = new DefaultHttpClient ();
}
return instanceCE;
}
DefaultHttpClient object = new DefaultHttpClient .getInstance(); //calling it here
我知道这个实现是错误的,正在等待您的输入来帮助我。谢谢 !
我已经给出了最终的实现,但它不起作用。
public class HttpClient {
public static DefaultHttpClient instanCE;
private static final String TAG = "&&----HTTPClient-----**";
public static DefaultHttpClient getInstance(){
if(instanCE == null){
instanCE = new DefaultHttpClient();
}
return instanCE;
}
public static void SendHttpPost (String URL, JSONObject jsonObjSend){
try{
DefaultHttpClient httpclient = DefaultHttpClient.getInstance();
HttpPost httpPostRequest = new HttpPost(URL);
I would like to convert the DefaultHttpClient to a singleton class
public class HttpClient {
private static final String TAG = "&&----HTTPClient-----**";
public static void SendHttpPost (String URL, JSONObject jsonObjSend){
try{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
}
How would I go about this ? Actually I tried some methods of implementing them but it didn't work out for me.
Below is an example of how my singleton should look
public class DefaultHttpClient {
static DefaultHttpClient instanceCE;
public static DefaultHttpClient getInstance(){
if(instanceCE == null){
instanceCE = new DefaultHttpClient ();
}
return instanceCE;
}
DefaultHttpClient object = new DefaultHttpClient .getInstance(); //calling it here
I know this implementation is wrong awaiting your inputs to help me. Thanks !
I've put the final implementation which isn't working.
public class HttpClient {
public static DefaultHttpClient instanCE;
private static final String TAG = "&&----HTTPClient-----**";
public static DefaultHttpClient getInstance(){
if(instanCE == null){
instanCE = new DefaultHttpClient();
}
return instanCE;
}
public static void SendHttpPost (String URL, JSONObject jsonObjSend){
try{
DefaultHttpClient httpclient = DefaultHttpClient.getInstance();
HttpPost httpPostRequest = new HttpPost(URL);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了你错误地调用它之外,你的代码没有任何问题。
而且你们的名字似乎也搞混了。在单例的第一行和第三行将 DefaultHttpClient 重命名为其他名称,例如 MyHttpclient。
这绝对应该解决它。
干杯!
There is nothing wrong with your code except that your calling it wrongly.
Also your names seem to have got mixed up. Rename DefaultHttpClient to something else like MyHttpclient on the first and third line of the singleton.
This should fix it definitely.
Cheers!
3 件事:
1) 您需要创建一个私有构造函数,以便确定
getInstance()
是创建实例的唯一方法。2)
getInstance()
是静态的,因此应该这样调用:3) 您缺少关闭
}
编辑:
“我收到一条错误,指出该方法未为 DefaultHttpClient 定义”
如果是这种情况,请确保您导入的是正确的类,而不是
org.apache.http.impl.client.DefaultHttpClient
3 things:
1) You need to create a private constructor, so that you are sure
getInstance()
is the only way to create an instance.2)
getInstance()
is static so it should be called like this:3) You are missing closing
}
EDIT:
"I get an error that states that the method is undefined for DefaultHttpClient"
If this is the case make sure you are importing the correct class and not
org.apache.http.impl.client.DefaultHttpClient
好像没问题,只是你叫错了。 getInstance() 方法是静态的。所以正确的调用方法是:
It seems to be OK, you are just calling it wrong. The getInstance() method is static. So the correct way how to call it is: