Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tomcat connect上的AVD中出现奇怪的FileNotFoundException_Java_Android_Tomcat_Servlets - Fatal编程技术网

Java Tomcat connect上的AVD中出现奇怪的FileNotFoundException

Java Tomcat connect上的AVD中出现奇怪的FileNotFoundException,java,android,tomcat,servlets,Java,Android,Tomcat,Servlets,我的android servlet设计用于在ApacheTomcat服务器上使用tomcat servlet发布请求和接收响应。为了进行调试,我使用相同的POST和GET方法设置了Servlet,这样我就可以通过浏览器尝试这些功能和可访问性 长话短说:当我部署应用程序时,我可以通过10.0.2.2:8080/my_app?request=test轻松地从AVD设备浏览器访问它,结果很好。使用localhost:8080/my_app?request=test从我的机器进行访问也是如此。 但是当我

我的android servlet设计用于在ApacheTomcat服务器上使用tomcat servlet发布请求和接收响应。为了进行调试,我使用相同的POST和GET方法设置了Servlet,这样我就可以通过浏览器尝试这些功能和可访问性

长话短说:当我部署应用程序时,我可以通过
10.0.2.2:8080/my_app?request=test
轻松地从AVD设备浏览器访问它,结果很好。使用
localhost:8080/my_app?request=test从我的机器进行访问也是如此。
但是当我从我的应用程序中尝试时,我总是得到一个
java.io.FileNotFoundException:http://10.0.2.2:8080/my_app
。 为什么?

到目前为止,我尝试了什么:这个应用程序有互联网权限,它们也可以工作,因为要到达Servlet通信点,我必须先通过PHP完成登录过程,它在同一台服务器上,工作正常

我的
AsyncTask
连接到servlet如下所示:

        AsyncTask<Void,Void,String> getDBdata = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                URL url = null;
                try {
                    url = new URL("http://10.0.2.2:8080/my_app");
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                String text;
                text = null;
                JsonArray js = null;
                try {
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoOutput(true);
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("action", "getDBData");
                    connection.setDoInput(true);
                    connection.connect();
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String aux = "";

                    while ((aux = in.readLine()) != null) {
                        builder.append(aux);
                    }
                    text = builder.toString();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return text;
AsyncTask getDBdata=new AsyncTask(){
@凌驾
受保护字符串doInBackground(无效…参数){
URL=null;
试一试{
url=新url(“http://10.0.2.2:8080/my_app");
}捕获(格式错误){
e、 printStackTrace();
}
字符串文本;
text=null;
JsonArray js=null;
试一试{
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(真);
connection.setRequestMethod(“POST”);
setRequestProperty(“action”、“getDBData”);
connection.setDoInput(true);
connection.connect();
BufferedReader in=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
StringBuilder=新的StringBuilder();
字符串aux=“”;
而((aux=in.readLine())!=null){
附加生成器(aux);
}
text=builder.toString();
}捕获(IOE异常){
e、 printStackTrace();
}
返回文本;

好吧,当然我一直在尝试在标题中发送参数,这导致了BS。菜鸟错误

bcody的回答在调试方面帮了我很大的忙!另外,从servlet中查看服务器协议可能会让我更早地发现错误

这是最终起作用的代码:

AsyncTask<Void,Void,String> getDBdata = new AsyncTask<Void, Void, String>() {
    @Override
    protected String doInBackground(Void... params) {
        URL url = null;
        try {
            url = new URL(Constants.SERVER_URL + getDBdataURL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        String text;
        text = null;
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
            connection.setRequestProperty("Accept", "*/*");
            connection.setChunkedStreamingMode(0);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            String request = "action=getDBdata";
            PrintWriter pw = new PrintWriter(connection.getOutputStream());
            pw.print(request);
            pw.close();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String aux = "";
            while ((aux = in.readLine()) != null) {
                builder.append(aux);
            }
            text = builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text;
    }
AsyncTask getDBdata=new AsyncTask(){
@凌驾
受保护字符串doInBackground(无效…参数){
URL=null;
试一试{
url=新url(Constants.SERVER\u url+getDBdataURL);
}捕获(格式错误){
e、 printStackTrace();
}
字符串文本;
text=null;
试一试{
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
setRequestProperty(“接受字符集”、“UTF-8”);
setRequestProperty(“用户代理”,“Mozilla/5.0(兼容)”);
connection.setRequestProperty(“接受”,“*/*”);
connection.setChunkedStreamingMode(0);
connection.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
connection.setDoOutput(真);
connection.setRequestMethod(“POST”);
String request=“action=getDBdata”;
PrintWriter pw=新的PrintWriter(connection.getOutputStream());
打印(请求);
关闭();
BufferedReader in=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
StringBuilder=新的StringBuilder();
字符串aux=“”;
而((aux=in.readLine())!=null){
附加生成器(aux);
}
text=builder.toString();
}捕获(IOE异常){
e、 printStackTrace();
}
返回文本;
}