最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【基本解决】java中没法new:Cannot instantiate the type HttpParams

Java crifan 18994浏览 0评论

【问题】

折腾:

【教程】模拟登陆百度之Java代码版

期间,想要去创建HttpParams类型一个局部变量headerParams。

Eclipse帮我自动加的是:

HttpParams headerParams;

然后以为需要改为:

HttpParams headerParams = new HttpParams();

结果,就出错了:

如图:

Cannot instantiate the type HttpParams

即:

Cannot instantiate the type HttpParams

【解决过程】

1.看着意思是,是type,所以无需new?

2.参考:

“Cannot instantiate the type…”

其对于其他类,是换别的,具体的某种类型,然后才能new。

3.参考:

Cannot instantiate the type HttpClient

然后再看到:

Interface HttpParams

所以,去试试那个DefaultedHttpParams

HttpParams headerParams = new DefaultedHttpParams();

结果又提示:

The constructor DefaultedHttpParams() is undefined

所以,再去按照Eclipse的提示,去修复为:

HttpParams headerParams = new DefaultedHttpParams(headerParams, headerParams);

也还是不知道如何继续。

4.然后去搞懂很多基本概念和参数的含义:

【基本解决】HttpParams中的BasicHttpParams vs DefaultedHttpParams以及CoreProtocolPNames中的HTTP_CONTENT_CHARSET vs HTTP_ELEMENT_CHARSET

5.而改为这么写:

    	HttpParams headerParams = null;
    	headerParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE);

结果又很明显不对,也有警告:

Null pointer access: The variable headerParams can only be null at this location

6.后来随便去试了试,发现:

HttpParams headerParams = new BasicHttpParams();

是可以编译的通过的:

use BasicHttpParams can compile

 

【总结】

暂时,就用BasicHttpParams吧,也不知道,后续会不会有什么限制。

转载请注明:在路上 » 【基本解决】java中没法new:Cannot instantiate the type HttpParams

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.169 seconds, using 22.13MB memory