首先,写一个访问webapi的工具类

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject; import java.io.IOException;
import java.util.List; /**
* Created by Tyler on 2018/1/18
*/ public class HttpWebapi {
public static String DoGet(String url) throws Exception { HttpGet httpGet = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(httpGet);
HttpEntity he = resp.getEntity();
String respContent = EntityUtils.toString(he, "UTF-8"); return respContent; } public static String DoPost(String url, List<NameValuePair> nvps) throws Exception {
HttpPost httpost = new HttpPost(url);
HttpClient client = new DefaultHttpClient();
httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse resp = client.execute(httpost);
HttpEntity he = resp.getEntity();
String respContent = EntityUtils.toString(he, "UTF-8");
return respContent;
} public static String DoPost(String url, JSONObject template)throws Exception{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
httpPost.setEntity(new StringEntity(template.toString(),"utf-8"));
HttpResponse resp = httpClient.execute(httpPost);
String respContent = EntityUtils.toString(resp.getEntity(), "UTF-8");
return respContent;
} //测试
public static String getRequest(String url) {
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
// HttpEntity entity = rp.getEntity();
HttpResponse rp;
try {
rp = hc.execute(get);
int statusCode = rp.getStatusLine().getStatusCode();
return statusCode + "";
} catch (ClientProtocolException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return "";
} }
 

使用的jar:

具体调用方式:http://blog.csdn.net/hanjun0612/article/details/73741127

然后主界面

public class MainActivity extends AppCompatActivity {
EditText editText;
//Handler更新UI
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg){
//信息接收完成
if(msg.what==1){
editText.setText(msg.toString());
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText);
//得到按钮实例
Button hellobtn = (Button) findViewById(R.id.hellobutton);
//设置监听按钮点击事件
hellobtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开启线程,抓取数据
new Thread(new Runnable() {
@Override
public void run() {
try { String json = HttpWebapi.DoGet("http://192.168.1.88:802/api/WMS_Product/Test");
Message msg = Message.obtain();
msg.obj = json;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
} }
}).start(); }
}); } }

UI界面:

<Button
android:id="@+id/hellobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="320dp"
android:text="@string/button_send"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <EditText
android:id="@+id/editText"
android:layout_width="375dp"
android:layout_height="299dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:ems="10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Android 访问 Webapi 更新UI的更多相关文章

  1. Android子线程更新UI成功

    android子线程更新UI成功 今天在写demo的时候,在子线程中更新UI,发现更新成功,记录一下. protected void onCreate(Bundle savedInstanceStat ...

  2. Android子线程更新UI主线程方法之Handler

    背景: 我们开发应用程序的时候,处于线程安全的原因子线程通常是不能直接更新主线程(UI线程)中的UI元素的,那么在Android开发中有几种方法解决这个问题,其中方法之一就是利用Handler处理的. ...

  3. Android子线程更新UI的方法总结

    版权声明:本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=6121280 消息机制,对于Android开发者来说,应该是非常 ...

  4. android 子线程更新UI

    参考http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/package com.exam ...

  5. android子线程更新UI

    参考:https://www.cnblogs.com/joy99/p/6121280.html 子线程是不能直接更新UI的.Android实现View更新有两组方法,分别是invalidate和pos ...

  6. Android 子线程更新UI 异常

    众所周知,Android是不可以在子线程中直接更新UI的,需要借助Handler或者View.post(Runnable runnable)或者runOnUIThread(Runnable runna ...

  7. android四种更新UI的方法

    笔记: // 使用handler.post(Runnable)更新UI public void updateUI_Fun1() { new Thread() { public void run() { ...

  8. Android开发——子进程更新UI

    方式一:Handler和Message ① 实例化一个Handler并重写handlerMessage()方法 private Handler handler = newHandler() { pub ...

  9. Android笔记——Handler更新UI示例

    public class MainActivity extends ActionBarActivity { private TextView textView; private int i=0; @O ...

随机推荐

  1. docker数据卷管理及网络基础配置

    数据卷 数据卷容器 数据卷迁移数据 端口映射 容器间通信 数据卷的管理 当需要查看容器内应用产生的数据或者把容器内数据备份及多个容器数据共享.有两种方式,数据卷以及数据卷容器. 数据卷 数据卷是一个可 ...

  2. Omi-touch实战 移动端图片轮播组件的封装

    pc端的轮播,移动端的轮播都很常见.一年前,我还为手机端没有左滑,右滑事件从而封装了一个swipe库,可以自定义超过多少滑动时间就不触发,也可以设置滑动多少距离才触发,这一个功能的代码就达到400多行 ...

  3. jdk和cglib简单理解

    之前使用cglib的时候不需要将classLoader作为参数传入,但动态代理却要,带着这个疑惑进入这个方法: Proxy.newProxyInstance(classLoader, interfac ...

  4. HashMap 的实现原理

    hashMap用了一个名字为table的数组:还有若干个名字为entry的链表.看hashMap是如何应用这些数据结构的.用插 入<key,value>举例:hashMap首先会通过key ...

  5. RabbmitMQ-Publish/Subscribe

    之前的学习中,我们了解的工作队列实现的是:一个消息只发送到一个消费者. 现在我们来学习一下新模式:发布/订阅模式 之前我们在原理中介绍了exchange,但好像并没有使用.而是直接往队列里发消息和取消 ...

  6. Luogu P4137 Rmq Problem / mex

    区间mex问题,可以使用经典的记录上一次位置之后再上主席树解决. 不过主席树好像不是很好写哈,那我们写莫队吧 考虑每一次维护什么东西,首先记一个答案,同时开一个数组记录一下每一个数出现的次数. 然后些 ...

  7. 【调试技巧】 Fiddler高级用法之url映射请求

    问题场景: 已发布线上APP出现接口错误,如何测试线上APP访问本地请求? 已发布线上H5页面,静态资源或js调试,如何映射本地js? 一般解决方案: 猜测(一般明显问题). 找到原发布包,修改请求资 ...

  8. Mvc_缓存浅谈

    缓存是将信息放在内存中以避免频繁访问数据库从数据库中提取数据,在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法. 对于MVC有Control缓存和Action缓存. 一.Control缓存 ...

  9. (第十二周)final预发布视频

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 Final阶段视频发布 平台:优酷 链接:http://v.youku.com/v_show/id_XMTg0 ...

  10. Zookeeper 源码学习(一)环境搭建

    前言 最近准备学习 Zookeeper,想从 Zookeeper 开始逐步深入了解各类中间件,学习分布式计算. 下载源码 执行指令,下载代码: git clone https://github.com ...