无法解析json并将其设置为recyclerview

时间:2016-11-03 12:04:17

标签: java android json android-studio

您好我正在学习Android应用开发。为此,我想让自己成为一个简单的壁纸应用程序。因此,我写了一些粗略的内容,在这里提出。我想从json获取壁纸网址。不幸的是,我无法从我的服务器获取数据。 java.lang.NullPointerException: Attempt to read from null array 如何从jsonParse asynctask中正确获取数据? 我整天都被困在这里。这里可能出了什么问题? 这是我的代码:

myjson.json:

{
    "walls":[
                   {"ourUrl":"http://www.hdbloggers.net/wp-content/uploads/2016/01/Wallpapers-for-Android.jpg"},
                   {"ourUrl":"http://androidwallpape.rs/content/02-wallpapers/131-night-sky/wallpaper-2707591.jpg"},
                   {"ourUrl":"http://androidwallpape.rs/content/02-wallpapers/155-starrynight/starry-night-sky-star-galaxy-space-dark-9-wallpaper.jpg"}
            ]
}

MainActivity.java:

package regalstreak.me.wallpapers;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends Activity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new RecyclerAdapter(this);
        recyclerView.setAdapter(adapter);

    }

}

RecyclerAdapter.java:

package regalstreak.me.wallpapers;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

// This is a recycleradapter which will set the correct images to the correct position in the recyclerview.

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private Context myCtx1;
    String[] arr;
    String[] arrurl;
    String jsonURL = "http://dev.regalstreak.me/myjson.json";

    public RecyclerAdapter(Context ctx) {
        this.myCtx1 = ctx;
    }

    public ImageView Image;

    private String[] mText = {
            "Text 1",
            "Text 2",
            "Text 3"
    };

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView Text;

        public ViewHolder(View itemView) {
            super(itemView);

            Image = (ImageView) itemView.findViewById(R.id.image_view);
            Text = (TextView) itemView.findViewById(R.id.text_view);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.wallpapers_list, viewGroup, false);

        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.Text.setText(mText[i]);
        new jsonParse().execute();
        new DownloadImageTask(Image).execute(arrurl[i]);
    }

    @Override
    public int getItemCount() {
        return mText.length;
    }

    class jsonParse extends AsyncTask<String, Void, String[]> {

        protected String[] doInBackground(String[] urls) {
            String myText = null;
            String url = urls[0];
            String ourUrl;

            try {
                InputStream in = new java.net.URL(jsonURL).openStream();
                myText = IOUtils.toString(in, "utf-8");
                in.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
                // Parse the json
                List<String> allUrls = new ArrayList<String>();

                JSONObject jsonObjectRoot = new JSONObject(myText);
                JSONArray jsonArray = jsonObjectRoot.getJSONArray("walls");

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    ourUrl = jsonObject.getString("ourUrl");
                    allUrls.add(ourUrl);
                }

                arr = allUrls.toArray(new String[allUrls.size()]);

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return arr;
        }

        protected void onPostExecute(String[] result){
            arrurl = result;
        }
    }

}

DownloadImageTask.java:

package regalstreak.me.wallpapers;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import java.io.InputStream;

// Here, we will download the wallpapers obtained from jsonData with an asynctask.

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap>{

    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage){
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
            in.close();
        } catch (Exception e) {
            Log.e("Error getting images.", e.getMessage());
            e.printStackTrace();
        }

        return mIcon11;
    }

    protected void onPostExecute(Bitmap result){
        bmImage.setImageBitmap(result);
    }

}

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"
    tools:context="regalstreak.me.wallpapers.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view" />

</RelativeLayout>

wallpaper_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="150dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/image_view"
        android:alpha="0.6"
        android:background="@color/colorDivider"
        android:padding="9dp">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:textColor="@color/colorPrimaryText" />

    </RelativeLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

我在这里使用了HttpURLConnection类来快速响应和缓存等功能。从URL接收的数据被添加到输入流中,然后我们将其转换为String构建器以获取字符串对象,我们可以将其与JSON类一起使用。

PS - 将AsyncTask代码添加到您的MainActivity本身,不要为此创建单独的Java文件。

提示 - 始终使用此工具验证json - jsonlint.com

  

MainActivity

/*

your code

*/

@Override
protected void onCreate(Bundle savedInstanceState) {

        new MyAsyncTask().execute("");

}

class MyAsyncTask extends AsyncTask<String, String, Void> {

    private ProgressDialog progressDialog = new ProgressDialog(StartScreen.this);
    InputStream inputStream = null;
    String result = "";
    ArrayList<String> list;

    protected void onPreExecute() {
        progressDialog.setTitle("Downloading JSON Data");
        progressDialog.show();
        // above code makes a dialog with a progress bar
    }

    @Override
    protected Void doInBackground(String... params) {
        ArrayList<String> param = new ArrayList<String>();
        URL url, url2;

        try{
            url = new URL("http://dev.regalstreak.me/myjson.json");
            // link to your json file
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setUseCaches(false);
            inputStream = new BufferedInputStream(urlConnection.getInputStream());

        }catch (MalformedURLException malle){
            Log.e("Mal", ""+malle);
            malle.printStackTrace();
        }catch (IOException ioe){
            Log.e("IO", ""+ioe);
            ioe.printStackTrace();
        }

        // Convert response to string using String Builder
        try {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
            StringBuilder sBuilder = new StringBuilder();

            String line = null;
            while ((line = bReader.readLine()) != null) {
                sBuilder.append(line + "\n");
            }

            inputStream.close();
            result = sBuilder.toString();

        } catch (Exception e) {
            Log.e("StringBuilding", "Error converting result " + e.toString());
        }
        return null;
    }

    protected void onPostExecute(Void v) {

        //parse JSON data
        try {
            JSONObject jobj = new JSONObject(result);
            //Taking a JSON Array from the JSONObject created above

            String url = jobj.getString("ourUrl");

            // We are adding this string to the ArrayList

            list.add(url);

            progressDialog.dismiss();
            Context con = ListLoader.this.getApplication();
            adapter = new RecyclerAdapter(list,con);

            recyclerView.setAdapter(adapter);

        } catch (JSONException e) {
            Log.e("JSONException", "Error: " + e.toString());
        } // catch (JSONException e)
    }

}


/*

your code

*/

现在要在列表中更有效地显示图像,请使用repo Universal图像加载器。它有很多功能。你可以在这里得到它 - https://github.com/nostra13/Android-Universal-Image-Loader

然后添加这种代码来显示图像。把它放在onBindViewHolder

  

适配器

@Override
public void onBindViewHolder(DataHolder holder, int position) {
    ImageLoaderConfiguration config;
    config = new ImageLoaderConfiguration.Builder(mContext).build();
    ImageLoader.getInstance().init(config);
    imageLoader = ImageLoader.getInstance();

    DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showImageForEmptyUri(R.drawable.ic_error_black_48dp) // displays this image not found
    .showImageOnFail(R.drawable.ic_error_black_48dp) // Displays this on failure
    .showImageOnLoading(R.drawable.white) // Displays while loading
    .cacheInMemory(false)
    .cacheOnDisk(true)
    .build();

    imageLoader.displayImage(list.get(position), holder.imageView, options);    
    // We are feeding the urls here. 
}