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 ...
随机推荐
- C++11 并发指南三(std::mutex 详解)
上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法. Mutex ...
- Attention[Content]
0. 引言 神经网络中的注意机制就是参考人类的视觉注意机制原理.即人眼在聚焦视野区域中某个小区域时,会投入更多的注意力到这个区域,即以"高分辨率"聚焦于图像的某个区域,同时以&qu ...
- CF662C Binary Table FWT
传送门 \(N \leq 20\)很小诶 一个暴力的思路是枚举行的翻转状态然后在列上贪心 复杂度为\(O(2^NM)\)显然过不去 考虑到可能有若干列的初始状态是一样的,那么在任意反转之后他们贪心的策 ...
- CF58E Expression 搜索
题目传送门:http://codeforces.com/problemset/problem/58/E 题意:给出一个形如$x+y=z$(不一定正确)的式子,试输出一个$a+b=c$的式子,满足:$1 ...
- JS-JS作用域问题
一. js没有块级作用域(可以自己闭包或其他方法实现),只有函数级作用域,函数外面的变量函数里面可以找到,函数里面的变量外面找不到. var a=10; function fn(){ console. ...
- nodejs-日志组件log4js的使用方法
log4js是在nodejs一个非常好用的日志组件,但是今天在使用的时候从网上查看的代码并不能运行(http://www.cnblogs.com/atp-sir/p/7070050.html),于是查 ...
- Puppet常识梳理
Puppet简单介绍 1)puppet是一种Linux/Unix平台下的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统 ...
- 【CV】ICCV2015_Describing Videos by Exploiting Temporal Structure
Describing Videos by Exploiting Temporal Structure Note here: it's a learning note on the topic of v ...
- 《Linux及安全》课程实践二
编译生成新内核 一.实践原理 Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个单内核.单内核由于所有内容都集成在一起,效率很高,但可扩展 ...
- hg命令
hg常用命令 hg命令跟git命令大同小异 hg version 查看hg版本 hg clone url 克隆代码仓库 hg branch newBranch 创建分支 hg update other ...