【发布时间】:2015-02-19 19:08:59
【问题描述】:
我有一个主要活动的 android 项目。 我希望它访问我的本地主机服务器内的 JSP 页面。
我的电脑中有一个 tomcat 7,在 /webapps/ 中有一个名为 JSPyDB 的文件夹,其中存储了我的 ejemlo.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 中尝试了其他 url,例如我的电脑的 ip 地址。但还是 404 响应。
如果我什至无法得到好的回应,我在这里错过了什么?
编辑: 这是一件愚蠢的事情。我的文件夹是 JSPyDB,使用 JSPYDB 的 url 是错误的。 错别字。
【问题讨论】:
-
thx 但建议在 Gingerbread 及更高版本中使用 HttpURLConnection,比 HttpClient 更好。
-
是的,我知道。这对你有帮助吗??
-
不,抱歉,链接中的代码看起来与我的相似,并且提出的解决方案使用了我不感兴趣的 Httpclient。无论如何谢谢:)