Android HTTP请求始终返回代码404
我正在尝试实现一个简单的HTTP请求。该请求将验证来自服务器的用户名和密码。服务器配置为只返回两个错误代码,200表示已验证,404表示其他所有错误。我用eclipse进行了大量调试,发现我总是收到代码404。在发布这篇文章之前,我已经阅读了所有其他现有的问题Android HTTP请求始终返回代码404,android,Android,我正在尝试实现一个简单的HTTP请求。该请求将验证来自服务器的用户名和密码。服务器配置为只返回两个错误代码,200表示已验证,404表示其他所有错误。我用eclipse进行了大量调试,发现我总是收到代码404。在发布这篇文章之前,我已经阅读了所有其他现有的问题 private class loginTask extends AsyncTask<String, Integer, Boolean> { String check; @Override protect
private class loginTask extends AsyncTask<String, Integer, Boolean> {
String check;
@Override
protected Boolean doInBackground(String... arg0) {
("http://www.unite.upmc.edu/account/IPhoneLogin?username=username&password=password
String username = arg0[0];
String password = arg0[1];
URI uri = null;
try {
uri = new URI("http://unite.upmc.edu/account/IPhoneLogin?"+"username="+username+"&"+"password="+password);// I know the method is called IPhoneLogin, it's being used by both Android and IOS clients
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
HttpClient client = new DefaultHttpClient();
try {//Look here
HttpResponse response = client.execute(new HttpGet(uri));
String code = "" + response.getStatusLine().getStatusCode();
check = code;
if(code.equals("200")){
loggedIn = true;
}else{
//need to re-enter username and password
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
hasPin = false;
return true;
}
@Override
protected void onProgressUpdate(Integer... integers) {
setProgress(integers[0]);
}
@Override
protected void onPreExecute() {
ProgressDialog dialog = new ProgressDialog(UniteActivity.this);
dialog.setMessage("Logging in...." + check);
dialog.show();
}
我知道服务器已启动,密码和用户名正确,因为我已使用internet explorer正常登录过多次。我将EclipseIDE与Android SDK一起使用,我担心这可能是它提供的错误模拟器的产物。关于我做错了什么有什么想法吗?在doInBackground方法中尝试此代码以获得响应代码
url = new URL(mUrl);
Log.i(LOG_TAG, url+"");
HttpGet method= new HttpGet(new URI(mUrl));
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(mUrl));
HttpResponse response = client.execute(method);
responseCode = response.getStatusLine().getStatusCode();
希望这可以正常工作。404找不到。。。出现此错误的原因是URL错误,或者服务器操作系统设置不正确,请在emulator的web浏览器中检查您是否可以访问URL。IIRC,仿真器网络不必访问与本地网络相同的DNS服务器。例如,在我的工作机器上运行模拟器时,我无法通过web浏览器或任何应用程序访问我们的内部网站。由于我无法从我的计算机访问您的URL,因此可能会出现这种情况。