Android 访问 Webapi 更新UI
首先,写一个访问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的更多相关文章
- Android子线程更新UI成功
android子线程更新UI成功 今天在写demo的时候,在子线程中更新UI,发现更新成功,记录一下. protected void onCreate(Bundle savedInstanceStat ...
- Android子线程更新UI主线程方法之Handler
背景: 我们开发应用程序的时候,处于线程安全的原因子线程通常是不能直接更新主线程(UI线程)中的UI元素的,那么在Android开发中有几种方法解决这个问题,其中方法之一就是利用Handler处理的. ...
- Android子线程更新UI的方法总结
版权声明:本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=6121280 消息机制,对于Android开发者来说,应该是非常 ...
- android 子线程更新UI
参考http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/package com.exam ...
- android子线程更新UI
参考:https://www.cnblogs.com/joy99/p/6121280.html 子线程是不能直接更新UI的.Android实现View更新有两组方法,分别是invalidate和pos ...
- Android 子线程更新UI 异常
众所周知,Android是不可以在子线程中直接更新UI的,需要借助Handler或者View.post(Runnable runnable)或者runOnUIThread(Runnable runna ...
- android四种更新UI的方法
笔记: // 使用handler.post(Runnable)更新UI public void updateUI_Fun1() { new Thread() { public void run() { ...
- Android开发——子进程更新UI
方式一:Handler和Message ① 实例化一个Handler并重写handlerMessage()方法 private Handler handler = newHandler() { pub ...
- Android笔记——Handler更新UI示例
public class MainActivity extends ActionBarActivity { private TextView textView; private int i=0; @O ...
随机推荐
- Zephyr的Power Management
1 关于Zephyr Zephyr是Linux基金会维护的微内核项目,来源于WindRiver向Zephyr捐赠的Rocket RTOS内核.主要用于开发针对物联网设备的实时操作系统. Zephyr操 ...
- vba基础知识
VBA中dim,static和public,private的区别 转载的文章:出自 红袖的博客http://blog.sina.com.cn/19850701xx 1.Public和Priva ...
- Luogu3763 TJOI2017 DNA NTT/SA
传送门 两种做法: ①SA 将两个串拼在一次建立后缀数组,把\(height\)数组求出来,然后对于\(S\)中每一个长度为\(T\)的串和\(T\)暴力匹配,每一次找到最长的\(LCP\)匹配,如果 ...
- JVM调优——之CMS 常见参数解析
最近在学习使用CMS这个GC,这里记录下常用的参数. 1. UseCMSCompactAtFullCollection 与 CMSFullGCsBeforeCompaction 有一点需要注意的是:C ...
- 有关素数判断的一些算法(总结&&对比)
素性测试是数论题中比较常用的一个技巧.它可以很基础,也可以很高级(哲学).这次主要要介绍一下有关素数判断的奇技淫巧 素数的判断主要分为两种:范围筛选型&&单个判断型 我们先从范围筛选型 ...
- STM32串口打印输出乱码的解决办法
前言 最近在试用uFUN开发板,下载配套的Demo程序,串口数据输出正常,当使用另一个模板工程,调用串口printf调试功能时,输出的却是乱码,最后发现是外部晶振频率不一样.很多STM32开发板都是使 ...
- 发布了一个基于jieba分词的ElasticSearch插件
github地址: https://github.com/hongfuli/elasticsearch-analysis-jieba 基于 jieba 的 elasticsearch 中文分词插件. ...
- 手机APP自动化之uiautomator2 +python3 UI自动化
题记: 之前一直用APPium直到用安卓9.0 发现uiautomatorviewer不支持安卓 9.0,点击截屏按钮 一直报错,百度很久解决方法都不可以,偶然间看见有人推荐:uiautomator ...
- Fedora 19安装mysql
安装数据库模块 Mysql和Mysql-server#yum install mysql mysql-server 开启mysql服务#systemctl start mysqld.service同样 ...
- M2团队贡献分分配
经过考虑,M2阶段团队贡献分分配如下: 团队成员 贡献分 12061166 宋天舒 56 12061157 黄漠源 52 12061159 张迎春 55 12061175 刘翔宇 54 1206117 ...