[英]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.