Android获取请求JSON图像和文字

发布于 2025-01-24 14:49:50 字数 8511 浏览 6 评论 0原文

嗨,我正在处理一个应用程序,在该应用程序中,我正在使用GET请求来获取JSON中的数据。我必须显示图像和文字。我使用文本,但我无法拍摄图像。我正在使用ArrayList存储字符串值,但对于图像,我不知道如何使用。 下方帮助我的代码

请在MainActivity

try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = "";

                while ((line = reader.readLine()) != null) {

                    buffer.append(line+"\n");
                    Log.d("Response: ", "> " + line);  ..... :-)

                }

                String response = buffer.toString();
                String page = "2021";
                try {

                    JSONObject json = new JSONObject(response);
                    JSONArray listings = json.getJSONArray("listings");
                    JSONObject jObject = null;
                    for (int i = 0; i < listings.length(); i++) {
                        JSONObject c = listings.getJSONObject(i);
                        String year = c.getString("year");
                        String make = c.getString("make");
                        String model = c.getString("model");
                        String trim = c.getString("trim");
                        String mileage = c.getString("mileage");
                        String currentPrice = c.getString("currentPrice");
                        JSONObject addressObject = c.getJSONObject("images").getJSONObject("firstPhoto");
                        String image = addressObject.getString("large");
                        URL url1 = new URL(image);
                        Bitmap bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
                        // tmp hash map for single contact
                        //page += "Node"+i+" : \n Name= "+ id;
                        HashMap<String, String> listing = new HashMap<>();
                        HashMap<String, Bitmap> listing1 = new HashMap<>();

                        // adding each child node to HashMap key => value
                        listing.put("year",year);
                        listing.put("make",make);
                        listing.put("model",model);
                        listing.put("trim",trim);
                        listing.put("mileage",mileage);
                        listing.put("currentPrice",currentPrice);
                        // adding listing to listing list
                        listingList.add(listing);
                    }

@Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (pDialog.isShowing()){
                pDialog.dismiss();
                //tvYear.setText(result);
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, listingList,
                        R.layout.list_item, new String[]{ "year","make","model","trim","mileage","image"},
                        new int[]{R.id.tvYear, R.id.tvMake, R.id.tvModel, R.id.tvTrim, R.id.tvMileage, R.id.tvPrice});
                lv.setAdapter(adapter);
            }
        }

list_item

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp">


        <ImageView
            android:id="@+id/imageViewVehicle"
            android:layout_width="403dp"
            android:layout_height="201dp"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="6dp"
            android:contentDescription="@string/Vehicle"
            android:src="@drawable/car" />

        <TextView
            android:id="@+id/tvYear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageViewVehicle"
            android:text="2022"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"/>


        <TextView
            android:id="@+id/tvMake"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvYear"
            android:text="BMW"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_below="@+id/imageViewVehicle"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvModel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvMake"
            android:text="3 Series"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_below="@+id/imageViewVehicle"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvTrim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvModel"
            android:text="328i"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_below="@+id/imageViewVehicle"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvYear"
            android:text="$ 17,234"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"/>


        <TextView
            android:id="@+id/tvDivider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvPrice"
            android:layout_below="@+id/tvYear"
            android:text="  |  "
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvMileage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvDivider"
            android:layout_below="@+id/tvYear"
            android:text="39.1k mi"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvPrice"
            android:text="Highland Park, IL"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"/>


        <Button
            android:id="@+id/btnCallDealer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_below="@+id/tvLocation"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:minHeight="60dp"
            android:textColor="#1357a6"
            android:background="@color/white"
            android:text="@string/call_dealer" />
    </RelativeLayout>

该代码正在工作,但我不知道如何显示ImageView

Hi I am working on an application where I am using get request to get data in JSON. And I have to display image and text.I got working with text but Image I cant take. I am using an arraylist to store the stringvalue but for image I dont know how to use. Please help My code below

MainActivity

try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = "";

                while ((line = reader.readLine()) != null) {

                    buffer.append(line+"\n");
                    Log.d("Response: ", "> " + line);  ..... :-)

                }

                String response = buffer.toString();
                String page = "2021";
                try {

                    JSONObject json = new JSONObject(response);
                    JSONArray listings = json.getJSONArray("listings");
                    JSONObject jObject = null;
                    for (int i = 0; i < listings.length(); i++) {
                        JSONObject c = listings.getJSONObject(i);
                        String year = c.getString("year");
                        String make = c.getString("make");
                        String model = c.getString("model");
                        String trim = c.getString("trim");
                        String mileage = c.getString("mileage");
                        String currentPrice = c.getString("currentPrice");
                        JSONObject addressObject = c.getJSONObject("images").getJSONObject("firstPhoto");
                        String image = addressObject.getString("large");
                        URL url1 = new URL(image);
                        Bitmap bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
                        // tmp hash map for single contact
                        //page += "Node"+i+" : \n Name= "+ id;
                        HashMap<String, String> listing = new HashMap<>();
                        HashMap<String, Bitmap> listing1 = new HashMap<>();

                        // adding each child node to HashMap key => value
                        listing.put("year",year);
                        listing.put("make",make);
                        listing.put("model",model);
                        listing.put("trim",trim);
                        listing.put("mileage",mileage);
                        listing.put("currentPrice",currentPrice);
                        // adding listing to listing list
                        listingList.add(listing);
                    }

@Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (pDialog.isShowing()){
                pDialog.dismiss();
                //tvYear.setText(result);
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, listingList,
                        R.layout.list_item, new String[]{ "year","make","model","trim","mileage","image"},
                        new int[]{R.id.tvYear, R.id.tvMake, R.id.tvModel, R.id.tvTrim, R.id.tvMileage, R.id.tvPrice});
                lv.setAdapter(adapter);
            }
        }

list_item

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp">


        <ImageView
            android:id="@+id/imageViewVehicle"
            android:layout_width="403dp"
            android:layout_height="201dp"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="6dp"
            android:contentDescription="@string/Vehicle"
            android:src="@drawable/car" />

        <TextView
            android:id="@+id/tvYear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageViewVehicle"
            android:text="2022"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"/>


        <TextView
            android:id="@+id/tvMake"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvYear"
            android:text="BMW"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_below="@+id/imageViewVehicle"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvModel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvMake"
            android:text="3 Series"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_below="@+id/imageViewVehicle"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvTrim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvModel"
            android:text="328i"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:layout_below="@+id/imageViewVehicle"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvYear"
            android:text="$ 17,234"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"/>


        <TextView
            android:id="@+id/tvDivider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvPrice"
            android:layout_below="@+id/tvYear"
            android:text="  |  "
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvMileage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvDivider"
            android:layout_below="@+id/tvYear"
            android:text="39.1k mi"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>


        <TextView
            android:id="@+id/tvLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvPrice"
            android:text="Highland Park, IL"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"/>


        <Button
            android:id="@+id/btnCallDealer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_below="@+id/tvLocation"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:minHeight="60dp"
            android:textColor="#1357a6"
            android:background="@color/white"
            android:text="@string/call_dealer" />
    </RelativeLayout>

The code is working but I dont know how to display the imageview

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-31 14:49:50

根据您的要求,请使用下面的代码,由您的:

ImageView iv = (ImageView) findViewbyId(R.id.image_view1);

iv.setImageBitmap(bmp);

Use the code below as you prefer, change the R.id.image_view1 by yours:

ImageView iv = (ImageView) findViewbyId(R.id.image_view1);

iv.setImageBitmap(bmp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文