亚洲国产爱久久全部精品_日韩有码在线播放_国产欧美在线观看_中文字幕不卡在线观看

Android 開發(fā)者從URL獲取數(shù)據(jù)的Volley庫含代碼

Android 開發(fā)者從URL獲取數(shù)據(jù)的Volley庫

在本教程中,我們將使用Volley庫從URL獲取JSON數(shù)據(jù)。愛掏網(wǎng) - it200.comVolley是一個HTTP庫,為我們的應(yīng)用程序提供網(wǎng)絡(luò)連接的功能。愛掏網(wǎng) - it200.com

使用Volley庫的優(yōu)點如下:

  • 更舒適和更快的請求管理。愛掏網(wǎng) - it200.com
  • 它提供高效的網(wǎng)絡(luò)管理。愛掏網(wǎng) - it200.com

在這個示例中,我們將使用Volley庫從URL加載JSON數(shù)據(jù)。愛掏網(wǎng) - it200.comJSON數(shù)據(jù)包含教程的字符串 “name” 、字符串 “imageurl” 和字符串 “description”愛掏網(wǎng) - it200.com在從URL獲取數(shù)據(jù)后,它們將在ListView中顯示。愛掏網(wǎng) - it200.com您可以在以下鏈接中了解更多關(guān)于JSON解析教程的信息: https://www.javatpoint.com/android-json-parsing-tutorial 。愛掏網(wǎng) - it200.com

讓我們創(chuàng)建包含以下信息的JSON數(shù)據(jù)。愛掏網(wǎng) - it200.com

URL上的JSON數(shù)據(jù)(提供您的信息URL)= http://192.168.1.35:8080/jsondata/

{
"tutorials":[
{
"name":"Java",
"imageurl":"https://www.javatpoint.com/images/logo/javahome.png",
"description":"Java is a high level, robust, object-oriented and secure programming language."
},
{
"name":"Android",
"imageurl":"https://www.javatpoint.com/images/logo/androidhome.png",
"description":"Android is a complete set of software for mobile devices."
},
{
"name":"Python",
"imageurl":"https://www.javatpoint.com/images/logo/pythonhome.png",
"description":"Python is a general purpose, dynamic, high level and interpreted programming language."
},
{
"name":"C++",
"imageurl":"https://www.javatpoint.com/cpp/images/cpp-home.png",
"description":"C++ is an object-oriented programming language."
}
]
}

JSON數(shù)據(jù)的樣式如下:

在layout中創(chuàng)建一個 activity_main.xml 文件,并添加以下代碼。愛掏網(wǎng) - it200.com

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="example.javatpoint.com.volleyreadimagejson.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="292dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

現(xiàn)在,在布局目錄中創(chuàng)建一個 list_item.xml 文件,其中包含ListView的行項目。愛掏網(wǎng) - it200.com這個文件包含一個ImageView用于顯示圖像,以及兩個TextView用于顯示文本。愛掏網(wǎng) - it200.com

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="https://deepinout.com/android/android-top-articles/@drawable/placeholder"
            android:contentDescription="image view" />
        <TextView
            android:id="@+id/textViewName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="title"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
    </LinearLayout>


    <TextView
        android:id="@+id/textViewImageUrl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="url"
        android:autoLink="web"
        android:textColor="#08308e" />


</LinearLayout>

創(chuàng)建一個名為 Tutorial.java 的數(shù)據(jù)模型類,并包含字符串”name”、字符串”imageurl”和字符串”description”的信息。愛掏網(wǎng) - it200.com

Tutorial.java

package example.javatpoint.com.volleyreadimagejson;

public class Tutorial {
    String name, imageUrl, description;

    public Tutorial(String name, String imageUrl, String description) {
        this.name = name;
        this.imageUrl = imageUrl;
        this.description = description;
    }
    public String getName() {
        return name;
    }
    public String getImageUrl() {
        return imageUrl;
    }
    public String getDescription() {
        return description;
    }
}

創(chuàng)建一個名為 MyAdapter.java 的自定義適配器類,并擴展ArrayAdapter以處理自定義ListView。愛掏網(wǎng) - it200.com在這個類中,我們調(diào)用一個匿名類 new ImageDownloaderTask(holder.imageView).execute(imageUrl) 來從URL下載圖片。愛掏網(wǎng) - it200.com

MyAdapter.java

package example.javatpoint.com.volleyreadimagejson;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;


public class MyAdapter extends ArrayAdapter {
    //the tutorial list that will be displayed
    private List tutorialList;
    private Bitmap bitmap;
    private Context mCtx;
    //here we are getting the tutoriallist and context
    //so while creating the object of this adapter class we need to give tutoriallist and context
    public MyAdapter(List tutorialList, Context mCtx) {
        super(mCtx, R.layout.list_item, tutorialList);
        this.tutorialList = tutorialList;
        this.mCtx = mCtx;
    }

    //this method will return the list item
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //getting the layoutinflater
        ViewHolder holder;
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        convertView = inflater.inflate(R.layout.list_item, null, true);
        holder = new ViewHolder();
        //getting text views
        holder.textViewName = convertView.findViewById(R.id.textViewName);
        holder.textDescription = convertView.findViewById(R.id.textViewImageUrl);
        holder.imageView = convertView.findViewById(R.id.imageView);

        convertView.setTag(holder);
        //Getting the tutorial for the specified position
        Tutorial tutorial = tutorialList.get(position);
        String imageUrl = tutorial.getImageUrl();
        String tutorialDescription = tutorial.getDescription();
        String tutorialTitle = tutorial.getName();

        holder.textViewName.setText(tutorialTitle);
        holder.textDescription.setText(tutorialDescription);

        if (holder.imageView != null) {
            /*-------------fatching image------------*/;
            new ImageDownloaderTask(holder.imageView).execute(imageUrl);
        }
        holder.imageView.setImageBitmap(bitmap);
        return convertView;
    }
    static class ViewHolder {
        TextView textViewName;
        TextView textDescription;
        ImageView imageView;
    }
}

build.gradle 文件中添加以下庫文件。愛掏網(wǎng) - it200.com

build.gradle

android {
    useLibrary 'org.apache.http.legacy'
}

dependencies {
compile 'com.android.volley:volley:1.0.0'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
}

創(chuàng)建一個匿名類 ImageDownloaderTask.java ,它繼承自AsyncTask<>。愛掏網(wǎng) - it200.com該類在執(zhí)行doInBackground()方法時從URL下載(或提取)圖像,并將結(jié)果(位圖)返回給onPostExecute()方法。愛掏網(wǎng) - it200.com在onPostExecute()方法中,將位圖設(shè)置到ImageView中。愛掏網(wǎng) - it200.com

ImageDownloaderTask.java

package example.javatpoint.com.volleyreadimagejson;

import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpStatus;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class ImageDownloaderTask extends AsyncTask {

    private final WeakReference imageViewReference;
    public ImageDownloaderTask(ImageView imageView) {
        imageViewReference = new WeakReference(imageView);
    }
    @Override
    protected Bitmap doInBackground(String... params) {
        return downloadBitmap(params[0]);
    }
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }
        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                } else {
                    Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.placeholder);
                    imageView.setImageDrawable(placeholder);
                }
            }
        }
    }

    private Bitmap downloadBitmap(String imageUrl) {
        HttpURLConnection urlConnection = null;
        try {
            URL uri = new URL(imageUrl);
            urlConnection = (HttpURLConnection) uri.openConnection();

            int statusCode = urlConnection.getResponseCode();
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }
            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        }
        catch (Exception e) {
            urlConnection.disconnect();
            Log.w("ImageDownloader", "Error downloading image from " + imageUrl);
        }
        finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }
}

MainActivity.java 類中,我們使用Volley從URL獲取和解析JSON數(shù)據(jù)。愛掏網(wǎng) - it200.com

MainActivity.java

package example.javatpoint.com.volleyreadimagejson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    private static final String JSON_URL = "http://192.168.1.35:8080/jsondata/";
    //the tutorial list where we will store all the tutorial objects after parsing json
    List tutorialList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);
        tutorialList = new ArrayList<>();
        //this method will fetch and parse the data
        loadTutorialList();
    }

    private void loadTutorialList() {
        //getting the progressbar
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setVisibility(View.VISIBLE);

        //creating a string request to send request to the url
        StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
                new Response.Listener() {
                    @Override
                    public void onResponse(String response) {
                        //hiding the progressbar after completion
                        progressBar.setVisibility(View.INVISIBLE);


                        try {
                            //getting the whole json object from the response
                            JSONObject obj = new JSONObject(response);

                            //we have the array named tutorial inside the object
                            //so here we are getting that json array
                            JSONArray tutorialsArray = obj.getJSONArray("tutorials");

                            //now looping through all the elements of the json array
                            for (int i = 0; i < tutorialsArray.length(); i++) {
                                //getting the json object of the particular index inside the array
                                JSONObject tutorialsObject = tutorialsArray.getJSONObject(i);

                                //creating a tutorial object and giving them the values from json object
                                Tutorial tutorial = new Tutorial(tutorialsObject.getString("name"), tutorialsObject.getString("imageurl"),tutorialsObject.getString("description"));

                                //adding the tutorial to tutoriallist
                                tutorialList.add(tutorial);
                            }

                            //creating custom adapter object
                            MyAdapter adapter = new MyAdapter(tutorialList, getApplicationContext());

                            //adding the adapter to listview
                            listView.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //displaying the error in toast if occur
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

        //creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //adding the string request to request queue
        requestQueue.add(stringRequest);
    }
}

在AndroidManifest.xml中添加Internet權(quán)限

AndroidManifest.xml

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

輸出:

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

亚洲国产爱久久全部精品_日韩有码在线播放_国产欧美在线观看_中文字幕不卡在线观看

    
    

    久久精品国产一区二区三区| 国产日韩亚洲欧美综合| 亚洲欧美电影院| 亚洲国产精品欧美一二99| 国产欧美91| 欧美亚一区二区| 欧美成人精品在线播放| 久久超碰97中文字幕| 一级日韩一区在线观看| 最新高清无码专区| 狠狠色狠狠色综合| 国产亚洲精品美女| 国产嫩草一区二区三区在线观看| 欧美日韩国产a| 欧美国产在线观看| 欧美xx视频| 欧美大片一区二区| 欧美高清成人| 欧美福利影院| 欧美成人激情在线| 免费一级欧美片在线播放| 久久精品国产成人| 久久精品国产第一区二区三区| 午夜精品久久久久久久蜜桃app | 小处雏高清一区二区三区| 亚洲天堂成人| 亚洲午夜激情网站| 亚洲在线日韩| 午夜精品www| 欧美呦呦网站| 久久久久久久高潮| 麻豆精品网站| 欧美另类69精品久久久久9999| 免费成人av在线| 欧美激情在线观看| 欧美日韩中国免费专区在线看| 欧美三级韩国三级日本三斤| 欧美三级不卡| 国产一区二区三区四区hd| 在线观看亚洲一区| 亚洲精品一区在线观看| 亚洲无人区一区| 欧美亚洲日本一区| 老司机精品久久| 欧美—级a级欧美特级ar全黄| 欧美日韩的一区二区| 国产精品久久午夜| 一区二区在线视频播放| 亚洲精品婷婷| 久久黄金**| 欧美日韩国产小视频在线观看| 国产精品久久久久av免费| 国产一区二区三区四区在线观看| 国产字幕视频一区二区| 日韩一级精品| 久久九九久精品国产免费直播| 免费成人性网站| 国产精品免费一区二区三区在线观看| 国内视频一区| 亚洲永久视频| 欧美成人在线免费观看| 国产欧美日韩伦理| 亚洲美女视频网| 久久久国产亚洲精品| 国产精品a久久久久久| 在线 亚洲欧美在线综合一区| 一区二区三区精品在线| 狼人天天伊人久久| 国产精品一区久久久久| 99精品热视频| 欧美成人精品在线| 国产一区亚洲| 性欧美18~19sex高清播放| 男人的天堂成人在线| 国产亚洲精品资源在线26u| 妖精视频成人观看www| 麻豆国产精品va在线观看不卡| 国产精品永久免费| 在线中文字幕一区| 欧美黑人国产人伦爽爽爽| 在线观看日韩欧美| 久久精品成人| 国产精品一区二区三区四区 | 欧美日韩美女一区二区| 亚洲国产99精品国自产| 久久久久久久综合狠狠综合| 国产日韩视频一区二区三区| 亚洲视频免费| 国产精品va在线播放| 在线亚洲一区二区| 欧美日韩日本国产亚洲在线| 91久久精品网| 欧美激情一二区| 91久久国产综合久久| 欧美岛国激情| 亚洲精品国产拍免费91在线| 欧美高清在线一区二区| 亚洲人成高清| 欧美视频国产精品| 亚洲视频在线一区| 国产精品一区二区在线观看网站| 亚洲一区二区三区色| 国产精品午夜春色av| 欧美与欧洲交xxxx免费观看| 国产亚洲成精品久久| 久久一区二区三区超碰国产精品| 亚洲福利在线观看| 欧美久久99| 一区二区三区色| 国产精品日韩在线| 久久精品伊人| 亚洲日本中文字幕免费在线不卡| 欧美精品久久一区| 亚洲欧美日韩成人| 一区在线电影| 欧美视频一区二区三区| 久久国产加勒比精品无码| 亚洲国产成人av| 欧美偷拍另类| 久久一区中文字幕| 亚洲狼人综合| 国产午夜精品久久久久久免费视| 久久综合五月| 亚洲一区二区三区国产| 激情久久五月| 欧美午夜在线视频| 久久婷婷一区| 亚洲伊人网站| 亚洲韩国精品一区| 国产精品一二| 欧美理论在线播放| 亚洲国产欧美一区二区三区久久 | 欧美精品激情在线| 久久精品国产综合| 亚洲深夜激情| 亚洲国产一区二区三区a毛片| 国产精品日韩久久久久| 免费亚洲婷婷| 午夜精品婷婷| aa亚洲婷婷| 亚洲夫妻自拍| 黑人操亚洲美女惩罚| 欧美视频精品在线| 久久综合精品一区| 久久精品99| 欧美亚洲在线视频| 亚洲夜间福利| 99视频在线精品国自产拍免费观看 | 久久精品视频网| 亚洲一区二区黄| 日韩一级黄色大片| 亚洲精品在线一区二区| 亚洲成人在线免费| 狠狠色综合网| 国产综合久久| 国内精品久久久久久影视8| 国产精品视频xxx| 国产精品久久久久aaaa九色| 欧美日韩精品一区二区在线播放| 老牛嫩草一区二区三区日本| 久久九九99| 久久久久久穴| 久久亚洲电影| 免费观看30秒视频久久| 免费成人av在线| 麻豆国产精品777777在线| 久久夜色精品国产欧美乱| 开元免费观看欧美电视剧网站| 久久国产乱子精品免费女| 欧美一区二区三区在线免费观看| 亚洲欧美日韩精品综合在线观看| 一区二区不卡在线视频 午夜欧美不卡在 | 国产日本亚洲高清| 国产综合久久久久久| 国产一区二区三区四区五区美女| 国产中文一区二区| 在线精品观看| 亚洲精品女av网站| 亚洲性夜色噜噜噜7777| 欧美一区二区性| 久久婷婷色综合| 欧美国产亚洲视频| 欧美日韩二区三区| 国产精品一区二区在线观看网站| 国产农村妇女精品一区二区| 国产一区香蕉久久| 亚洲国产综合在线| 中文一区二区| 久久久99爱| 欧美人与禽猛交乱配| 国产精品毛片va一区二区三区| 国产精品亚洲一区| 在线播放豆国产99亚洲| 一区二区三区免费在线观看| 欧美一区二区视频97| 欧美成熟视频| 国产日本亚洲高清| 亚洲精品日产精品乱码不卡| 午夜在线精品偷拍| 欧美精品激情|