![](/img/trans.png)
[英]I'm trying to access to my localhost server from my Android Emulator and i'm getting an ECONNREFUSED error
[英]I get 404 response trying to access a jsp page in my localhost server from my Android app
我有一个主要活动的android项目。 我希望它访问我的localhost服务器中的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是错误的。
错字错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.