图片实在想不出应该怎么做了
相关代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
相关代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="牛排"
android:layout_toRightOf="@id/img"
android:textSize="20sp"/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:text="大飒飒大大"
android:layout_toRightOf="@id/img"/>
<TextView
android:id="@+id/q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥"
android:textSize="20sp"
android:layout_toRightOf="@id/img"
android:layout_below="@id/description"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="39.9"
android:textSize="20sp"
android:layout_toRightOf="@id/q"
android:layout_below="@id/description"/>
<TextView
android:id="@+id/action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用户一元"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/price"
android:layout_below="@id/description"/>
<TextView
android:id="@+id/yishou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已售:"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/action"
android:layout_below="@id/description"/>
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="44"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/yishou"
android:layout_below="@id/description"/>
</RelativeLayout>
相关代码:
package com.example.zuoye12;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import com.example.zuoye12.util.Msg;
import com.example.zuoye12.util.Url;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Msg> list;
private ListView listView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview);
list = new ArrayList<>();
new Thread() {
@Override
public void run() {
super.run();
try {
JSONObject jo = new JSONObject(Url.get("http://www.imooc.com/api/shopping?type=11"));
// String a=jo.getString("data");
// Log.e("TAG",a);
JSONArray array = jo.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
Log.e("TAG","1");
JSONObject object=array.getJSONObject(i);
String id=object.getString("id");
String name=object.getString("name");
String img=object.getString("img");
String count=object.getString("count");
String price=object.getString("price");
String description=object.getString("description");
String action=object.getString("action");
list.add(new Msg(id,name,img,count,price,description,action));
}
adapter = new MyAdapter(MainActivity.this, list);
runOnUiThread(new Runnable() {
@Override
public void run() {
listView.setAdapter(adapter);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}.start();
}
}
相关代码:
package com.example.zuoye12;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.zuoye12.util.Msg;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Msg> list;
public MyAdapter(Context context, List<Msg> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.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) {
ViewHolder viewHolder = null;
if (viewHolder == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
viewHolder.name = convertView.findViewById(R.id.name);
viewHolder.imageView=convertView.findViewById(R.id.img);
viewHolder.count = convertView.findViewById(R.id.count);
viewHolder.price = convertView.findViewById(R.id.price);
viewHolder.description = convertView.findViewById(R.id.description);
viewHolder.action = convertView.findViewById(R.id.action);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Msg msg = list.get(position);
String img=msg.getImageView();
viewHolder.name.setText(msg.getName());
viewHolder.count.setText(msg.getCount());
viewHolder.price.setText(msg.getPrice());
viewHolder.description.setText(msg.getDescription());
viewHolder.action.setText(msg.getAction());
return convertView;
}
public static class ViewHolder {
static TextView name, count, price, description, action;
static ImageView imageView;
}
}
相关代码:
package com.example.zuoye12.util;
import android.widget.ImageView;
public class Msg {
private String id;
private String name;
String imageView;
private String count;
private String price;
private String description;
private String action;
public Msg(String id, String name, String imageView, String count, String price, String description, String action) {
this.id = id;
this.name = name;
this.imageView = imageView;
this.count = count;
this.price = price;
this.description = description;
this.action = action;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImageView() {
return imageView;
}
public void setImageView(String imageView) {
this.imageView = imageView;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
@Override
public String toString() {
return "Msg{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", imageView=" + imageView +
", count='" + count + '\'' +
", price='" + price + '\'' +
", description='" + description + '\'' +
", action='" + action + '\'' +
'}';
}
}
相关代码:
package com.example.zuoye12.util;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Url {
public static String get(String s){
try {
URL url=new URL(s);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6000);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
//5.判断响应码并获取响应数据(响应的正文)
//获取响应的流
InputStream in = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = in.read(b)) > -1) {
baos.write(b, 0, len);
}
Log.e("TAG","a");
String msg = new String(baos.toByteArray());
return msg;
}
} catch (Exception e) {
e.printStackTrace();
}
Log.e("TAG","b");
return null;
}
}
尝试过的解决方式:
在MyAdapter再创建一个异步任务静态内部类,也试过在新建一个异步任务类
相关截图:
问题描述:
获得了Json解析的字符串,但是没办法解决图片的问题
10
收起
正在回答 回答被采纳积分+1
1回答
LovelyChubby
2021-08-01 22:51:22
可以把网络请求得到的图片的url 在浏览器中打开,以确保连接是有效的地址
如果图片连接是http开头的,需要在manefiest中配置android:usesCleartextTraffic="true"。因为在高版本中(android.9.0)开始,不允许访问http协议的网络了。
你也可以断点到Url这个类的catch (Exception e) { }的地方,可以观察网络请求时都失败了。
不会断点的话,可以通过log日志输出的形式观察图片的网络请求
android基础入门课也可以参考:https://www.songyubao.com/book/primary/
2.Android 网络操作与流行框架
- 参与学习 人
- 提交作业 220 份
- 解答问题 1800 个
本阶段是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理;从文件存储、到轻量级数据库SQLite的使用;最后利用最流程的第三方框架助你快速开发企业级APP。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星