Unfortunately app has stopped
CODE
public class MainActivity extends AppCompatActivity{
ArrayList<Movie_setget> arrlist = null;
ListView _lvlist = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_lvlist = (ListView)findViewById(R.id.lvlist);
Showdata obj = new Showdata();
obj .execute();
}
public class Showdata extends AsyncTask<String , Void, Long>
{
@Override
protected Long doInBackground(String... params)
{
JsonParser jp = new JsonParser();
JSONObject jobj = jp.getJsonFromUrl("https://api.myjson.com/bins/4xdbu");
arrlist = new ArrayList<Movie_setget>();
try
{
JSONArray jarr = jobj.getJSONArray("Superhero");
for(int i=0; i<jarr.length(); i++)
{
JSONObject m = jarr.getJSONObject(i);
String name = m.getString("name");
String image = m.getString("img");
Movie_setget obj = new Movie_setget();
obj.setName(name);
obj.setImg(image);
arrlist.add(obj);
}
}
catch(JSONException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Long result)
{
_lvlist.setAdapter(new CustomerAdapter());
super.onPostExecute(result);
}
}
public class CustomerAdapter extends BaseAdapter
{
@Override
public int getCount()
{
return arrlist.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inf = getLayoutInflater();
View v =inf.inflate(R.layout.rowdetails, parent, false);
TextView _tvname = (TextView)v.findViewById(R.id.tvname);
//ImageView _ivimg = (ImageView)v.findViewById(R.id.ivimg);
Movie_setget m = new Movie_setget();
m = arrlist.get(position);
_tvname.setText(m.getName());
ImageLoaderTask task = new ImageLoaderTask();
try
{
Bitmap image = task.execute(m.getImg()).get();
_ivimg.setImageBitmap(image);
}
catch(Exception e)
{
e.printStackTrace();
}
return v;
}
}
public class ImageLoaderTask extends AsyncTask<String, Void, Bitmap>
{
@Override
protected Bitmap doInBackground(String... params)
{
try
{
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
}
and logcat
06-03 16:59:26.570 9720-9757/com.example.abhijitroy.superhero E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.abhijitroy.superhero, PID: 9720 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:304) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference at com.example.abhijitroy.superhero.MainActivity$Showdata.d oInBackground(MainActivity.java:54) at com.example.abhijitroy.superhero.MainActivity$Showdata.doInBackground(MainActivity.java:43) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
Your json object is null thats why it is giving you that error
@Override
protected Long doInBackground(String... params)
{
JsonParser jp = new JsonParser();
JSONObject jobj = jp.getJsonFromUrl("https://api.myjson.com/bins/4xdbu");
arrlist = new ArrayList<Movie_setget>();
try
{
if(jobj!=null){
JSONArray jarr = jobj.getJSONArray("Superhero");
if(jarr!=null){
for(int i=0; i<jarr.length(); i++)
{
JSONObject m = jarr.getJSONObject(i);
String name = m.getString("name");
String image = m.getString("img");
Movie_setget obj = new Movie_setget();
obj.setName(name);
obj.setImg(image);
arrlist.add(obj);
}
}
}
}
catch(JSONException e)
{
e.printStackTrace();
}
return null;
}
try this code i have add the if statement for both jsonobject and for jsonarray for checking either it is not null.
Hello PLese use Volly for json parsing
StringRequest strReq = new StringRequest(Method.GET,
"https://api.myjson.com/bins/4xdbu", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
// msgResponse.setText(response.toString());
// hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
// hideProgressDialog();
}
});
Here i get response perfectly
StringRequest Class
public class StringRequest extends Request<String> {
private final Listener<String> mListener;
/**
* Creates a new request with the given method.
*
* @param method the request {@link Method} to use
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
/**
* Creates a new GET request.
*
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
its perfect give me this log
D/StringRequestActivity: {"Superhero":[{"name":"Batman vs Superman","img":"http://cdn1-www.comingsoon.net/assets/uploads/gallery/batman-v-superman-dawn-of-justice-1400694960/tb9umjs-1.jpg"},{"name":"CivilWar","img":"http://screenrant.com/wp-content/uploads/captain-america-civil-war-trailer-2-views.jpg"},{"name":"WonderWoman","img":"http://cdn2-www.comingsoon.net/assets/uploads/2016/03/wonder-woman-header5.jpg"}]}
链接地址: http://www.djcxy.com/p/78422.html
上一篇: AsyncTask中的getDefaultSharedPreferences(getActivity())
下一篇: 不幸的是,应用已停止