JSON Question

JSON Displaying error

Trying to display json on screen

Here is my MainActivity code:

package com.example.nemes.myapplication;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private RecyclerView mRVFishPrice;
private AdapterFish mAdapter;

protected void onCreate(Bundle savedInstanceState) {
//Make call to AsyncTask
new AsyncFetch().execute();

private class AsyncFetch extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
HttpURLConnection conn;
URL url = null;

protected void onPreExecute() {

//this method will be running on UI thread


protected String doInBackground(String... params) {
try {

// Enter URL address where your json file resides
// Even you can make call to php file which returns json data
url = new URL(" http://api.learn2crack.com/android/jsonandroid");

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
return e.toString();
try {

// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();

// setDoOutput to true as we recieve data from json file

} catch (IOException e1) {
// TODO Auto-generated catch block
return e1.toString();

try {

int response_code = conn.getResponseCode();

// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {

// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {

// Pass data to onPostExecute method
return (result.toString());

} else {

return ("unsuccessful");

} catch (IOException e) {
return e.toString();
} finally {


protected void onPostExecute(String result) {

//this method will be running on UI thread

List<DataFish> data=new ArrayList<>();

try {

JSONArray jArray = new JSONArray(result);

// Extract data from json and store into ArrayList as class objects
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
DataFish fishData = new DataFish();
//fishData.fishImage= json_data.getString("fish_img");
fishData.fishName= json_data.getString("name");
// fishData.catName= json_data.getString("cat_name");
// fishData.sizeName= json_data.getString("size_name");
//fishData.price= json_data.getInt("price");

// Setup and Handover data to recyclerview
mRVFishPrice = (RecyclerView)findViewById(R.id.fishPriceList);
mAdapter = new AdapterFish(MainActivity.this, data);
mRVFishPrice.setLayoutManager(new LinearLayoutManager(MainActivity.this));

} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();



Here is Adapter code

import android.content.Context;
import android.support.v4.content.ContextCompat;
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 com.bumptech.glide.Glide;
import java.util.Collections;
import java.util.List;

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

private Context context;
private LayoutInflater inflater;
List<DataFish> data= Collections.emptyList();
DataFish current;
int currentPos=0;

// create constructor to innitilize context and data sent from MainActivity
public AdapterFish(Context context, List<DataFish> data){
inflater= LayoutInflater.from(context);

// Inflate the layout when viewholder created
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container_fish, parent,false);
MyHolder holder=new MyHolder(view);
return holder;

// Bind data
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

// Get current position of item in recyclerview to bind data and assign values from list
MyHolder myHolder= (MyHolder) holder;
DataFish current=data.get(position);
myHolder.textSize.setText("Size: " + current.sizeName);
myHolder.textType.setText("Category: " + current.catName);
myHolder.textPrice.setText("Rs. " + current.price + "\\Kg");
myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

// load image into imageview using glide
Glide.with(context).load("" + current.fishImage)


// return total item from List
public int getItemCount() {
return data.size();

class MyHolder extends RecyclerView.ViewHolder{

TextView textFishName;
ImageView ivFish;
TextView textSize;
TextView textType;
TextView textPrice;

// create constructor to get widget reference
public MyHolder(View itemView) {
textFishName= (TextView) itemView.findViewById(R.id.textFishName);
ivFish= (ImageView) itemView.findViewById(R.id.ivFish);
textSize = (TextView) itemView.findViewById(R.id.textSize);
textType = (TextView) itemView.findViewById(R.id.textType);
textPrice = (TextView) itemView.findViewById(R.id.textPrice);



And here is DataFIsh code:

package com.example.nemes.myapplication;

* Created by nemes on 9/4/2016.

public class DataFish {

public String fishImage;
public String fishName;
public String catName;
public String sizeName;
public int price;

When I launch application, I have popup with JSON and fields on screen are not filled.


Where is error?

Thank for help.


You are receiving JSONObject and you are parsing it using JSONArray

{"field" : "hello"} is jsonObject and

[{"field" : "hello"}, {"field" : "hello"}] is jsonArray

so you should do it as follows

JSONObject jsonObject = new JSONObject(result);
JSONArray jArray = jsonObject.getJSONArray("android");