tags:

views:

14

answers:

0

Hi, in my android application i have to download xml files from the server. i used the code that shown below. sometimes it shows null pointer exception for getResponseCode() method. the httpConn is not null.

                    public static byte[] getGprsConnData(String urlStr){
                    URL url = new URL(urlStr);
        URLConnection urlConn = url.openConnection();
        if(!(urlConn instanceof HttpURLConnection)){
        Log.e("Error", "connection is not an instance of HTTP connection");
            throw new IOException ("URL is not an Http URL");
        }
        httpConn = (HttpURLConnection)urlConn;
        if(httpConn == null){
            System.out.println("httpconnection is null");
        }else{
            System.out.println("httpconnection isn't null");
        }
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        System.out.println("getting http Connection ");
        httpConn.connect(); 
        System.out.println("http Connection established");
        int code = -1;
        try{
        code = httpConn.getResponseCode();
        }catch(Exception e){
            e.printStackTrace();
            Log.e("Exception", "while getting http response code");
        }
        System.out.println("http Connection response code" + code);
        if(httpConn != null && httpConn.getResponseCode()!= HttpURLConnection.HTTP_OK){
            return null;
        }
        System.out.println("getting inputStream");
        InputStream fromServer = httpConn.getInputStream();}

What will be the reason for that exception?? anybody please help