【问题标题】:get dynamic url with json in listview在listview中使用json获取动态url
【发布时间】:2016-06-27 17:58:16
【问题描述】:

我是 android 的新程序员,我编写程序来获取图像 url 并在列表视图中显示图像,我的程序工作正常并且可以显示图像但是当我想显示图像时我应该使用静态 url,但我想要使用静态链接获取所有带有 json 的图像 url 并在列表视图中显示。

我有 json 类来获取图像 url,但我不知道如何使用它 private String imageUrls[] 从我的静态链接中获取图片动态 url。

***string url 是 mainactivity 中的静态链接,用于获取带有 json 的图像 url。

我的主要活动类:

public class MainActivity extends Activity {

private final String url="http://192.168.1.4:81/upload/images.php";

ListView list;
LazyAdapter adapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.listView1);
    adapter = new LazyAdapter(this, imageUrls);
    list.setAdapter(adapter);

}

@Override
public void onDestroy() {
    list.setAdapter(null);
    super.onDestroy();
}

private String imageUrls[] = {
        "http://www.technotalkative.com/wp-content/uploads/2012/09/tt_listview1-171x300.png",
        "http://www.technotalkative.com/wp-content/uploads/2012/11/f-DayDream-Example-Landscape.png",
};


}

和json类:

class get url extends AsyncTask<String,Void,String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd=new ProgressDialog(MainActivity.this);
        pd.setMessage("login");
        pd.show();
    }

    @Override
    protected String doInBackground(String... params) {

        List<NameValuePair> parms=new ArrayList<>();
        JSONObject json=jParser.makeHTTPRequest(url,"GET");

        try {
            int t=json.getInt("t");
            if(t==1){
                s=json.getJSONArray("travel");
                for(int i=0;i<s.length();i++){

                    String url_image=c.getString("url_image");

                    HashMap<String,String>map=new HashMap<String,String>();

                    map.put("url_image",url_image);

                    P.add(map);

                }
            }else {
                Toast.makeText(MainActivity.this,"No Data Found",Toast.LENGTH_SHORT).show();
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

【问题讨论】:

    标签: java android json


    【解决方案1】:

    使用 Gson 库 gor 解析对 java 对象的 json 响应比手动提取字段要方便得多。使用 Retrofit 库而不是 asynctasks 也是一个很好的做法(更方便) - 请参阅 https://stackoverflow.com/a/38002999/6175778 帖子

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2020-09-02
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多