简体   繁体   中英

how to retrieve data from URL which is not a JSON object into android listView?

I have This URL and I want to fetch all the data present in here in an android list view, I only know how to retrieve data from a JSON object but here I don't even know the format of this data present in the URL.

The format of the URL is:
tvg-logo = url of the logo chanel

group-title = category where you need to display the channel (just for movie not for TV)

After the "," you have the name of the channel

And after the name you have the URL of video

How can I parse my data from the URL so that I can make a list view like that: img

i think, you must split the String text by special characters. and keep them in an array. for example,the special character might be "[ space character ]" or "," or "#". I hope to help you

This function will get the data from URL and you could split your data as per your requirement and populate UI.

void fetchDataFromUrl() {
        try {
            URL oracle = new URL("http://cinecosta.com/api_tv.php?pass=yojeju123");
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                System.out.println(inputLine);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Try with below code, Here I am extracted only url from the api response

    String strData = "#EXTM3U #EXTINF:-1 tvg-logo=\"http://www.cinecosta.com/image-appletv/tv/tf1-tv.png\" tvg-categorie=\"TV\",TF1 http://217.182.164.103:25461/live/YnAmpNBQUX/YUCgme6CXS/314.ts #EXTINF:-1 tvg-logo=\"http://www.cinecosta.com/image-appletv/tv/france2.png\" tvg-categorie=\"TV\",France 2 http://217.182.164.103:25461/live/YnAmpNBQUX/YUCgme6CXS/315.ts #EXTINF:-1 tvg-logo=\"http://www.cinecosta.com/image-appletv/tv/france3.png\" tvg-categorie=\"TV\",France 3 http://217.182.164.103:25461/live/YnAmpNBQUX/YUCgme6CXS/316.ts #EXTINF:-1 tvg-logo=\"http://www.cinecosta.com/image-appletv/tv/france4.png\" tvg-categorie=\"TV\",France 4 http://217.182.164.103:25461/live/YnAmpNBQUX/YUCgme6CXS/317.ts #EXTINF:-1 tvg-logo=\"http://www.cinecosta.com/image-appletv/tv/france5.png\" tvg-categorie=\"TV\",France 5 http://217.182.164.103:25461/live/YnAmpNBQUX/YUCgme6CXS/318.ts";

private void convertDataToArray() {
    String[] splitArray = strData.split("#EXTINF:-");
    ArrayList<String> arrstrUrl = new ArrayList<String>();
    ArrayList<String> arrstrMainUrl = new ArrayList<String>();
    ArrayList<String> arrstrCategory = new ArrayList<String>();
    ArrayList<String> arrstrName = new ArrayList<String>();
    for (int i = 1; i < splitArray.length; i++) {
        System.out.println("Final=>" + splitArray[i]);
        arrstrUrl.add(splitArray[i].split("1 tvg-logo=")[1].split(" ")[0]);
        arrstrMainUrl.add("http" + splitArray[i].split("1 tvg-logo=")[1].split("tvg-categorie=")[1].split("http")[1]);
        arrstrName.add(splitArray[i].split("1 tvg-logo=")[1].split("tvg-categorie=")[1].split(",")[0]);
        arrstrCategory.add(splitArray[i].split("1 tvg-logo=")[1].split("tvg-categorie=")[1].split(",")[1].split("http")[0]);
    }
    System.out.println("Final Image=>" + arrstrUrl.toString());
    System.out.println("Final Main=>" + arrstrMainUrl.toString());
    System.out.println("Final Name=>" + arrstrName.toString());
    System.out.println("Final Category=>" + arrstrCategory.toString());
}

So this way, you can get parse your data and update your listview.

Note:- You need to write your own logic to parse this data, by checking data pattern.

The result seems easy to parse actually.Just see the pattern. #SOMETHING tvg-logo="logo" tvg-categorie="something"

Use regex for split the pattern you want. Regex

if you are using retrofit as a network library so you can pass the "ResponseBody" in the api callback function. In onSuccess Method We will get the Body And Use the Following the Code.

Interface Class:

Call<ResponseBody>  yourFuncationName();


ResponseBody data = (ResponseBody) model.body();
            String json = getStringData(data.byteStream());

Function is

public String getStringData(InputStream inputStream) {
    BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder total = new StringBuilder();
    String line;
    try {
        while ((line = r.readLine()) != null) {
            total.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return total.toString();
}

Maybe this will helpful for you.

The solution for this is :

  1. Either you can scrap the data from python libraries like scrapy or beautiful soup then convert it to json and read from the android.

  2. Parse the html using the jsoup lib ( https://jsoup.org/ ) and model the data in the desire format that you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM