Android:http登录没有预期效果
我似乎无法让我的登录页面正常工作,它一直告诉我我的用户名/密码不正确,而我显然知道它们不正确。所以我开始调试并打开log.v,我发现了一些有趣的东西。查找“//我也在这样做,您的代码似乎是正确的。问题可能出在您的服务器上。您得到的响应看起来像是重定向。(可能您的服务器希望请求中有一些标题或类似的内容?)@GabrielPetrovay很可能是这样的,我没有设计我试图访问的网站、服务器或php文件。但是我确实可以完全访问这些文件。我在哪里可以找到这些信息?在login.php?@GabrielPetrovay中查看login.php文件、function.php和server.php后,我没有发现任何请求任何其他信息的内容韩:用户名/密码。叹息这就是我选择的生活。哈哈。尝试向我的服务器发出请求:发布“电子邮件”和“密码”(不要发送真实数据)很抱歉:请尝试Android:http登录没有预期效果,android,Android,我似乎无法让我的登录页面正常工作,它一直告诉我我的用户名/密码不正确,而我显然知道它们不正确。所以我开始调试并打开log.v,我发现了一些有趣的东西。查找“//我也在这样做,您的代码似乎是正确的。问题可能出在您的服务器上。您得到的响应看起来像是重定向。(可能您的服务器希望请求中有一些标题或类似的内容?)@GabrielPetrovay很可能是这样的,我没有设计我试图访问的网站、服务器或php文件。但是我确实可以完全访问这些文件。我在哪里可以找到这些信息?在login.php?@GabrielPe
/m/login
以获得移动版JSON。在任何情况下,前面的示例也表明,这里的问题是服务器,而不是Android应用程序中的java代码。
class LogMeIn extends AsyncTask<String, Void, String> {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.fakesite.com/login.php");
protected String doInBackground(String... urls) {
try {
username = un.getText().toString();
password = pw.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs
.add(new BasicNameValuePair("username", username));
nameValuePairs
.add(new BasicNameValuePair("password", password));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
String res = inputStream(response.getEntity().getContent())
.toString();
Log.v("RESPONSE", res); // <--- THIS LINE!
// if username and password are valid, launch main activity
if (res.toString() == "1") {
Intent logIn = new Intent(getApplicationContext(), Main.class);
startActivity(logIn);
}
// send the user a message saying the login failed
else {
runOnUiThread(new Runnable() {
public void run() {
pw.setText("");
fail.setText(R.string.fail);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
}
}
private StringBuilder inputStream(InputStream input) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader read = new BufferedReader(new InputStreamReader(input));
try {
while ((line = read.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return total;
}
05-18 15:41:27.620: V/RESPONSE(27641): <!--<script>window.location.href="";</script>--> <!-- <meta http-equiv="refresh" content="0;url="> -->