将 DefaultHttpClient 作为单例调用

发布于 2024-12-05 14:17:05 字数 1370 浏览 0 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薔薇婲 2024-12-12 14:17:05

除了你错误地调用它之外,你的代码没有任何问题。

 DefaultHttpClient object = DefaultHttpClient.getInstance();

而且你们的名字似乎也搞混了。在单例的第一行和第三行将 DefaultHttpClient 重命名为其他名称,例如 MyHttpclient。
这绝对应该解决它。

干杯!

There is nothing wrong with your code except that your calling it wrongly.

 DefaultHttpClient object = DefaultHttpClient.getInstance();

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!

只是偏爱你 2024-12-12 14:17:05

3 件事:

1) 您需要创建一个私有构造函数,以便确定 getInstance() 是创建实例的唯一方法。

private DefaultHttpClient() {
    // Optional Code
}

2) getInstance() 是静态的,因此应该这样调用:

DefaultHttpClient object = DefaultHttpClient.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.

private DefaultHttpClient() {
    // Optional Code
}

2) getInstance() is static so it should be called like this:

DefaultHttpClient object = DefaultHttpClient.getInstance();

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

本王不退位尔等都是臣 2024-12-12 14:17:05

好像没问题,只是你叫错了。 getInstance() 方法是静态的。所以正确的调用方法是:

DefaultHttpClient object = 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:

DefaultHttpClient object = DefaultHttpClient.getInstance();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文