Android URL连接java.io.FileNotFoundException

Jan*_*Jan 3 java email android httprequest bufferedreader

我尝试向WordlPress网站发送正常的Http请求.

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

如果我用我的Web浏览器打开它,一切正常.如果我在Android App上打开它会抛出java.io.FileNotFoundException.

Android代码:

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) {

    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我删除电子邮件地址的"@"或".com"时,它甚至适用于Android.似乎可用的电子邮件地址是崩溃的原因.

这是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)
Run Code Online (Sandbox Code Playgroud)

Nom*_*que 5

URL编码存在问题.看看这个链接

String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "https://stackoverflow.com/search?q=" + query;
Run Code Online (Sandbox Code Playgroud)