怎么在Android中使用凌空实现一个帖子请求

  介绍

本篇文章给大家分享的是有关怎么在Android中使用凌空实现一个帖子请求,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

首先我们在项目中导入这个框架:

implementation  & # 39; com.mcxiaoke.volley:库:1.0.19& # 39;

在AndroidManifest文件当中添加网络权限:

& lt; uses-permission  Android: name=癮ndroid.permission.INTERNET"/祝辞

下面是我们的首页布局:

在这个布局当中我们将凌空框架的所有功能都做成了一个按钮,按下按钮之后就会在“显示结果”下面显示结果,显示结果下面使用了一个滚动视图,并在滚动视图下面嵌套了一个Textview和Imageview,用于把我们加载成功之后的图片和文字进行显示。

怎么在Android中使用凌空实现一个帖子请求

下面是首页布局的代码:

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,xmlns:应用=癶ttp://schemas.android.com/apk/res-auto"   ,xmlns:工具=癶ttp://schemas.android.com/tools"   ,android: layout_width=癿atch_parent"   ,android: layout_height=癿atch_parent"   ,android:取向=皏ertical"   ,工具:上下文=?MainActivity"祝辞   & lt;按钮   ,android: id=癅 + id/get"   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=扒肭蟆?比;   & lt;按钮   ,android: id=癅 + id/post"   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=疤忧肭蟆?比;   & lt;按钮   ,android: id=癅 + id/json"   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=扒肭驤SON"/比;   & lt;按钮   ,android: id=癅 + id/ImageRquest"   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癐mageRquest加载图片“/比;   & lt;按钮   ,android: id=癅 + id/ImageLoader"   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癐mageLoader加载图片“/比;   & lt;按钮   ,android: id=癅 + id/NetWorkImageView"   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癗etWorkImageView加载图片“/比;   & lt; TextView   ,android:文本=跋允窘峁?   ,android: textSize=?0 sp"   ,android: layout_width=皐rap_content"   ,android: layout_height=皐rap_content",/比;   & lt; ImageView   ,android:可见性=癵one"   ,android: id=癅 + id/iv_volley"   ,android: layout_width=皐rap_content"   ,android: layout_height=皐rap_content",/比;   & lt; com.android.volley.toolbox.NetworkImageView   ,android: id=癅 + id/NetWork"   ,android:可见性=癵one"   200年,android: layout_width=癲p"   ,android: layout_height=?00 dp",/比;   & lt;滚动视图   ,android: layout_width=癿atch_parent"   ,android: layout_height=癿atch_parent"祝辞   & lt; TextView   android:才能id=癅 + id/tv_volley_result"   android:才能layout_width=癿atch_parent"   android:才能layout_height=癿atch_parent",/比;   ,& lt;/ScrollView>   & lt;/LinearLayout>

为了实现岗位请求,进行帖子请求一共需要三步,分别是:

1。创建一个请求队列

2。创建一个请求

3。将创建的请求添加到请求队列当中

在创建请求的时候,必须同时写两个监听器,一个是实现请求,正确接受数据的回调,另一个是发生异常之后的回调。这里我们准备了json数据,是在gank.io的官网上找的,大家可以自行百度一下,这里就直接采用了网址:http://api.m.mtime.cn/PageSubArea/TrailerList.api

当中的json数据进行帖子请求了,只要我们在文本显示区返回的数据和这个网站上面的数据显示相同,则请求成功。如果不同也会显示出错误的原因。

实现的核心代码如下:

post.setOnClickListener (new  View.OnClickListener (), {   @Override才能   public 才能;void  onClick (View 视图),{//才能,1,创建一个请求队列   RequestQueue 才能;RequestQueue =, Volley.newRequestQueue (MainActivity.this);//才能,2,创建一个帖子请求   String 才能;url =,“http://api.m.mtime.cn/PageSubArea/TrailerList.api";   StringRequest 才能;StringRequest =, new  StringRequest (Request.Method.POST, url, new  Response.Listener

怎么在Android中使用凌空实现一个帖子请求