我开发了一个 servlet,我试图通过一个 android 应用程序访问它。这是我的异步课程的重要部分。'params[0]' 是作为参数传入的 url
URL url;
BufferedReader reader = null;
String s = "";
try {
url = new URL(params[0]);
URLConnection con = url.openConnection();
reader = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
s = s + line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
这在大多数情况下都有效。但有时,我得到一个 java.io.FileNotFoundException 当我使用相同的 url 并在浏览器中尝试它时它可以工作,但是从我的应用程序中,无论我尝试多少次它都不起作用。这是我的日志:
05-14 19:35:51.852: W/System.err(767): java.io.FileNotFoundException: http://192.168.10.105:8080/CarpoolServer/EditProfileServlet?name=anna&yob=1977&gender=Female&major=Architecture&uni=N/A&occupation=Software Development&hobby1=Basketball&hobby2=Reading&hobby3=Football&email=jad_naoum%40hotmail.com&phone=70202018&from11=Beirut&from12=Beirut&from13=Mousseitbeh&to11=Beirut&to12=Beirut&to13=Mazraa&days1=SAT&time_from1=3:21AM&time_to1=4:21PM&car=true
05-14 19:35:51.852: W/System.err(767): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
05-14 19:35:51.852: W/System.err(767): at com.jad.carpooling.EditProfile$Async.doInBackground(EditProfile.java:416)
05-14 19:35:51.862: W/System.err(767): at com.jad.carpooling.EditProfile$Async.doInBackground(EditProfile.java:1)
05-14 19:35:51.862: W/System.err(767): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-14 19:35:51.862: W/System.err(767): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-14 19:35:51.862: W/System.err(767): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-14 19:35:51.862: W/System.err(767): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-14 19:35:51.872: W/System.err(767): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-14 19:35:51.872: W/System.err(767): at java.lang.Thread.run(Thread.java:856)
有任何想法吗?
我想到了。看来我没有正确编码 URL。URL 中有一个空格。当我对其进行编码时,它起作用了。谢谢您的帮助