1、将服务器zhbj文件放在tomcat\webapps\ROOT下,启动服务器
2、①、Request(此处使用Fiddler软件)
在RAW中可以得到这些数据:
GET http://localhost:8080/zhbj/categories.json HTTP/1.1
User-Agent: Fiddler
Host: localhost:8080
第一行是:请求消息行
第二、三行是:请求消息头Json或(Key-Value&Key-Value)
②、Response
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"1171-1476250269703"
Last-Modified: Wed, 12 Oct 2016 05:31:09 GMT
Content-Type: application/json
Content-Length: 1171
Date: Wed, 12 Oct 2016 07:20:03 GMT
{"retcode":200,"data":[{"id":10000,"title":"新闻","type":1,"children":[{"id":10007,"title":"北京","type":1,"url":"/10007/list_1.json"},{"id":10006,"title":"中国","type":1,"url":"/10006/list_1.json"},{"id":10008,"title":"国际","type":1,"url":"/10008/list_1.json"},{"id":10010,"title":"体育","type":1,"url":"/10010/list_1.json"},{"id":10091,"title":"生活","type":1,"url":"/10091/list_1.json"},{"id":10012,"title":"旅游","type":1,"url":"/10012/list_1.json"},{"id":10095,"title":"科技","type":1,"url":"/10095/list_1.json"},{"id":10009,"title":"军事","type":1,"url":"/10009/list_1.json"},{"id":10093,"title":"时尚","type":1,"url":"/10093/list_1.json"},{"id":10011,"title":"财经","type":1,"url":"/10011/list_1.json"},{"id":10094,"title":"育儿","type":1,"url":"/10094/list_1.json"},{"id":10105,"title":"汽车","type":1,"url":"/10105/list_1.json"}]},{"id":10002,"title":"专题","type":10,"url":"/10006/list_1.json","url1":"/10007/list1_1.json"},{"id":10003,"title":"组图","type":2,"url":"/10008/list_1.json"},{"id":10004,"title":"互动","type":3,"excurl":"","dayurl":"","weekurl":""}],"extend":[10007,10006,10008,10014,10012,10091,10009,10010,10095]}
Request:
1、请求消息行:请求方式,url地址
2、请求消息头:key-value
3、请求内容:
掌握Url,请求方式,请求参数,消息头
Response:
1、响应消息行:相应的状态码
2、响应消息头:
3、响应内容:Json、xml、text
状态码、内容
———————————————回到正题——————————————
HttpUtils使用方法:
普通get方法
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.GET,
"http://www.lidroid.com",
new RequestCallBack<String>(){
@Override
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + "/" + total);
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
textView.setText(responseInfo.result);
}
@Override
public void onStart() {
}
@Override
public void onFailure(HttpException error, String msg) {
}
});
使用HttpUtils上传文件 或者 提交数据 到服务器(post方法)
RequestParams params = new RequestParams();
params.addHeader("name", "value");
params.addQueryStringParameter("name", "value");
// 只包含字符串参数时默认使用BodyParamsEntity,
// 类似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。
params.addBodyParameter("name", "value");
// 加入文件参数后默认使用MultipartEntity("multipart/form-data"),
// 如需"multipart/related",xUtils中提供的MultipartEntity支持设置subType为"related"。
// 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如:
// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
// 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset));
params.addBodyParameter("file", new File("path"));
...
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.POST,
"uploadUrl....",
params,
new RequestCallBack<String>() {
@Override
public void onStart() {
testTextView.setText("conn...");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
if (isUploading) {
testTextView.setText("upload: " + current + "/" + total);
} else {
testTextView.setText("reply: " + current + "/" + total);
}
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
testTextView.setText("reply: " + responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
testTextView.setText(error.getExceptionCode() + ":" + msg);
}
});
使用HttpUtils下载文件:
支持断点续传,随时停止下载任务,开始任务
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download("http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip",
"/sdcard/httpcomponents-client-4.2.5-src.zip",
true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack<File>() {
@Override
public void onStart() {
testTextView.setText("conn...");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + "/" + total);
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
testTextView.setText("downloaded:" + responseInfo.result.getPath());
}
@Override
public void onFailure(HttpException error, String msg) {
testTextView.setText(msg);
}
});
...
//调用cancel()方法停止下载
handler.cancel();
...
回归正题:
NewsCenterTabController.java
package huaxa.it.zhihuidemo.base.tab;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import huaxa.it.zhihuidemo.base.TabController;
import huaxa.it.zhihuidemo.utils.Constans;
/**
*
* @项目名: ZhiHuiDemo
* @包名: huaxa.it.zhihuidemo.base.tab
* @类名: NewsCenterTabController
* @创建者: 黄夏莲
* @创建时间: 2016年10月9日 ,上午7:29:31
*
* @描述: 新闻中心对应的controller
*/
public class NewsCenterTabController extends TabController
{
private TextView text;
public NewsCenterTabController(Context context)
{
super(context);
}
protected View initContentView(Context context)
{
text = new TextView(context);
text.setTextColor(Color.RED);
text.setTextSize(24);
text.setGravity(Gravity.CENTER);
return text;
}
@Override
public void initData()
{
// 设置menu是否可见
mIbMenu.setVisibility(View.VISIBLE);
// 设置标题数据
mTvTitle.setText("新闻");
// 设置内容数据
text.setText("新闻中心的内容");
// 去网络加载数据
// *Url,请求方式,请求参数,消息头*
HttpUtils util = new HttpUtils();
String url = Constans.NewsCenter_URI;
// RequestParams params = new RequestParams();
// params.addBodyParameter(key, value);//请求的内容中的key-value
// params.addQueryStringParameter(key,value);//请求行中的(GET的请求行中可以加“?key=&value=
// ”)
// params.addHeader(name, value);//可以自定义请求消息头
util.send(HttpMethod.GET, url, new RequestCallBack<String>()
{
private String TAG = "TAG";
@Override
public void onSuccess(ResponseInfo<String> responseInfo)
{
// 响应状态码,Success指的是访问服务器成功,返回状态码与服务器有关而不一定是200,返回状态码不确定。
// 这个状态码要和提供的状态码匹配,访问接口才会成功。
// int statusCode = responseInfo.statusCode;
// 访问接口成功(此处先默认是匹配的)
String result = responseInfo.result;
Log.i(TAG, "访问接口成功" + result);
}
@Override
public void onFailure(HttpException error, String msg)
{
// 访问接口失败
Log.i(TAG, "访问接口失败" + msg);
}
});
}
}
Constans.java
package huaxa.it.zhihuidemo.utils;
/**
* @项目名: ZhiHuiDemo
* @包名: huaxa.it.zhihuidemoUtils
* @类名: Constans
* @创建者: 黄夏莲
* @创建时间: 2016年10月12日 ,下午4:26:35
*
* @描述: TODO
*/
public interface Constans
{
//android虚拟机的固定IP
String Base_URI = "http://10.0.2.2:8080/zhbj";
//新闻中心的网络接口
String NewsCenter_URI = Base_URI + "/categories.json";
}
运行结果: