volley框架使用小Demo
MainActivity
public class MainActivity extends AppCompatActivity { private static final String URL = "https://www.baidu.com/"; private RequestQueue mQueue; // volley的请求队列 @BindView(R.id.volley_get) Button btn; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQueue = Volley.newRequestQueue(getApplicationContext()); ButterKnife.bind(this); } @OnClick({R.id.volley_get}) public void onClick(View v) { get(); } /** * 创建一个请求,这里我们做一个最简单的通过GET方式请求网页源码的操作。请求成功后打印结果。 */ private void get() { StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() { @Override public void onResponse(String arg0) { //Toast.makeText(getApplicationContext(), arg0, Toast.LENGTH_LONG).show(); textView = (TextView) findViewById(R.id.text); textView.setText(arg0.toString()); //Log.d("onResponse", arg0); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { //Toast.makeText(getApplicationContext(), arg0.toString(), Toast.LENGTH_LONG).show(); textView = (TextView) findViewById(R.id.text); textView.setText(arg0.toString()); } }); mQueue.add(request); }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" 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.an.volleytest.MainActivity"> <Button android:id="@+id/volley_get" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get请求" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/text" android:layout_marginTop="25dp" android:layout_below="@+id/volley_get" android:layout_centerHorizontal="true" /> </RelativeLayout>
点击按钮 hello world 替换为请求网址的html文件
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
使用kotlin实现一个智能聊天机器人「图灵机器人,Android,kotlin」
推荐一部关于Ai的系列漫画,叫做代码的深渊 相关代码已经上传到Github的仓库kotlinRobot 先来看一下实现的效果图 智能机器人效果预览~1.gif 文章思路参考自刘桂林前辈的带领新手快速开发APP ,聊天界面代码参考于郭神的第一行代码第二版第三章3.7节,由衷感谢。自己也写过一篇讲解kotlin几个基础小知识点的文章,kotlin初体验,文中不足之处,希望能得到您的指正,十分感谢。 布局文件 创建我们的项目,勾选kotlin支持,并在build.gradle下填入依赖 implementation 'com.android.support:design:26.1.0' 然后我们进去res-values-styles下,把主题修改成没有标题的样式 <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name...
- 下一篇
[Android]大牛直播SDK基于unity3d平台的rtmp/rtsp直播播放端SDK接口说明
最近,好多开发者在unity3d平台面临一个问题,难于找到一个真正靠谱的低延迟rtmp播放器或rtsp播放器。 不少开发者联系我们,希望大牛直播SDK能出官方版的unity3d直播播放器SDK,以下是Android端的SDK接口: /// <summary> /// Init /// </summary> public int NT_U3D_Init() { return DANIULIVE_RETURN_OK; } /// <summary> /// 开始 /// 返回播放句柄 /// </summary> public long NT_U3D_Open() { if ( 0 != player_obj_.Call<int>("Init", java_obj_cur_activity_) ) { return 0; } return player_obj_.Call<long>("Open"); } /// <summary> /// Register Game Object,用于消息传递 /// &...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS6,CentOS7官方镜像安装Oracle11G