android用xutils获取tomcat本地服务器器的数据,怎么配置tomcat

android怎样从tomcat下载文件?_百度知道
android怎样从tomcat下载文件?
提问者采纳
己写的话用HttpURLConnection,不过用开源框架去写更简单,afinal和xutils,几行代码搞定
提问者评价
相关专业回答
从tomcat下载文件的配置有几种,以下是常用的设置方式:
创建虚拟目录
首先停止Tomcat服务。打开tomcat里找到conf这个文件夹下的server.xml文件,在里面找到&/Host& 在上面 加上这样的一段:
&Context path=&& docBase=&d:/download& crossContext=&false& debug=&0& reloadable=&true&&&/Context&
然后把tomcat启动一下就OK
在tomcat首页中显示根目录下的...
其他类似问题
为您推荐:
android的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁首先,我们需要建立服务端,这里就用java的一个servlet作为示例吧
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType(&text/html&);
request.setCharacterEncoding(&UTF-8&);
response.setCharacterEncoding(&UTF-8&);
PrintWriter out = response.getWriter();
JSONObject person = new JSONObject();
person.put(&name&, &小明&);
JSONArray phone = new JSONArray();
phone.add(&1333444&);
phone.add(&021333&);
person.put(&phone&, phone);
JSONObject address = new JSONObject();
address.put(&provice&, &安徽&);
address.put(&city&, &合肥&);
person.put(&address&, address);
out.print(person.toString());
直接通过浏览器访问,我们可以得到如下结果
{&name&:&小明&,&phone&:[&1333444&,&021333&],&address&:{&provice&:&安徽&,&city&:&合肥&}}
关于JSON的结果,这里就不详细介绍了,可以看出这里phone有两个,地址又另外分出两个属性,基本将所有可能都包含了。
android端我们选用XUtils框架来写,主要由于android的html请求原生的相当复杂,涉及到ui线程及多线程,这些就让框架给我们做吧。
这里贴出布局的代码跟样式
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&wrap_content&
android:layout_height=&match_parent&
android:paddingBottom=&@dimen/activity_vertical_margin&
android:paddingLeft=&@dimen/activity_horizontal_margin&
android:paddingRight=&@dimen/activity_horizontal_margin&
android:paddingTop=&@dimen/activity_vertical_margin&
tools:context=&com.example.hello.MainActivity& &
android:id=&@+id/button1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignParentBottom=&true&
android:layout_centerHorizontal=&true&
android:layout_marginBottom=&66dp&
android:text=&Button& /&
android:id=&@+id/textView1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignParentLeft=&true&
android:layout_alignParentTop=&true&
android:layout_marginLeft=&18dp&
android:layout_marginTop=&28dp&
android:text=&姓名& /&
android:id=&@+id/textView2&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignLeft=&@+id/textView1&
android:layout_below=&@+id/textView1&
android:layout_marginTop=&24dp&
android:text=&电话& /&
android:id=&@+id/name&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignBaseline=&@+id/textView1&
android:layout_alignBottom=&@+id/textView1&
android:layout_alignLeft=&@+id/textView4&
android:layout_marginLeft=&13dp&
android:ems=&10&
android:inputType=&textPersonName& /&
android:id=&@+id/phone1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignBaseline=&@+id/textView2&
android:layout_alignBottom=&@+id/textView2&
android:layout_alignLeft=&@+id/name&
android:ems=&10&
android:inputType=&phone& &
&requestFocus /&
&/EditText&
android:id=&@+id/provice&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignBaseline=&@+id/textView4&
android:layout_alignBottom=&@+id/textView4&
android:layout_marginLeft=&24dp&
android:layout_toRightOf=&@+id/textView4&
android:ems=&10&
android:inputType=&textPostalAddress& /&
android:id=&@+id/city&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignLeft=&@+id/provice&
android:layout_alignTop=&@+id/textView5&
android:ems=&10&
android:inputType=&textPostalAddress& /&
android:id=&@+id/textView3&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignLeft=&@+id/textView2&
android:layout_below=&@+id/phone1&
android:layout_marginTop=&48dp&
android:text=&地址& /&
android:id=&@+id/textView4&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_below=&@+id/textView3&
android:layout_marginTop=&24dp&
android:layout_toRightOf=&@+id/textView3&
android:text=&省份& /&
android:id=&@+id/textView5&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_centerVertical=&true&
android:layout_toRightOf=&@+id/textView3&
android:text=&城市& /&
android:id=&@+id/phone2&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignLeft=&@+id/phone1&
android:layout_below=&@+id/textView2&
android:ems=&10&
android:inputType=&phone& /&
&/RelativeLayout&
首先,声明好各个View,利用 Xutils注入他们的值
@ViewInject(R.id.button1)
@ViewInject(R.id.name)
private EditT
@ViewInject(R.id.phone1)
private EditText phone1;
@ViewInject(R.id.phone2)
private EditText phone2;
@ViewInject(R.id.provice)
private EditT
@ViewInject(R.id.city)
private EditT记得在oncreate方法里面加入ViewUtils.inject(this);这句代码
其次是一个根据Json文本来获取所有最上层Json对象的方法,方法如下
private JSONArray getJOS(String jsonText) {
JSONTokener jsparser = new JSONTokener(jsonText);
JSONArray jos = new JSONArray();
while ((ob = jsparser.nextValue()) != null)
jos.put(ob);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}记住:一个大括号的内容{ }就是一个JSONObject,中括号[ ]可以理解为一个JSONArray,JSONObject中具体某个属性可以用get(&key&)方法
具体的HTML请求跟JSON的解析如下
@OnClick(R.id.button1)
public void clickMethod(View view) {
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.GET,
&http://172.25.66.227:8080/Android/Test&,
new RequestCallBack&String&() {
public void onLoading(long total, long current,
boolean isUploading) {
public void onSuccess(ResponseInfo&String& responseInfo) {
JSONArray jos = getJOS(responseInfo.result);
JSONObject jo = (JSONObject) jos.get(0);
name.setText((String) jo.get(&name&));
phone1.setText((CharSequence) ((JSONArray) jo
.get(&phone&)).get(0));
phone2.setText((CharSequence) ((JSONArray) jo
.get(&phone&)).get(1));
provice.setText((CharSequence) ((JSONObject) jo
.get(&address&)).get(&provice&));
city.setText((CharSequence) ((JSONObject) jo
.get(&address&)).get(&city&));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public void onStart() {
public void onFailure(HttpException arg0, String arg1) {
}注意:这里的Button事件由XUtils框架注入,具体见XUtils框架文档
其中网址是服务端的网址,顺便说一下,如果是安卓虚拟机访问本地Tomcat,本地电脑对于虚拟机的ip是10.0.0.2,这个很重要。真机的话自己找到电脑的ip吧,前提是对计算机网络要有一定了解,否则这交互没法做。
点击button,看服务端的结果是不是显示在手机上了。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1267次
排名:千里之外
(1)(1)(1)(1)(1)(1)(1)阅读(26143)
xUtils简介
xUtils 包含了很多实用的android工具。xUtils 支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响...xUitls 最低兼容android 2.2 (api level 8)
目前xUtils主要有四大模块:
DbUtils模块:
android中的orm框架,一行代码就可以进行增删改查;支持事务,默认关闭;可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名);支持绑定外键,保存实体时外键关联实体自动保存或更新;自动加载外键关联实体,支持延时加载;支持链式表达查询,更直观的查询语义,参考下面的介绍或sample中的例子。
ViewUtils模块:
android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定;新的事件绑定方式,使用混淆工具混淆后仍可正常工作;目前支持常用的20种事件绑定,参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
HttpUtils模块:
支持同步,异步方式的请求;支持大文件上传,上传大文件不会oom;支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT请求;下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件;返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。
BitmapUtils模块:
加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象;支持加载网络图片和本地图片;内存管理使用lru算法,更好的管理bitmap内存;可配置线程加载线程数量,缓存大小,缓存路径,加载显示动画等...
使用xUtils快速开发框架需要有以下权限:
&uses-permission android:name=&android.permission.INTERNET& /&
&uses-permission android:name=&android.permission.WRITE_EXTERNAL_STORAGE& /&
混淆时注意事项:
添加Android默认混淆配置${sdk.dir}/tools/proguard/proguard-android.txt不要混淆xUtils中的注解类型,添加混淆配置:-keep class * extends java.lang.annotation.Annotation { *; }对使用DbUtils模块持久化的实体类不要混淆,或者注解所有表和列名称@Table(name=&xxx&),@Id(column=&xxx&),@Column(column=&xxx&),@Foreign(column=&xxx&,foreign=&xxx&);
DbUtils使用方法:
DbUtils db = DbUtils.create(this);
User user = new User(); //这里需要注意的是User对象必须有id属性,或者有通过@ID注解的属性
user.setEmail(&&);
user.setName(&wyouflf&);
db.save(user); // 使用saveBindingId保存实体时会为实体的id赋值
Parent entity = db.findById(Parent.class, parent.getId());
List&Parent& list = db.findAll(Parent.class);//通过类型查找
Parent Parent = db.findFirst(Selector.from(Parent.class).where(&name&,&=&,&test&));
// IS NULL
Parent Parent = db.findFirst(Selector.from(Parent.class).where(&name&,&=&, null));
// IS NOT NULL
Parent Parent = db.findFirst(Selector.from(Parent.class).where(&name&,&!=&, null));
// WHERE id&54 AND (age&20 OR age&30) ORDER BY id LIMIT pageSize OFFSET pageOffset
List&Parent& list = db.findAll(Selector.from(Parent.class)
.where(&id& ,&&&, 54)
.and(WhereBuilder.b(&age&, &&&, 20).or(&age&, & & &, 30))
.orderBy(&id&)
.limit(pageSize)
.offset(pageSize * pageIndex));
// op为&in&时,最后一个参数必须是数组或Iterable的实现类(例如List等)
Parent test = db.findFirst(Selector.from(Parent.class).where(&id&, &in&, new int[]{1, 2, 3}));
// op为&between&时,最后一个参数必须是数组或Iterable的实现类(例如List等)
Parent test = db.findFirst(Selector.from(Parent.class).where(&id&, &between&, new String[]{&1&, &5&}));
DbModel dbModel = db.findDbModelAll(Selector.from(Parent.class).select(&name&));//select(&name&)只取出name列
List&DbModel& dbModels = db.findDbModelAll(Selector.from(Parent.class).groupBy(&name&).select(&name&, &count(name)&));
List&DbModel& dbModels = db.findDbModelAll(sql); // 自定义sql查询
db.execNonQuery(sql) // 执行自定义sql
ViewUtils使用方法
完全注解方式就可以进行UI绑定和事件绑定。无需findViewById和setClickListener等。
// xUtils的view注解要求必须提供id,以使代码混淆不受影响。
@ViewInject(R.id.textView)
TextView textV
//@ViewInject(vale=R.id.textView, parentId=R.id.parentView)
//TextView textV
@ResInject(id = R.string.label, type = ResType.String)
private String
// 取消了之前使用方法名绑定事件的方式,使用id绑定不受混淆影响
// 支持绑定多个id @OnClick({R.id.id1, R.id.id2, R.id.id3})
// or @OnClick(value={R.id.id1, R.id.id2, R.id.id3}, parentId={R.id.pid1, R.id.pid2, R.id.pid3})
// 更多事件支持参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
@OnClick(R.id.test_button)
public void testButtonClick(View v) { // 方法签名必须和接口中的要求一致
//在Activity中注入:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewUtils.inject(this); //注入view和事件
textView.setText(&some text...&);
//在Fragment中注入:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bitmap_fragment, container, false); // 加载fragment布局
ViewUtils.inject(this, view); //注入view和事件
//在PreferenceFragment中注入:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewUtils.inject(this, getPreferenceScreen()); //注入view和事件
// 其他重载
// inject(View view);
// inject(Activity activity)
// inject(PreferenceActivity preferenceActivity)
// inject(Object handler, View view)
// inject(Object handler, Activity activity)
// inject(Object handler, PreferenceGroup preferenceGroup)
// inject(Object handler, PreferenceActivity preferenceActivity)
HttpUtils使用方法:
普通get方法
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.GET,
new RequestCallBack&String&(){
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + &/& + total);
public void onSuccess(ResponseInfo&String& responseInfo) {
textView.setText(responseInfo.result);
public void onStart() {
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....&,
new RequestCallBack&String&() {
public void onStart() {
testTextView.setText(&conn...&);
public void onLoading(long total, long current, boolean isUploading) {
if (isUploading) {
testTextView.setText(&upload: & + current + &/& + total);
testTextView.setText(&reply: & + current + &/& + total);
public void onSuccess(ResponseInfo&String& responseInfo) {
testTextView.setText(&reply: & + responseInfo.result);
public void onFailure(HttpException error, String msg) {
testTextView.setText(error.getExceptionCode() + &:& + msg);
使用HttpUtils下载文件:
支持断点续传,随时停止下载任务,开始任务
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download(&/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip&,
&/sdcard/httpcomponents-client-4.2.5-src.zip&,
true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack&File&() {
public void onStart() {
testTextView.setText(&conn...&);
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + &/& + total);
public void onSuccess(ResponseInfo&File& responseInfo) {
testTextView.setText(&downloaded:& + responseInfo.result.getPath());
public void onFailure(HttpException error, String msg) {
testTextView.setText(msg);
//调用cancel()方法停止下载
handler.cancel();
BitmapUtils
BitmapUtils bitmapUtils = new BitmapUtils(this);
// 加载网络图片
bitmapUtils.display(testImageView, &/static/image/common/logo.png&);
// 加载本地图片(路径以/开头, 绝对路径)
bitmapUtils.display(testImageView, &/sdcard/test.jpg&);
// 加载assets中的图片(路径以assets开头)
bitmapUtils.display(testImageView, &assets/img/wallpaper.jpg&);
// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true, customListener));
其他(更多示例代码见sample文件夹中的代码)
// 自动添加TAG,格式: className.methodName(L:lineNumber)
// 可设置全局的LogUtils.allowD = false,LogUtils.allowI = false...,控制是否输出log。
// 自定义log输出LogUtils.customLogger = new xxxLogger();
LogUtils.d(&wyouflf&);
下载地址:/wyouflf/xUtils
阅读排行榜}

我要回帖

更多关于 android 本地服务器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信