2

I am sending info to a php file through a url. When I type the url in my browser the right information is displayed, however I do not get this information through the InputStreamReader.

Here is my code.

 try {
          URL url = new URL("http://example/example.php?userImage="
         + URLEncoder.encode(userImageString, "UTF-8") + "&userHeight="
         + URLEncoder.encode(userHeight,"UTF-8" ));
          Log.d("url", "" + url);
          Scanner in = new Scanner(new InputStreamReader(url.openStream()))
            .useDelimiter("\n");

          Log.d("hasnext?", "" + in.hasNext());
      while (in.hasNext()) 
      {
          Log.d("sent", in.next());
      }
      in.close();
    }
      catch(Exception e)
      {
          e.printStackTrace();
      }
 }

HasNext returns false, it should be returning true. Any suggestions?

I should note that the userImageString is a Base64 encoded string.

James Fazio
  • 6,370
  • 9
  • 38
  • 47
  • Try replacing `Scanner in = new Scanner(new InputStreamReader(url.openStream())) .useDelimiter("\n");` with `Scanner in = new Scanner(url.openStream());`. – Eng.Fouad May 05 '12 at 22:22

1 Answers1

1

1) Do you have the permission to access the internet?

<uses-permission android:name="android.permission.INTERNET" />

2) I think you can solve your problem with this answere: Make an HTTP request with android

3) Here is a working http request which returns the response:

public String request(String fUrl){
    String page = "";
    try
    {
        HttpClient hc = new DefaultHttpClient();
        HttpPost post = new HttpPost(fUrl);
        HttpResponse response = hc.execute(post);

        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
        {
            page = EntityUtils.toString(response.getEntity());
        }
    } catch(IOException e) {
        e.printStackTrace();
        System.out.println("Exception Thrown");
    }
    return page;
}
Community
  • 1
  • 1
Wezelkrozum
  • 831
  • 5
  • 15
  • Yes I have an internet connection, I have done similar url calls on different parts off my application, which is why I cannot understand why this one is only working using a browser. – James Fazio May 05 '12 at 22:49
  • Using a HttpClient object is a lot easier, so I recommend using that instead of a InputStreamReader. – Wezelkrozum May 05 '12 at 22:53
  • Ok, I have tried to work with them before, is there a simple way to read from the results of a HTTPClient? – James Fazio May 05 '12 at 23:05
  • I have added a working http request function in point 3 of my answere – Wezelkrozum May 05 '12 at 23:13