您好我的应用程序有一个奇怪的问题。我试图将一个Android客户端连接到一个Servlet,但是当我运行我的android应用程序时,我得到一个FileNotFoundException
。我在Netbeans中创建了一个带有多个控制器的java web项目,当我连接到我创建的第一个Servlet时,一切正常,但是当我尝试连接到另一个Servlet时,我得到了异常。
我正在使用这两个网址:
http://10.0.2.2:8080/HelloWordlServlet2/HelloWorld(这很好用,它是我创建的第一个servlet)
http://10.0.2.2:8080/HelloWordlServlet2/GetList(这不起作用,我在尝试连接时得到FileNotFoundException)
我在使用Android应用程序之前部署服务器,我会在下面发布web.xml
所以可能问题就在那里。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>Controller.HelloWorld</servlet-class>
</servlet>
<servlet>
<servlet-name>GetList</servlet-name>
<servlet-class>Controller.GetList</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetList</servlet-name>
<url-pattern>/GetList</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
这是活动的代码
public class LoginPanel extends AppCompatActivity {
Button showListButton;
EditText showListText;
public String URL =
"http://10.0.2.2:8080/HelloWordlServlet2/GetList";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_panel);
findId();
setOnClickListenerButton();
}
public void findId(){
showListButton= (Button)findViewById(R.id.button_show_list);
showListText= (EditText)findViewById(R.id.text_show_list);
}
public void setOnClickListenerButton(){
showListButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getListConnection test = new getListConnection();
test.execute(URL);
}
});
}
private class getListConnection extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
StringBuffer output = new StringBuffer("ciao");
try {
java.net.URL url = new URL(URL); //Enter URL here
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET"); // here you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
//httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
httpURLConnection.connect();
//if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = httpURLConnection.getInputStream();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(in));
String s = "";
while ((s = buffer.readLine()) != null)
output.append(s);
//}
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
Log.d("errore","Non trovo il file");
e.printStackTrace();
}
return output.toString();
}
protected void onPostExecute(String output) {
showListText.setText(output);
}
}
}
我使用的服务器是glassfish,有人可以帮助我吗? ty
这是错误日志
05-15 14:12:19.754 20338-20367/com.example.luchino.simplehttpgetservlet D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-15 14:12:19.878 20338-20367/com.example.luchino.simplehttpgetservlet D/errore: Non trovo il file
05-15 14:12:19.878 20338-20367/com.example.luchino.simplehttpgetservlet W/System.err: java.io.FileNotFoundException: http://10.0.2.2:8080/HelloWordlServlet2/GetList
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
at com.example.luchino.simplehttpgetservlet.LoginPanel$getListConnection.doInBackground(LoginPanel.java:68)
at com.example.luchino.simplehttpgetservlet.LoginPanel$getListConnection.doInBackground(LoginPanel.java:53)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
05-15 14:12:19.879 20338-20367/com.example.luchino.simplehttpgetservlet W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)