📅  最后修改于: 2020-10-11 05:32:39             🧑  作者: Mango
在本教程中,我们将使用Volley库从URL获取JSON数据。 Volley是一个HTTP库,它为我们应用程序的网络连接提供了便利。
使用Volley库的优点如下:
在此示例中,我们将使用Volley库从URL加载JSON数据。 JSON数据包含教程的字符串“名称”,字符串“ imageurl”和字符串“ description”。从URL提取数据后,它们将显示在ListView中。您可以在https://www.javatpoint.com/android-json-parsing-tutorial上参考有关JSON解析教程的更多信息。
让我们创建一个包含以下信息的JSON数据。
URL处的JSON数据(提供您的信息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数据如下所示:
在布局中创建一个activity_main.xml并添加以下代码。
现在,在布局目录中创建一个list_item.xml文件,其中包含ListView的行项目。它包含一个用于图像显示的ImageView和两个用于文本显示的TextView。
创建一个名称为Tutorial.java且信息为字符串“ name”,字符串“ imageurl”和字符串“ description”的数据模型类。
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;
}
}
创建一个名为MyAdapter.java的自定义适配器类,并扩展ArrayAdapter来处理自定义ListView。在此类中,我们将匿名类称为new ImageDownloaderTask(holder.imageView).execute(imageUrl)以从URL下载图像。
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文件中添加以下库文件。
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'
}
创建扩展AsyncTask <>的匿名类ImageDownloaderTask.java。此类在执行doInbackground()方法时从URL下载(或获取)图像,并将结果(位图)返回到onPostExecute()。在onPostExecute()方法中,位图设置为ImageView。
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数据。
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权限
输出: