0

I'd like to send an http request and get a string back from that request.

I am currently using this code, but no matter what my URL is, I always get null. What's the issue?

try { // Create a URL for the desired page URL url = new URL("http://hostname:80/index.html");

// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
    // str is one line of text; readLine() strips the newline character(s)
}
in.close(); } catch (MalformedURLException e) { } catch (IOException e) { }
user1319668
  • 329
  • 1
  • 4
  • 16
  • The code is working fine, if you supply a proper url. As you're trying to do this on Android, have you set the proper Internet permission in the AndroidManifest.xml? – Darwind Oct 16 '12 at 16:27
  • I have added the internet permissions in AndroidManifest.xml. I do give it proper URL but get null every time. – user1319668 Oct 16 '12 at 16:39
  • As a matter of fact, my URL was incorrect, now I have corrected it and I get an exception "java.io.EOFException". This is of course tested with my original code. – user1319668 Oct 16 '12 at 19:50
  • The url is this => http://tinyurl.com/coajz9p The string that will need to be back from that particular URL is: YldZbUZNakF3TUE9PQ== – user1319668 Oct 17 '12 at 16:55

3 Answers3

0

Try this

URL url = new URL("http://whatever");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//read stream here    
speakingcode
  • 1,518
  • 13
  • 12
0
                String URL = "http://hostname:80/index.html"
                String XML = stringWriter.toString();
                // System.out.println(XML);

                se = new StringEntity(XML, "UTF-8");
                se.setContentType("text/xml;charset=UTF-8");
                HttpPost httppost = new HttpPost(URL);
                httppost.setEntity(se);

                HttpClient httpclient = new DefaultHttpClient();
                response = httpclient.execute(httppost);

                HttpEntity entity = response.getEntity();

try use from this code

Jamshid
  • 340
  • 3
  • 12
  • As a matter of fact, my URL was incorrect, now I have corrected it and I get an exception "java.io.EOFException". This is of course tested with my original code. – user1319668 Oct 17 '12 at 10:03
0

This is working for me:

httpClient = AndroidHttpClient.newInstance("Android");
        String url = "Your URL";
        try{
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //Don't know what string you are waiting for
                 //do something
            }
        }
        catch(IOException e){
            httpClient.close();
        }
zkminusck
  • 1,230
  • 1
  • 12
  • 23
  • I get an error on this line: "HttpResponse response = httpClient.execute(httpGet);". – user1319668 Oct 18 '12 at 17:24
  • These are a few first lines of the errors: // Field descriptor #9 Lorg/apache/http/conn/ClientConnectionManager; protected final org.apache.http.conn.ClientConnectionManager connManager; // Field descriptor #11 Lorg/apache/http/conn/routing/HttpRoutePlanner; protected final org.apache.http.conn.routing.HttpRoutePlanner routePlanner; // Field descriptor #13 Lorg/apache/http/ConnectionReuseStrategy; protected final org.apache.http.ConnectionReuseStrategy reuseStrategy; – user1319668 Oct 22 '12 at 20:03