我想通过使用Asynctask从json获取图像url并在RecyclerView的帮助下将其设置在ImageView上。但是当我执行代码时它没有显示任何内容,这里是我的Json文件
[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "http://placehold.it/600/771796",
"thumbnailUrl": "http://placehold.it/150/771796"
}
]
这是我的主要活动
public class MainActivity extends AppCompatActivity {
ProgressDialog progressDialog;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new SimpleDivider(MainActivity.this));
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this);
recyclerView.setAdapter(recyclerViewAdapter);
new MyAsyncTask().execute();
}
private class MyAsyncTask extends AsyncTask<Object, Object, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Object... params) {
StringBuffer chaine = new StringBuffer("");
try {
URL url = new URL("https://jsonplaceholder.typicode.com/photos");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
chaine.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String result = chaine.toString();
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
JB jb = new JB();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String image = jObject.getString("url");
jb.setAndroid_image_url(image);
}
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
}
}
}
这是我的RecyclerAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<JB> jbs;
private Context context;
public RecyclerViewAdapter(Context context) {
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.image_layout, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Picasso.with(context).load(jbs.get(position).getAndroid_image_url()).resize(120, 60).into(holder.imageView);
}
@Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.image);
}
}
}
我正在使用picaso将照片加载到ImageView
答案 0 :(得分:1)
我认为你错过了什么,我做了一些改变。更新你的代码
更新onPostExecute
<div id="someImg" width="332px" height="300px">
</div>
更新RecyclerView适配器构造函数
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ArrayList<JB> jb = new ArrayList<JB>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String image = jObject.getString("url");
jb.setAndroid_image_url(image);
}
// remove below code from your OnCreate
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new SimpleDivider(MainActivity.this));
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this, jb);
recyclerView.setAdapter(recyclerViewAdapter);
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
}
答案 1 :(得分:0)
您必须创建JB类的ArrayList,然后传递给recyclerview适配器。
public static void createPipeline(Class<?> fnClass){
Pipeline p = TestPipeline.create();
p.apply(new Source()).apply(fnClass.newInstance());
p.apply(new AnotherSource()).apply(fnClass.newInstance());
p.run;
}
在RecyclerViewAdapter中,更改构造函数。
ArrayList<JB> jbArray = new ArrayList();
JB jb;
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
jb = new JB();
JSONObject jObject = jArray.getJSONObject(i);
String image = jObject.getString("url");
jb.setAndroid_image_url(image);
jbArray.add(jb);
}
recyclerViewAdapter = new RecyclerViewAdapter(this ,jbArray);
recyclerView.setAdapter(recyclerViewAdapter);
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
答案 2 :(得分:0)
初始化数组
ArrayList<JB> yourarray = new ArrayList;
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
JB jb = new JB();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
jb = new JB();
JSONObject jObject = jArray.getJSONObject(i);
String image = jObject.getString("url");
jb.setAndroid_image_url(image);
yourarray.add(jb);
}
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this,yourarray);
recyclerView.setAdapter(recyclerViewAdapter,yourarray);
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
}
更新适配器构造函数
public RecyclerViewAdapter(Context context,ArrayList<JB> jbs) {
this.context = context;
this.jbs=jbs;
}
答案 3 :(得分:0)
首先,你需要使用JB的ArrayList,因为在onPostExecute中你创建了一个JB对象并在每次循环迭代中覆盖数据。
其次,您不要在适配器中设置数据集。适配器始终在getItemCount方法中返回0。尝试类似:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<JB> jbs;
private Context context;
public setData(ArrayList<JB> list) {
jbs = list;
notifyDataSetChanged();
}
<...>
@Override
public int getItemCount() {
return jbs != null ? jbs.size() : 0;
}
}
在onPostExecute:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ArrayList<JB> jbList = new ArrayList<>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JB curJb = new JB();
JSONObject jObject = jArray.getJSONObject(i);
String image = jObject.getString("url");
curJb.setAndroid_image_url(image);
jbList.add(curJb);
}
recyclerViewAdapter.setData(jbList); //don't forget make recycleViewAdapter available in this scope
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
}