0

I am building a simple app that will download random images from 'catass.com' and display them in the app in a slideshow format. My code displays no errors until I attempt to run it. I get the following:

ParseError at [row,col]:[8,10] Message: The processing instruction target matching "[xX][mM][lL]" is not allowed.

I am unsure what exactly is causing this, below is my javaMain

package com.example.lab6;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ProgressBar progressBar;
    private ImageView catImageView;
    private String currentCatId;
    private Bitmap currentCatPicture;

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

        progressBar = findViewById(R.id.progressBar);
        catImageView = findViewById(R.id.catImageView);

        CatImages catImages = new CatImages(this);
        catImages.execute();
    }

    private void updateCatImageView(Bitmap catPicture) {
        catImageView.setImageBitmap(catPicture);
    }

    private void updateProgressBar(int progress) {
        progressBar.setProgress(progress);
    }

    @SuppressLint("StaticFieldLeak")
    private class CatImages extends AsyncTask<Void, Integer, Void> {
        private final Context context;

        public CatImages(Context context) {
            this.context = context;
        }

        @Override
        protected Void doInBackground(Void... params) {
            while (true) {
                try {
                    // Get a new random cat image from the API
                    URL url = new URL("https://cataas.com/cat?json=true");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    String response = convertStreamToString(input);
                    JSONObject jsonObject = new JSONObject(response);
                    String id = jsonObject.getString("id");
                    String urlStr = jsonObject.getString("url");

                    // Check if the cat image is already saved locally
                    String fileName = id + ".jpg";
                    File file = new File(context.getFilesDir(), fileName);
                    if (file.exists()) {
                        // Load the cat image from the file
                        currentCatId = id;
                        currentCatPicture = BitmapFactory.decodeFile(file.getAbsolutePath());
                        publishProgress(100);
                    } else {
                        // Download and save the cat image
                        url = new URL(urlStr);
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setDoInput(true);
                        connection.connect();
                        input = connection.getInputStream();
                        currentCatPicture = BitmapFactory.decodeStream(input);
                        FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
                        currentCatPicture.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                        outputStream.close();
                        currentCatId = id;
                        publishProgress(100);
                    }

                    // Wait for a few seconds before loading the next cat image
                    for (int i = 0; i < 100; i++) {
                        Thread.sleep(30);
                        publishProgress(i);
                    }
                } catch (IOException | InterruptedException | JSONException e) {
                    Log.e(TAG, "Error", e);
                }
            }
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            updateProgressBar(progress[0]);
            if (currentCatPicture != null && currentCatId != null) {
                if (!currentCatId.equals(catImageView.getTag())) {
                    updateCatImageView(currentCatPicture);
                    catImageView.setTag(currentCatId);
                }
            }
        }

        private String convertStreamToString(InputStream input) throws IOException {
            StringBuilder stringBuilder = new StringBuilder();
            byte[] bytes = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(bytes)) != -1) {
                stringBuilder.append(new String(bytes, 0, bytesRead));
            }
            return stringBuilder.toString();
        }
    }
}
Steve X
  • 55
  • 5
  • It seems that you have problem with your XML files. Have you looked into this question https://stackoverflow.com/questions/19889132/error-the-processing-instruction-target-matching-xxmmll-is-not-allowed – Roshan Kumar Apr 07 '23 at 02:28

0 Answers0