Android - org.json.JSONException No Value for

0

During execution of the application I received a JSONException error No Value for

{
"time": "05:07:18 AM",
"milliseconds_since_epoch": 1504588038515,
"date": "09-05-2017"
}

I know there are many bugs reported, but everyone I read did not get results. The codes below follow.

Main Activity:

package com.example.joo.myapplication;

import android.app.ProgressDialog;
import android.support.annotation.NonNull;
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 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.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MainActivity extends AppCompatActivity {
private static final String URL_DATA = "http://date.jsontest.com";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    listItems = new ArrayList<>();
    loadRecyclerViewData();
}

private void loadRecyclerViewData(){

    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try{
                JSONObject jsonObject = new JSONObject(response);
                JSONArray array = jsonObject.getJSONArray(response);

                    JSONObject o = array.getJSONObject(0);
                    ListItem item = new ListItem(
                      o.optString("time"),
                            o.optString("milliseconds_since_epoch"),
                            o.optString("date")
                    );
                    listItems.add(item);


                adapter = new MyAdapter(getApplicationContext(),listItems);
                recyclerView.setAdapter(adapter);

            }catch (JSONException e){
                e.printStackTrace();
                Toast.makeText(MainActivity.this, e.toString(),Toast.LENGTH_LONG).show();
            }
        }
    },
    new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError volleyError){
            Toast.makeText(MainActivity.this, volleyError.toString(),Toast.LENGTH_LONG).show();
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

}

List Item:

package com.example.joo.myapplication;



public class ListItem {
public String time, milliseconds_since_epoch, date;

public ListItem(String time, String milliseconds_since_epoch, String date) {
    this.time = time;
    this.milliseconds_since_epoch = milliseconds_since_epoch;
    this.date = date;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getMilliseconds_since_epoch() {
    return milliseconds_since_epoch;
}

public void setMilliseconds_since_epoch(String milliseconds_since_epoch) {
    this.milliseconds_since_epoch = milliseconds_since_epoch;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}
}

My Adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private Context context;
private List<ListItem> items;

public MyAdapter(Context context, List<ListItem> items) {
    this.context = context;
    this.items = items;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
    holder.date.setText(items.get(position).getTime());
    holder.time.setText(items.get(position).getMilliseconds_since_epoch());
    holder.mili.setText(items.get(position).getDate());
}

@Override
public int getItemCount() {
    return items.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView date, time, mili;

    public ViewHolder(View itemView) {
        super(itemView);
        date = (TextView) itemView.findViewById(R.id.ViewDesc);
        time = (TextView) itemView.findViewById(R.id.ViewHead);
        mili = (TextView) itemView.findViewById(R.id.ViewImg);

    }
    }
    }
    
asked by anonymous 05.09.2017 / 07:35

1 answer

0
ListItem item = new ListItem(
                      o.optString("time"),
                      String.valueOf(o.optLong("milliseconds_since_epoch")),
                      o.optString("date"));
    
05.09.2017 / 15:37