Connecting with Android URL java.io.FileNotFoundException

I am trying 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 everything works fine. If I open it on top of an Android app, it throws java.io.FileNotFoundException.

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 ".com" E-Mail addresses, it even works on Android. It looks like an accessible email address is causing the crash.

This is 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)

      

+3


source to share


1 answer


There is a problem 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;

      

+5


source







All Articles