简体   繁体   English

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

[英]I get 404 response trying to access a jsp page in my localhost server from my Android app

I have an android project with one main activity. 我有一个主要活动的android项目。 And i want it to access a JSP page inside my localhost server. 我希望它访问我的localhost服务器中的JSP页面。

I have a tomcat 7 in my pc and, in /webapps/ i have a folder called JSPyDB where my ejemplo.jsp page is stored.(i get to access the jsp page from the browser so i can see that the server runs perfectly). 我的电脑里有一个tomcat 7,在/ webapps /我有一个名为JSPyDB的文件夹,我的ejemplo.jsp页面存放在那里。(我可以从浏览器访问jsp页面,这样我就可以看到服务器运行完美了) 。

So this is my MainMenuActivity: 所以这是我的MainMenuActivity:

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);
}

} }

And the response i get is 404... 我得到的回应是404 ......

I tried other urls like the ip address of my pc in my network 192.68.1.12. 我在我的网络192.68.1.12尝试了其他网址,比如我的电脑的IP地址。 But still 404 response. 但仍有404回应。

What am i missing here if i cannot even get a good response? 如果我甚至无法得到好的回应,我在这里失踪了什么?

EDITED: it was a stupid thing. 编辑:这是一个愚蠢的事情。 My folder is JSPyDB and the urls was wrong using JSPYDB. 我的文件夹是JSPyDB,使用JSPYDB时url是错误的。 Typo mistake. 错字错误。

it was a stupid thing. 这是一个愚蠢的事情。 My folder is JSPyDB and the urls was wrong using JSPYDB. 我的文件夹是JSPyDB,使用JSPYDB时url是错误的。

Typo mistake. 错字错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正试图从我的Android模拟器访问我的localhost服务器,我收到了ECONNREFUSED错误 - I'm trying to access to my localhost server from my Android Emulator and i'm getting an ECONNREFUSED error 尝试从本地主机获取xml时在Android应用中获取404 - Getting 404 in Android app while trying to get xml from localhost 如何从Android手机访问本地XAMP服务器? - How can i access my localhost XAMP server from my Android mobile? 在我的情况下,设备访问本地服务器(在MacBook上)上运行的Android应用 - Android app running on device access localhost server (on MacBook) in my case 无法访问我的社交android应用程序的wamp服务器(本地主机)? - Cant access wamp server(localhost) for my social android app? 当我的Android应用程序在我的服务器上调用我的API时,为什么会得到空响应? - Why do I get an empty response when my android app calls my API on my server? 如何从我的 Android 设备访问我的本地主机? - How can I access my localhost from my Android device? 如何从我的 Android 设备访问我的本地主机 - How can I access my localhost from my Android device 如何从Android应用程序连接到我的本地主机Web服务器 - How to connect to my localhost web server from Android app 无法将我的 android 应用程序连接到本地主机服务器 - Unable to connect my android app to localhost server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM