最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android框架Volley使用之Json請(qǐng)求實(shí)現(xiàn)

 更新時(shí)間:2019年05月16日 09:56:08   作者:Geeksongs  
這篇文章主要介紹了Android框架Volley使用之Json請(qǐng)求實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下

首先我們?cè)陧?xiàng)目中導(dǎo)入這個(gè)框架:

implementation 'com.mcxiaoke.volley:library:1.0.19'

在AndroidManifest文件當(dāng)中添加網(wǎng)絡(luò)權(quán)限:

<uses-permission android:name="android.permission.INTERNET"/>

下面是我們的首頁布局:

在這個(gè)布局當(dāng)中我們將Volley框架的所有功能都做成了一個(gè)按鈕,按下按鈕之后就會(huì)在“顯示結(jié)果”下面顯示結(jié)果,顯示結(jié)果下面使用了一個(gè)ScrollView,并在ScrollView下面嵌套了一個(gè)Textview和Imageview,用于把我們加載成功之后的圖片和文字進(jìn)行顯示。

下面是首頁布局的代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity">
<Button
  android:id="@+id/get"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Get請(qǐng)求"/>
  <Button
    android:id="@+id/post"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Post請(qǐng)求"/>
  <Button
    android:id="@+id/json"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="請(qǐng)求JSON"/>
  <Button
    android:id="@+id/ImageRquest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ImageRquest加載圖片"/>
  <Button
    android:id="@+id/ImageLoader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ImageLoader加載圖片"/>
  <Button
    android:id="@+id/NetWorkImageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="NetWorkImageView加載圖片"/>
  <TextView
    android:text="顯示結(jié)果"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <ImageView
    android:visibility="gone"
    android:id="@+id/iv_volley"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <com.android.volley.toolbox.NetworkImageView
    android:id="@+id/NetWork"
    android:visibility="gone"
    android:layout_width="200dp"
    android:layout_height="200dp" />
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
      android:id="@+id/tv_volley_result"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
  </ScrollView>
</LinearLayout>

為了實(shí)現(xiàn)Json請(qǐng)求,進(jìn)行Json請(qǐng)求一共需要三步,分別是:

1.創(chuàng)建一個(gè)請(qǐng)求隊(duì)列

2.創(chuàng)建一個(gè)請(qǐng)求

3.將創(chuàng)建的請(qǐng)求添加到請(qǐng)求隊(duì)列當(dāng)中

在創(chuàng)建請(qǐng)求的時(shí)候,必須同時(shí)寫兩個(gè)監(jiān)聽器,一個(gè)是實(shí)現(xiàn)請(qǐng)求,正確接受數(shù)據(jù)的回調(diào),另一個(gè)是發(fā)生異常之后的回調(diào)。這里我們準(zhǔn)備了json數(shù)據(jù),是在gank.io的官網(wǎng)上找的,大家可以自行百度一下,這里就直接采用了網(wǎng)址:

網(wǎng)址:

http://gank.io/api/xiandu/category/wow

當(dāng)中的json數(shù)據(jù)進(jìn)行json請(qǐng)求了,只要我們?cè)谖谋撅@示區(qū)返回的數(shù)據(jù)和這個(gè)網(wǎng)站上面的數(shù)據(jù)顯示相同,則請(qǐng)求成功。如果不同也會(huì)顯示出錯(cuò)誤的原因。

在我們進(jìn)行請(qǐng)求的時(shí)候,如果發(fā)現(xiàn)我們被請(qǐng)求的json數(shù)據(jù)是以中括號(hào)開頭的則使用
JsonArrayRequest
來創(chuàng)建對(duì)象,否則則使用下面代碼當(dāng)中的:
JsonObjectRequest
來創(chuàng)建對(duì)象

核心代碼如下:

json.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
// 1 創(chuàng)建一個(gè)請(qǐng)求隊(duì)列
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        // 2 創(chuàng)建一個(gè)請(qǐng)求
        String url = "http://gank.io/api/xiandu/category/wow";
        //JsonArrayRequest jsonObjectRequest2=......
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
          @Override
          public void onResponse(JSONObject jsonObject) {
            tv_volley_result.setText(jsonObject.toString());
          }
        }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError volleyError) {
            tv_volley_result.setText("請(qǐng)求失敗" + volleyError);
          }
        });
        // 3 將創(chuàng)建的請(qǐng)求添加到請(qǐng)求隊(duì)列中
        requestQueue.add(jsonObjectRequest);
//這一步完成之后就可以使用我們的json解析了
      }
    });

全部主活動(dòng)的Java代碼如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
  private Button get;
  private Button post;
  private Button json;
  private Button imagerequest;
  private Button imageload;
  private Button netWorkImageView;
  private ImageView iv;
  private NetworkImageView network;
  private TextView tv_volley_result;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initview();
    initListener();
  }
  public void initview()//把需要初始化的控件的邏輯都寫在這里是一個(gè)很好的編程范式
  {
    get=findViewById(R.id.get);
    post=findViewById(R.id.post);
    json=findViewById(R.id.json);
    imagerequest=findViewById(R.id.ImageRquest);
    imageload=findViewById(R.id.ImageLoader);
    netWorkImageView=findViewById(R.id.NetWorkImageView);
    iv=findViewById(R.id.iv_volley);
    network=findViewById(R.id.NetWork);
    tv_volley_result=findViewById(R.id.tv_volley_result);
  }
  public void initListener()
  {
    get.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        //創(chuàng)建一個(gè)請(qǐng)求隊(duì)列
        RequestQueue requestQueue=Volley.newRequestQueue(MainActivity.this);
        //創(chuàng)建一個(gè)請(qǐng)求
        String url="http://gank.io/api/xiandu/category/wow";
        StringRequest stringRequest=new StringRequest(url, new Response.Listener<String>() {
          //正確接受數(shù)據(jù)之后的回調(diào)
          @Override
          public void onResponse(String response) {
          tv_volley_result.setText(response);
          }
        }, new Response.ErrorListener() {//發(fā)生異常之后的監(jiān)聽回調(diào)
          @Override
          public void onErrorResponse(VolleyError error) {
            tv_volley_result.setText("加載錯(cuò)誤"+error);
          }
        });
        //將創(chuàng)建的請(qǐng)求添加到請(qǐng)求隊(duì)列當(dāng)中
        requestQueue.add(stringRequest);
      }
    });
    post.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        // 1 創(chuàng)建一個(gè)請(qǐng)求隊(duì)列
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        // 2 創(chuàng)建一個(gè)post請(qǐng)求
        String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
          @Override
          public void onResponse(String s) {
            tv_volley_result.setText(s);
          }
        }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError volleyError) {
            tv_volley_result.setText("請(qǐng)求失敗" + volleyError);
          }
        }) {
          @Override
          protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
//            map.put("value1","param1");
            return map;
          }
        };
        // 3 將post請(qǐng)求添加到隊(duì)列中
        requestQueue.add(stringRequest);

      }
    });
    json.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
// 1 創(chuàng)建一個(gè)請(qǐng)求隊(duì)列
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        // 2 創(chuàng)建一個(gè)請(qǐng)求
        String url = "http://gank.io/api/xiandu/category/wow";
        //JsonArrayRequest jsonObjectRequest2=......
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
          @Override
          public void onResponse(JSONObject jsonObject) {
            tv_volley_result.setText(jsonObject.toString());
          }
        }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError volleyError) {
            tv_volley_result.setText("請(qǐng)求失敗" + volleyError);
          }
        });
        // 3 將創(chuàng)建的請(qǐng)求添加到請(qǐng)求隊(duì)列中
        requestQueue.add(jsonObjectRequest);
//這一步完成之后就可以使用我們的json解析了
      }
    });
    imagerequest.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      }
    });
    imageload.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      }
    });
    netWorkImageView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      }
    });
  }
}

得到下圖:


總結(jié)

以上所述是小編給大家介紹的Android框架Volley使用之Json請(qǐng)求實(shí)現(xiàn),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • Android中Root權(quán)限獲取的簡單代碼

    Android中Root權(quán)限獲取的簡單代碼

    那么我們?cè)贏ndroid開發(fā)中如何獲取Android的Root權(quán)限呢?下面是主要的簡單代碼。
    2013-06-06
  • Android RIL使用詳解

    Android RIL使用詳解

    這篇文章主要介紹了Android RIL使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例

    android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例

    本篇文章主要介紹了android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android RecyclerView四級(jí)緩存源碼層詳細(xì)分析

    Android RecyclerView四級(jí)緩存源碼層詳細(xì)分析

    RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來講解RecyclerView的用法
    2022-11-11
  • Android AsyncTask實(shí)現(xiàn)機(jī)制詳細(xì)介紹及實(shí)例代碼

    Android AsyncTask實(shí)現(xiàn)機(jī)制詳細(xì)介紹及實(shí)例代碼

    這篇文章主要介紹了Android AsyncTask實(shí)現(xiàn)機(jī)制詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,這里附有示例代碼,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下
    2016-12-12
  • Android開發(fā)之自動(dòng)朗讀TTS用法分析

    Android開發(fā)之自動(dòng)朗讀TTS用法分析

    這篇文章主要介紹了Android開發(fā)之自動(dòng)朗讀TTS用法,較為詳細(xì)的分析了TTS的概念、功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-06-06
  • Android TextView文本控件介紹

    Android TextView文本控件介紹

    大家好,本篇文章主要講的是Android TextView文本控件介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Kotlin協(xié)程概念原理與使用萬字梳理

    Kotlin協(xié)程概念原理與使用萬字梳理

    協(xié)程的作用是什么?協(xié)程是一種輕量級(jí)的線程,解決異步編程的復(fù)雜性,異步的代碼使用協(xié)程可以用順序進(jìn)行表達(dá),文中通過示例代碼介紹詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-08-08
  • Android實(shí)現(xiàn)朋友圈多圖顯示功能

    Android實(shí)現(xiàn)朋友圈多圖顯示功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)朋友圈多圖顯示功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android 開發(fā)實(shí)例簡單涂鴉板

    Android 開發(fā)實(shí)例簡單涂鴉板

    本文主要介紹 Android 簡單涂鴉板,這里提供了代碼示例和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下
    2016-08-08

最新評(píng)論

饶阳县| 湘西| 扶绥县| 聂荣县| 濮阳县| 托克托县| 耿马| 社旗县| 翁牛特旗| 灌云县| 方城县| 石棉县| 登封市| 江陵县| 鄂伦春自治旗| 永胜县| 余干县| 洪洞县| 寻甸| 平乐县| 梁平县| 新晃| 石门县| 肃南| 古田县| 尼木县| 手机| 扎囊县| 上虞市| 津市市| 栾川县| 四川省| 大竹县| 石泉县| 平乐县| 大理市| 二连浩特市| 永丰县| 德阳市| 文成县| 司法|