3

I try to send a normal Http Request to a WordlPress Website.

This is the URL: "http://localhost.com/api/submit_comment/?post_id=12345&name=Max&email=Max.Mustermann@googlemail.com&content=hallo"

If I open this with my Web-Browser all works fine. If I open it over this over an Android App it throws a java.io.FileNotFoundException.

The Android Code:

class SendComment extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... urls) {
        String _output = null;
        try {
            URL url = new URL(urls[0]);
            BufferedReader buffer = new BufferedReader(new InputStreamReader(url.openStream()));
            _output = buffer.readLine();
            buffer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return _output;
    }

    protected void onPostExecute(String result) {

    }
}

But when I remove the "@" or the ".com" of the E-Mail address it even works on Android. It seems like a available E-Mail address is the reason for the crash.

This is the StackTrace:

java.io.FileNotFoundException: http://localhost.com/api/submit_comment?post_id=23856&name=Max&email=Max.Mustermann%40googlemail.com&content=hallo 
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206) 
at java.net.URL.openStream(URL.java:470)
at de.example.app.SendComment.doInBackground(ServerManager.java:230)
at de.example.app.SendComment.doInBackground(ServerManager.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Jan
  • 75
  • 2
  • 6
  • StackTrace from logcat? Anyway, I believe this could simply be an URLEncode issue: https://developer.android.com/reference/java/net/URLEncoder.html – shkschneider Jun 16 '15 at 13:03
  • 1
    Is your phone connected to the same network as your computer? Try opening this URL on your phone's WebBrowser and see if it works. Also, try removing the "/" before the "?", leaving it like this: http://localhost.com/api/submit_comment?post_id=12345&name=Max&email=Max.Mustermann@googlemail.com&content=hallo – Rodrigo Direito Jun 16 '15 at 14:23
  • Exceptions are thrown, not returned. When you post the stack trace you will see that BufferedReader has nothing to do with it. – user207421 Jun 16 '15 at 14:32
  • `http://localhost.com/api/submit_comment?.....` localhost???? You have the website on the same device as your app? `a WorldPress website` ? – greenapps Jun 16 '15 at 15:16
  • This is only to show you what I mean. It's a normal Website. – Jan Jun 16 '15 at 15:21

1 Answers1

5

There's an issue with URL encoding. Take a look at This Link

String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "https://stackoverflow.com/search?q=" + query;
Community
  • 1
  • 1
Noman Rafique
  • 3,735
  • 26
  • 29
  • Thanks for your quick reply. If I use the URLEncoder it still give me the same error. After the URLEncoder the url looks like this: http://localhost.com/api/submit_comment/?post_id=12345&name=Max&email=Max.Musterman%40googlemail.com&content=abc – Jan Jun 16 '15 at 13:14