我尝试从我的 Android 应用程序访问本地主机服务器中的 jsp 页面时收到 404 响应
I get 404 response trying to access a jsp page in my localhost server from my Android app
我有一个 android 项目,其中有一个主要 activity。
我希望它访问我的本地主机服务器内的 JSP 页面。
我的电脑中有一个 tomcat 7,在 /webapps/ 中,我有一个名为 JSPyDB 的文件夹,其中存储了我的 ejemplo.jsp 页面。(我可以访问来自浏览器的 jsp 页面,所以我可以看到服务器运行完美)。
所以这是我的 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);
}
}
我得到的响应是 404...
我尝试了其他网址,例如我的电脑在我的网络 192.68.1.12 中的 IP 地址。但是还是404响应。
如果我连一个好的回复都得不到,我还缺少什么?
已编辑:
这是一件愚蠢的事情。我的文件夹是 JSPyDB,使用 JSPYDB 时 url 是错误的。
打字错误。
这是一件愚蠢的事情。我的文件夹是 JSPyDB,使用 JSPYDB 时 url 是错误的。
打字错误。
我有一个 android 项目,其中有一个主要 activity。 我希望它访问我的本地主机服务器内的 JSP 页面。
我的电脑中有一个 tomcat 7,在 /webapps/ 中,我有一个名为 JSPyDB 的文件夹,其中存储了我的 ejemplo.jsp 页面。(我可以访问来自浏览器的 jsp 页面,所以我可以看到服务器运行完美)。
所以这是我的 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);
}
}
我得到的响应是 404...
我尝试了其他网址,例如我的电脑在我的网络 192.68.1.12 中的 IP 地址。但是还是404响应。
如果我连一个好的回复都得不到,我还缺少什么?
已编辑: 这是一件愚蠢的事情。我的文件夹是 JSPyDB,使用 JSPYDB 时 url 是错误的。 打字错误。
这是一件愚蠢的事情。我的文件夹是 JSPyDB,使用 JSPYDB 时 url 是错误的。
打字错误。