HttpClient接口测试Word格式文档下载.docx

上传人:b****3 文档编号:8060074 上传时间:2023-05-10 格式:DOCX 页数:14 大小:18.60KB
下载 相关 举报
HttpClient接口测试Word格式文档下载.docx_第1页
第1页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第2页
第2页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第3页
第3页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第4页
第4页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第5页
第5页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第6页
第6页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第7页
第7页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第8页
第8页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第9页
第9页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第10页
第10页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第11页
第11页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第12页
第12页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第13页
第13页 / 共14页
HttpClient接口测试Word格式文档下载.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

HttpClient接口测试Word格式文档下载.docx

《HttpClient接口测试Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《HttpClient接口测试Word格式文档下载.docx(14页珍藏版)》请在冰点文库上搜索。

HttpClient接口测试Word格式文档下载.docx

7. 

catch 

(JSONException 

e) 

8. 

e.printStackTrace();

9. 

10. 

11. 

resultJSON 

JSONObject();

12. 

13. 

resultJSON.append("

name"

 

"

Violet"

) 

14. 

.append("

occupation"

developer"

15. 

age"

Integer(22));

16. 

System.out.println(resultJSON.toString());

17. 

18. 

19. 

20. 

response.getWriter().print(resultJSON.toString());

21. 

js代码

1.function 

originalRequest) 

alert(originalRequest.responseText);

var 

myobj 

originalRequest.responseText.evalJSON(true);

alert(myobj.name);

alert(myobj.age);

6.} 

处理Json对象的基本API

JSON包中最常用的两个类就是JSONObject和JSONArray,具体可以参考JSONforjava入门总结

如下是自己模仿的简单例子:

packagecom.james.json;

importorg.json.JSONArray;

importorg.json.JSONObject;

publicclassJsonTest{

publicstaticvoidmain(String[]args){

//TestJSONObject.

JSONObjectjsonobj=newJSONObject("

{'

name'

:

'

jingshou'

'

age'

30}"

Stringname=jsonobj.getString("

intage=jsonobj.getInt("

System.out.println(jsonobj.toString());

System.out.println(name+"

+age);

System.out.println("

**********"

//TestJSONArray.

JSONArrayjsonarray=newJSONArray("

[{'

30},{'

xiaohong'

29}]"

for(inti=0;

i<

jsonarray.length();

i++){

JSONObjectjo=jsonarray.getJSONObject(i);

System.out.println(jo);

Stringname1=jo.getString("

intage1=jo.getInt("

name1="

+name1);

age1="

+age1);

}

}

运行结果如下

{"

30,"

jingshou"

jingshou:

30

**********

name1=jingshou

age1=30

29,"

xiaohong"

name1=xiaohong

age1=29

从以上例子我们看到的基本事实是:

∙可以通过字符串直接构造一个JSONObject

∙JSONObject里的key在显式传入的时候是用单引号包裹起来的,但是打印出来的时候依然是我们期望的双引号

使用httpclient处理API返回

如下例子演示如何使用httpClient获取API返回的JSON字符串以及处理:

importjava.io.IOException;

importorg.apache.http.HttpEntity;

importorg.apache.http.client.ClientProtocolException;

importorg.apache.http.client.methods.CloseableHttpResponse;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.impl.client.CloseableHttpClient;

importorg.apache.http.impl.client.HttpClients;

importorg.apache.http.util.EntityUtils;

publicclassSimpleServiceTest{

publicstaticvoidmain(String[]args)throwsClientProtocolException,IOException{

CloseableHttpClienthttpclient=HttpClients.createDefault();

HttpPosthttppost=newHttpPost("

CloseableHttpResponseresponse=httpclient.execute(httppost);

try{

HttpEntitymyEntity=response.getEntity();

System.out.println(myEntity.getContentType());

System.out.println(myEntity.getContentLength());

StringresString=EntityUtils.toString(myEntity);

//使用返回的字符串直接构造一个JSONObject

JSONObjectjsonobj=newJSONObject(resString);

System.out.println(jsonobj.toString());

//获取返回对象中"

resultSize的值"

intresutltSize=jsonobj.getInt("

resultSize"

System.out.println("

SearchResultsSizeis:

"

+resutltSize);

//获取"

clients"

的值,它是一个JSONArray

JSONArrayjsonarray=jsonobj.getJSONArray("

System.out.println(jsonarray.toString());

}finally{

response.close();

}

运行结果如下:

Content-Type:

text/plain;

charset=UTF-8

-1

1,"

[{"

expirationDate"

0,"

reqStatus"

mostRecentActivity"

1388376890000,"

clientName"

JingshouLi"

"

company"

Pending"

internal"

true,"

clientId"

salesNames"

disabled"

false}]}

1

false}]

小结:

∙通过API返回的JSON字符串直接构造JSON对象

∙如果要读取JSONObject内部数据,需要事先知道对象的结构,所以以上处理方法不具有通用性,只能处理特定的返回

===================================

JSONObject代码实例 

packagecom.xiazdong.json;

importweibo4j.org.json.JSONArray;

importweibo4j.org.json.JSONObject;

publicclassTest{

publicstaticvoidmain(String[]args)throwsException{

xiazdong'

20}"

Stringname=jsonobj.getString("

intage=jsonobj.getInt("

System.out.println(name+"

2.JSONArray代码实例

JSONArrayarray=newJSONArray(Stringstr);

//将String转为JSONArray

intlength=array.length();

//返回Array的长度;

JSONArrayjsonarray=newJSONArray("

20},{'

xzdong'

15}]"

for(inti=0;

Stringname=jsonarray.getJSONObject(i).getString("

intage=jsonarray.getJSONObject(i).getInt("

name="

+name);

age="

3.嵌套JSONObject和JSONArray代码实例

Stringstr="

20,'

book'

['

book1'

book2'

]"

;

JSONObjectobj=newJSONObject(str);

System.out.println(obj.getJSONArray("

book"

).getString(0));

.JSONStringer代码示例

JSONStringer可以用来快速构建一个JSON格式的文本,并转换成String,可以写入文件;

JSONStringer是JSONWriter的子类;

JSONStringer一般通过object().key().value().key().value().endObject()进行构造;

object()表明开始一个对象,即添加{ 

endObject()表明结束一个对象,即添加};

array()表明开始一个数组,即添加一个[;

endArray()表明结束一个数组,即添加一个];

key()表示添加一个key;

value()表示添加一个value;

importweibo4j.org.json.JSONStringer;

publicclassJsonStringerDemo{

JSONStringerstringer=newJSONStringer();

Stringstr=stringer.object().key("

).value("

xiazdong"

).key("

).value(20).endObject().toString();

System.out.println(str);

复杂JSON格式写入

importjava.io.File;

importjava.io.FileReader;

importweibo4j.org.json.JSONTokener;

JSONStringerjs=newJSONStringer();

JSONObjectobj2=newJSONObject();

JSONObjectobj3=newJSONObject();

JSONObjectobj4=newJSONObject();

obj4.put("

title"

"

book1"

).put("

price"

$11"

obj3.put("

obj4);

author"

newJSONObject().put("

author-1"

JSONObjectobj5=newJSONObject();

JSONObjectobj6=newJSONObject();

obj6.put("

book2"

$22"

obj5.put("

obj6);

author-2"

JSONArrayobj7=newJSONArray();

obj7.put(obj3).put(obj5);

obj2.put("

BOOK"

signing"

obj7);

js.object().key("

session"

).value(obj2).endObject();

System.out.println(js.toString());

PrintWriterout=newPrintWriter(newFileOutputStream(1.txt));

out.println(js.toString());

以上代码生成了如下JSON格式:

section"

{

"

[

{

"

{

"

},

}

},

}

]}

5.JSONTokener代码示例

JSONTokener是用来读取JSON格式的文件;

JSONObjectobj=newJSONObject(newJSONTokener(java.io.Reader));

可以从文件中读取一个JSONObject;

JSONArrayobj=newJSONArray(newJSONTokener(java.io.Reader));

可以从文件中读取一个JSONArray;

1.txt

'

]

JSONObjectobj=newJSONObject(newJSONTokener(newFileReader(newFile("

1.txt"

))));

).getString

(1));

//可以读取book2

复杂JSON格式的读取代码:

System.out.println(obj.getJSONObject("

).getJSONArray("

).getJSONObject(0).getJSONObject("

).getString("

<

SPANstyle="

WHITE-SPACE:

pre"

>

<

/SPAN>

//获取author-1

总结:

1在Java中JSON格式的String最好用单引号表示;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2