1

I want some help; I actually have to read and use the content of some website in an Android app. I followed some tutorials but in vain. Someone can help me here.

Updated:

I actually have used two different codes to get the content of a website but they did not work for me

public static String connect(String url)
{
    String result = "bubububu" ;

    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url); 

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Praeda",response.getStatusLine().toString());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {

            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            result= convertStreamToString(instream);
            // now you have the string representation of the HTML request
            instream.close();
            return result ;
        }


    } catch (Exception e) {
        e.getMessage() ;
    }

    return result ;
}

    private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}


public static String connect(String url)
{
    String result = "bubububu" ;

    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url); 

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Praeda",response.getStatusLine().toString());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {

            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            result= convertStreamToString(instream);
            // now you have the string representation of the HTML request
            instream.close();
            return result ;
        }


    } catch (Exception e) {
        e.getMessage() ;
    }

    return result ;
}

    private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

And

private String DownloadText(String URL)
{
    int BUFFER_SIZE = 2000;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        return "";
    }

    InputStreamReader isr = new InputStreamReader(in);
    int charRead;
    String str = "";
    char[] inputBuffer = new char[BUFFER_SIZE];          
    try {
        while ((charRead = isr.read(inputBuffer))>0)
        {                    
            //---convert the chars to a String---
            String readString = String.copyValueOf(inputBuffer, 0, charRead);
            str += readString;
            inputBuffer = new char[BUFFER_SIZE];
        }
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }    
    return str;        
}

private InputStream OpenHttpConnection(String urlString) 
        throws IOException
        {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))                     
        throw new IOException("Not an HTTP connection");

    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 

        response = httpConn.getResponseCode();                 
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                                 
        }                     
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");            
    }
    return in;     
        }

Both of these give me exception. The 1st one gives exception at response = httpclient.execute(httpget) and exception.getMessage() is "null" while the 2nd one gives exception at httpConn.setAllowUserInteraction(false) and exception.getMessage() is Error connecting. Even I have used Internet permissions in menifest file

user2281330
  • 181
  • 2
  • 11

2 Answers2

0

Look at the answer for this question: How do I use the Simple HTTP client in Android?

It has a code which will read some URL.

However, it's good idea to be more specific on StackOverflow and explain what is exactly your problem.

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
0

Not 100% sure of the question, but you can use Apache HTTPClient (recommended for pre-Gingerbread) or HTTPURLConnection (Gingerbread and onwards) and perform a GET to get the webpage. From here, you can go through the raw data (usually HTML, which comes back as text). There's plenty of good tutorials out there nowadays on both HTTPClient and HTTPURLConnection, so I won't explain it here

The other option is usually the WebView, which I'll admit can be is messy. The WebView lets you log in and do things like extract the resultant URL from the next page. The problem is that the behavior of WebView isn't the same across Android devices.

Joe Plante
  • 6,308
  • 2
  • 30
  • 23