Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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
我尝试从Android应用程序访问localhost服务器中的jsp页面时收到404响应_Android_Jsp_Url_Localhost - Fatal编程技术网

我尝试从Android应用程序访问localhost服务器中的jsp页面时收到404响应

我尝试从Android应用程序访问localhost服务器中的jsp页面时收到404响应,android,jsp,url,localhost,Android,Jsp,Url,Localhost,我有一个android项目,有一个主要活动。 我希望它访问本地主机服务器中的JSP页面 我的电脑中有一个tomcat 7,在/webapps/中有一个名为JSPyDB的文件夹,其中存储着我的ejempo.jsp页面。我可以从浏览器访问jsp页面,这样我就可以看到服务器运行得很好 这就是我的主要活动: public class MainMenuActivity extends Activity implements OnClickListener{ private static String D

我有一个android项目,有一个主要活动。 我希望它访问本地主机服务器中的JSP页面

我的电脑中有一个tomcat 7,在/webapps/中有一个名为JSPyDB的文件夹,其中存储着我的ejempo.jsp页面。我可以从浏览器访问jsp页面,这样我就可以看到服务器运行得很好

这就是我的主要活动:

public class MainMenuActivity extends Activity implements OnClickListener{

private static String DEBUG_TAG1 = "MainMenuA";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(R.layout.mainmenu);
    findViewById(R.id.my_space_button).setOnClickListener(this);

}
@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.my_space_button:
            ConnectivityManager cM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo infoNet = cM.getActiveNetworkInfo();
            String url = "http://10.0.2.2:8080/JSPYDB/ejemplo.jsp";
            if(infoNet != null && infoNet.isConnected()){
                //fetch data
                new Task().execute(url);
                Log.d(DEBUG_TAG1,"Able to connect to network ");
            }
            else{
                //error to connect
                Log.d(DEBUG_TAG1,"fail to connect to network ");
            }

            break;
    }
} 
private class Task extends AsyncTask <String, Void, String>{
    @Override
    protected String doInBackground(String... urls){
        try {
            Log.d(DEBUG_TAG1,"do in background download url ");
            return downloadUrl(urls[0]);
            //return "Retrieve page";
        } catch (Exception e) {
            return "Unable to retrieve page";
        }
    }
    @Override
    protected void onPostExecute(String result){
    }

}
private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;
    Log.d(DEBUG_TAG1,"download url: " + myurl);
    // Only display the first 500 characters of the retrieved
    // web page content.
    int len = 500;

    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        Log.d(DEBUG_TAG1,"create connection to: " + url);
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        Log.d(DEBUG_TAG1,"connect()");
        int response = conn.getResponseCode();
        Log.d(DEBUG_TAG1, "The response is: " + response);
        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = readIt(is, len);
        return contentAsString;

    // Makes sure that the InputStream is closed after the app is
    // finished using it.
    } finally {
        if (is != null) {
            is.close();
        } 
    }
}
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");        
    char[] buffer = new char[len];
    reader.read(buffer);
    return new String(buffer);
}
}

我得到的答复是404

我在网络192.68.1.12中尝试了其他URL,比如我电脑的ip地址。但仍有404响应

如果我连一个好的回答都得不到,我还缺什么

编辑: 这是一件愚蠢的事。我的文件夹是JSPyDB,使用JSPyDB时URL错误。
打字错误。

这是一件愚蠢的事情。我的文件夹是JSPyDB,使用JSPyDB时URL错误


输入错误。

请看这里:thx,但建议对姜饼和更高版本使用HttpURLConnection,比HttpClient更好。是的,我知道。这对您很有帮助??不,抱歉,链接中的代码看起来与我的类似,并且建议的解决方案使用Httpclient,我对此不感兴趣。无论如何,Thx: