我正在尝试使用API从json解析数据,但是我不知道出于什么原因,当我计算m_name3的大小时它显示为0,但正在通过文件解析数据。当祝酒的String name = o.getString(“ name”);它显示了名称字符串的内容,因此这意味着它正在解析数据,但不存储在arraylist中。我不知道为什么请帮忙!
package com.example.amitc.pvrathome;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
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 com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
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.URL;
import java.util.ArrayList;
import okhttp3.OkHttpClient;
public class msms extends AppCompatActivity {
String img_src;
private ArrayList<String> m_name3 = new ArrayList<>();
private ArrayList<String> m_release_year3 = new ArrayList<>();
private ArrayList<String> m_genre3 = new ArrayList<>();
private ArrayList<String> m_director3 = new ArrayList<>();
private ArrayList<String> m_rating3 = new ArrayList<>();
private ArrayList<String> m_actor3 = new ArrayList<>();
private ArrayList<String> m_desc3 = new ArrayList<>();
private ArrayList<String> m_imgsrc3 = new ArrayList<>();
StringRequest stringRequest;
Context mContext;
String url_all = "https://www.dropbox.com/s/ofzv2j16vq9ofmq/100MovieList.json?dl=1";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.msms);
new getIncomingIntent().execute();
ImageView imageView = findViewById(R.id.msms_image);
mContext = this;
Glide.with(this)
.load(img_src)
.apply(new RequestOptions()
.placeholder(R.drawable.rec)
.dontAnimate()
.error(R.drawable.movies))
.into(imageView);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//txt.setText("Searching by: "+ query);
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
String uri = intent.getDataString();
final int index = Integer.parseInt(uri.replaceAll("[^0-9]", ""));
//
stringRequest = new StringRequest(Request.Method.GET,
url_all,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("movies");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
String name = o.getString("name");
String year = o.getString("year");
String genre = o.getString("genre");
String rating = o.getString("rating");
String director = o.getString("directors_name");
String actor = o.getString("cast");
String img_src = o.getString("poster_src");
String desc = o.getString("description");
m_name3.add(name);
m_release_year3.add(year);
m_genre3.add(genre);
m_rating3.add(rating);
m_director3.add(director);
m_actor3.add(actor);
m_imgsrc3.add(img_src);
m_desc3.add(desc);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Some error Occured", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(msms.this);
requestQueue.add(stringRequest);
Toast.makeText(this, "Index: "+ m_name3.size(), Toast.LENGTH_SHORT).show();
// TextView m_name = findViewById(R.id.msms_name);
// m_name.setText(m_name3.get(index));
//
// TextView m_name1 = findViewById(R.id.msms_name1);
// m_name1.setText(m_name3.get(index));
// // TextView m_year = (TextView) findViewById(R.id.msms_year);
// // m_year.setText(year);
//
// TextView m_genre = findViewById(R.id.msms_genre);
// m_genre.setText(m_genre3.get(index));
//
// TextView m_rating = findViewById(R.id.msms_rating);
// m_rating.setText(m_rating3.get(index));
//
// TextView m_desc = findViewById(R.id.msms_desc);
// m_desc.setText(m_desc3.get(index));
//
// TextView m_direc = findViewById(R.id.msms_direc);
// m_direc.setText(m_director3.get(index));
//
// TextView m_actor = findViewById(R.id.msms_actor);
// m_actor.setText(m_actor3.get(index));
// Glide.with(this)
// .load(img_src)
// .apply(new RequestOptions()
// .placeholder(R.drawable.rec)
// .dontAnimate()
// .error(R.drawable.movies))
// .into(imageView);
}
}
public class getIncomingIntent extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
String name = getIntent().getStringExtra("names");
// String year = getIntent().getStringExtra("years");
String genre = getIntent().getStringExtra("genres");
String rating = getIntent().getStringExtra("ratings");
String desc = getIntent().getStringExtra("decss");
String director = getIntent().getStringExtra("direcs");
String actor = getIntent().getStringExtra("stars");
img_src = getIntent().getStringExtra("img1");
TextView m_name = (TextView) findViewById(R.id.msms_name);
m_name.setText(name);
TextView m_name1 = (TextView) findViewById(R.id.msms_name1);
m_name1.setText(name);
// TextView m_year = (TextView) findViewById(R.id.msms_year);
// m_year.setText(year);
TextView m_genre = (TextView) findViewById(R.id.msms_genre);
m_genre.setText(genre);
TextView m_rating = (TextView) findViewById(R.id.msms_rating);
m_rating.setText(rating);
TextView m_desc = (TextView) findViewById(R.id.msms_desc);
m_desc.setText(desc);
TextView m_direc = (TextView) findViewById(R.id.msms_direc);
m_direc.setText(director);
TextView m_actor = (TextView) findViewById(R.id.msms_actor);
m_actor.setText(actor);
return null;
}
}
}
已编辑:
package com.example.amitc.pvrathome;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
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 com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
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.URL;
import java.util.ArrayList;
import okhttp3.OkHttpClient;
public class msms extends AppCompatActivity {
String img_src;
private ArrayList<String> m_name3 = new ArrayList<>();
private ArrayList<String> m_release_year3 = new ArrayList<>();
private ArrayList<String> m_genre3 = new ArrayList<>();
private ArrayList<String> m_director3 = new ArrayList<>();
private ArrayList<String> m_rating3 = new ArrayList<>();
private ArrayList<String> m_actor3 = new ArrayList<>();
private ArrayList<String> m_desc3 = new ArrayList<>();
private ArrayList<String> m_imgsrc3 = new ArrayList<>();
StringRequest stringRequest;
Context mContext;
String url_all = "https://www.dropbox.com/s/ofzv2j16vq9ofmq/100MovieList.json?dl=1";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.msms);
new getIncomingIntent().execute();
ImageView imageView = findViewById(R.id.msms_image);
mContext = this;
Glide.with(this)
.load(img_src)
.apply(new RequestOptions()
.placeholder(R.drawable.rec)
.dontAnimate()
.error(R.drawable.movies))
.into(imageView);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//txt.setText("Searching by: "+ query);
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
String uri = intent.getDataString();
final int index = Integer.parseInt(uri.replaceAll("[^0-9]", ""));
//
stringRequest = new StringRequest(Request.Method.GET,
url_all,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("movies");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
String name = o.getString("name");
String year = o.getString("year");
String genre = o.getString("genre");
String rating = o.getString("rating");
String director = o.getString("directors_name");
String actor = o.getString("cast");
String img_src = o.getString("poster_src");
String desc = o.getString("description");
m_name3.add(name);
m_release_year3.add(year);
m_genre3.add(genre);
m_rating3.add(rating);
m_director3.add(director);
m_actor3.add(actor);
m_imgsrc3.add(img_src);
m_desc3.add(desc);
}
ProgressDialog progress;
progress= new ProgressDialog(mContext);
progress.setMessage("Loading...");
progress.show();
TextView m_name = findViewById(R.id.msms_name);
m_name.setText(m_name3.get(index));
TextView m_name1 = findViewById(R.id.msms_name1);
m_name1.setText(m_name3.get(index));
// TextView m_year = (TextView) findViewById(R.id.msms_year);
// m_year.setText(year);
TextView m_genre = findViewById(R.id.msms_genre);
m_genre.setText(m_genre3.get(index));
TextView m_rating = findViewById(R.id.msms_rating);
m_rating.setText(m_rating3.get(index));
TextView m_desc = findViewById(R.id.msms_desc);
m_desc.setText(m_desc3.get(index));
TextView m_direc = findViewById(R.id.msms_direc);
m_direc.setText(m_director3.get(index));
TextView m_actor = findViewById(R.id.msms_actor);
m_actor.setText(m_actor3.get(index));
ImageView imageView = findViewById(R.id.msms_image);
Glide.with(getApplicationContext())
.load(img_src)
.apply(new RequestOptions()
.placeholder(R.drawable.rec)
.dontAnimate()
.error(R.drawable.movies))
.into(imageView);
progress.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Some error Occured", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(msms.this);
requestQueue.add(stringRequest);
}
}
public class getIncomingIntent extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
String name = getIntent().getStringExtra("names");
// String year = getIntent().getStringExtra("years");
String genre = getIntent().getStringExtra("genres");
String rating = getIntent().getStringExtra("ratings");
String desc = getIntent().getStringExtra("decss");
String director = getIntent().getStringExtra("direcs");
String actor = getIntent().getStringExtra("stars");
img_src = getIntent().getStringExtra("img1");
TextView m_name = (TextView) findViewById(R.id.msms_name);
m_name.setText(name);
TextView m_name1 = (TextView) findViewById(R.id.msms_name1);
m_name1.setText(name);
// TextView m_year = (TextView) findViewById(R.id.msms_year);
// m_year.setText(year);
TextView m_genre = (TextView) findViewById(R.id.msms_genre);
m_genre.setText(genre);
TextView m_rating = (TextView) findViewById(R.id.msms_rating);
m_rating.setText(rating);
TextView m_desc = (TextView) findViewById(R.id.msms_desc);
m_desc.setText(desc);
TextView m_direc = (TextView) findViewById(R.id.msms_direc);
m_direc.setText(director);
TextView m_actor = (TextView) findViewById(R.id.msms_actor);
m_actor.setText(actor);
return null;
}
}
}
答案 0 :(得分:0)
由于stringRequest在不同的线程上运行,因此无法确定在显示吐司时是否已经有了响应,因此,只有在获得响应之后,才应该查看大小。 / p>
简而言之,将烤面包移到onResponse内:
stringRequest = new StringRequest(Request.Method.GET,
url_all,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("movies");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
String name = o.getString("name");
String year = o.getString("year");
String genre = o.getString("genre");
String rating = o.getString("rating");
String director = o.getString("directors_name");
String actor = o.getString("cast");
String img_src = o.getString("poster_src");
String desc = o.getString("description");
m_name3.add(name);
m_release_year3.add(year);
m_genre3.add(genre);
m_rating3.add(rating);
m_director3.add(director);
m_actor3.add(actor);
m_imgsrc3.add(img_src);
m_desc3.add(desc);
}
Toast.makeText(msms.this, "Index: "+ m_name3.size(), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Some error Occured", Toast.LENGTH_SHORT).show();
}
});