Android的代碼得到錯誤的登錄是這裏使用web服務
public class NewLogin extends ActionBarActivity {
private EditText editTextUserName;
private EditText editTextPassword;
public static final String USER_NAME = "USERNAME";
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
editTextUserName = (EditText) findViewById(R.id.et_email);
editTextPassword = (EditText) findViewById(R.id.et_password);
}
public void invokeLogin(View view){
username = editTextUserName.getText().toString();
password = editTextPassword.getText().toString();
login(username,password);
}
private void login(final String username, String password) {
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(NewLogin.this, "Please wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
String uname = params[0];
String pass = params[1];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myusername", uname));
nameValuePairs.add(new BasicNameValuePair("mypassword", pass));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"www.sample.com/home_webservice.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
if(s.equalsIgnoreCase("success")){
Intent intent = new Intent(NewLogin.this, Sample.class);
/* intent.putExtra(USER_NAME, username);
finish();*/
startActivity(intent);
}else {
Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
}
}
}
LoginAsync la = new LoginAsync();
la.execute(username, password);
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
}
JSON代碼是在這裏
<?php
require '../db_connect.php';
if (isset($_GET['username']) && isset($_GET['password']))
{
$myusername = $_GET['username'];
$mypassword = $_GET['password'];
$sql = "SELECT * FROM user_registration WHERE (username = '$myusername' or email = '$myusername' or phone = '$myusername')";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>0){
$response["VerifiedMember"] = array();
$row = mysqli_fetch_array($result);
$VerifiedMember = array();
$id=$row['id'];
$phone=$row['phone'];
$reg_type=$row['register_type'];
$stored_salt = $row['salt'];
$stored_hash = $row['hashed_password'];
$check_pass = $stored_salt . $mypassword;
$check_hash = hash('sha512',$check_pass);
if($check_hash == $stored_hash){
$VerifiedMember['user_id'] = $id;
$VerifiedMember['first_name']=$row['first_name'];
$VerifiedMember['phone']=$row['phone'];
array_push($response["VerifiedMember"], $VerifiedMember);
if(!empty($phone)&& $reg_type==1){
$sql="select * from user_otps where user_id='".$id."'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_array($result);
if($row['verified']==0)
{
//no product found
$response["success"] = 0;
$response["message"] = "failure";
// echo no users JSON
echo json_encode($response);
}
else
{
//no product found
$response["success"] = 1;
$response["message"] = "success";
// echo no users JSON
echo json_encode($response);
}
}
}
else{
//no product found
$response["success"] = 1;
$response["message"] = "success";
// echo no users JSON
echo json_encode($response);
}
//echo json_encode($response);
}
else{
//no product found
$response["success"] = 0;
$response["message"] = "invalid";
// echo no users JSON
echo json_encode($response);
}
}
else{
//no product found
$response["success"] = 0;
$response["message"] = "invalid";
// echo no users JSON
echo json_encode($response);
}
}
?>
這裏即時得到無效的用戶名和密碼,請幫我從Android中的JSON文件中獲取用戶的詳細信息 如何解決這個問題誰能告訴我?? 在這裏我得到無效的用戶名和密碼,請幫我從json文件中獲取用戶詳細信息在android 如何解決這個任何人都可以告訴我??
你的API是一個GET請求,但要創建從Android的POST請求。你傳遞給php腳本的鍵也是錯誤的。你的MySQL查詢沒有任何意義。 –
幫助我如何使用相同的php文件創建GET請求@KNeerajLal –
'DefaultHttpClient'已棄用。看看這個[link](https://developer.android.com/training/volley/simple.html)。 –