jeudi 14 mai 2015

Android - web request not working and application stop working

i have a android app .i want to get a request from server and show it to user.but in android it not working and application stop working. this is my code: please check my code and fix it. thanks all.

 URL url;
    try {
        url = new URL("http://ift.tt/1IAAyJx");
        URLConnection conn = url.openConnection();
        conn.setRequestProperty("Cookie", "IBS_SESSID=hlpjpnm8hdrf84n7igk0m5n9v6; session_id=f49f028f0c3b87719638136ce5cd5f23090eca5e");
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String inputLine;

        while ((inputLine = br.readLine()) != null) {
            if (inputLine.contains("<td class=") && inputLine.contains("Middle") && inputLine.contains("<sub>"))
            {
                String chargenum =  getBetween(inputLine, "<td class=\"List_Col\" valign=\"Middle\" >", "<sub>").trim();

                String chargeunit = getBetween(inputLine, "<sub>", "</sub>");
                String gig = chargenum.trim() + chargeunit.trim();
                System.out.println("Your gig is "+gig);
            }
        }
        br.close();



    } catch (IOException e) {
    }
}
public static String getBetween(String str, String open, String close) {
    if (str == null || open == null || close == null) {
        return null;
    }
    int start = str.indexOf(open);
    if (start != -1) {
        int end = str.indexOf(close, start + open.length());
        if (end != -1) {
            return str.substring(start + open.length(), end);
        }
    }
    return null;
}




Aucun commentaire:

Enregistrer un commentaire