来自URL的Android JSON Parse,我被困在上面

时间:2016-06-20 17:05:54

标签: android json parsing url

您好,我的网址为http://test.muhabirce.de/app/home。这受用户名和密码保护。我想将json解析为我的RecyclerView和CardView。但我失败了。我要分享我的代码。请帮我。感谢。

  

这是我的模特课

public class NewsModel {
String title;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

  

这是我的VolleyRequest课程

public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;

private CustomVolleyRequest(Context context) {
    this.context = context;
    this.requestQueue = getRequestQueue();

    ImageLoader.ImageCache imageCache=new BitmapLruCache();

    imageLoader=new ImageLoader(requestQueue, imageCache);

    /** imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
     private final LruCache<String, Bitmap>
     cache = new LruCache<>(20);

     @Override
     public Bitmap getBitmap(String url) {
     return cache.get(url);
     }

     @Override
     public void putBitmap(String url, Bitmap bitmap) {
     cache.put(url, bitmap);
     }
     });*/
}

public static synchronized CustomVolleyRequest getInstance(Context context) {
    if (customVolleyRequest == null) {
        customVolleyRequest = new CustomVolleyRequest(context);
    }
    return customVolleyRequest;
}

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        requestQueue = new RequestQueue(cache, network);
        requestQueue.start();
    }
    return requestQueue;
}

public ImageLoader getImageLoader() {
    return imageLoader;
}

public static class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {

    public BitmapLruCache() {
        this(getDefaultLruCacheSize());
    }

    public BitmapLruCache(int maxSize) {
        super(maxSize);
    }

    @Override
    public Bitmap getBitmap(String url) {
        return get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight() / 1024;
    }

    public static int getDefaultLruCacheSize() {
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 2014);
        final int cacheSize = maxMemory / 8;

        return cacheSize;
    }
}

}

  

我的活动课

  private JsonArrayRequest getDataFromServer() {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            parseData(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), "Hata", Toast.LENGTH_LONG).show();
        }
    });

    return jsonArrayRequest;
}

private void parseData(JSONArray array) {

    try {


        array=jsonObject.getJSONArray("nodes");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < array.length(); i++) {
        NewsModel newsModel = new NewsModel();


        try {


            jsonObject = array.getJSONObject(i);

            newsModel.setTitle(jsonObject.getString(Config.TAG_TITLE));

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

        newsModels.add(newsModel);
        mAdapter.notifyDataSetChanged();
    }
}

private void getData() {
    requestQueue.add(getDataFromServer());
}

} enter image description here

0 个答案:

没有答案