我已经在棉花糖中创建并测试了一个登录表单,我收到了成功消息。
但是当我尝试在 kitkat 操作系统中运行相同的应用程序时,它会给出错误“登录错误”,我从数据库返回该错误。 (php)
知道为什么会发生这样的事情吗?
我看到两个操作系统中的密码字段都不同。
即,当我在棉花糖密码字段中输入时,仅显示点,但在 kitkat 操作系统中,按下的键会显示一秒钟。这是错误吗?
在这种情况下,我尝试了操作系统中的注册表单(其中包括密码字段)并且工作正常。
d_login.php
<?php
$host='mysql7.000webhost.com';
$uname='a5502952_dds';
$pwd='000dds';
$db="a5502952_dds";
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$user = $_POST['user'];
$pass = $_POST['pass'];
$r=mysql_query("SELECT Count(password) as c,role,username from login WHERE username='$user'and password='$pass' and approve='approve'");
while($row=mysql_fetch_array($r))
{
$flag[role]=$row['role'];
$count=$row['c'];
$flag[user]=$row['username'];
}
if($count==1){
print(json_encode($flag));
}
else{
$flag[role]="5";
$flag[user]="5";
print(json_encode($flag));
}
mysql_close($con);
?>
public class LoginActivity extends AppCompatActivity {
EditText ed1,ed2;
Button btn1,btn2;
String useracc=null;
//Fetch//
String role=null;
String approve=null;
InputStream is=null;
String result2=null;
String line=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ed1=(EditText)findViewById(R.id.user);
ed2=(EditText)findViewById(R.id.pass);
btn1=(Button)findViewById(R.id.sign);
btn2=(Button)findViewById(R.id.reg);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (ed1.getText().toString().length() == 0 || ed1.getText().toString().length() < 3) {
ed1.setError("Enter valid username");
return;
} else if (ed2.getText().toString().length() == 0 || ed2.getText().toString().length() < 3) {
ed2.setError("Enter valid password");
return;
}
else {
if (isNetworkAvailable()) {
Toast.makeText(getApplicationContext(), "Please wait", Toast.LENGTH_SHORT).show();
insert();
} else {
Toast.makeText(getApplicationContext(), "Please check the internet connection", Toast.LENGTH_LONG).show();
}
}
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent2);
}
});
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public void insert() {
String user = ed1.getText().toString();
String pass = ed2.getText().toString();
insertToDatabase(user,pass);
}
private void insertToDatabase(final String user,final String pass) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user", user));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
try
{
HttpClient httpclient2 = new DefaultHttpClient();
HttpPost httppost2 = new HttpPost("http://www.diamonddiscretions.tk/d_login.php");
httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response2 = httpclient2.execute(httppost2);
HttpEntity entity2 = response2.getEntity();
is = entity2.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address"+e.toString(),
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result2 = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result2);
role=String.valueOf(json_data.getInt("role"));
useracc=json_data.getString("user");
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(role=="0") {
Intent intent1 = new Intent(LoginActivity.this, MyProfile.class);
intent1.putExtra("Value", String.valueOf(useracc));
startActivity(intent1);
}
else if(role.toString()=="5" && useracc.toString()=="5") {
Toast.makeText(getApplicationContext(),"Incorrect Login", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Incorrect Login", Toast.LENGTH_LONG).show();
}
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(user, pass);
}
}
activity_login.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp">
<EditText
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="username"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/black"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText
android:id="@+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/black"
android:hint="password" />
</android.support.design.widget.TextInputLayout>
最佳答案
这是一个愚蠢的 java 错误。
我使用的是 if(useracc=="5") 这是错误的。我们应该始终使用 .equals 来比较字符串。
所以它总是返回到 else 并给出登录错误。
:D
关于php - 登录表单在 Marshmallow 中成功,但在 Kitkat os 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41320928/