简体   繁体   中英

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. And i want it to access a JSP page inside my localhost server.

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).

So this is my 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...

I tried other urls like the ip address of my pc in my network 192.68.1.12. But still 404 response.

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. Typo mistake.

it was a stupid thing. My folder is JSPyDB and the urls was wrong using JSPYDB.

Typo mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM