dongyashun2559 2011-07-03 16:44
浏览 29
已采纳

无法验证

At this time, I want user to enter his/her username and password(Data is mocked @MySQL) But it does not work.

My file.php :

<?php
    include("ConnectDatabase.php");
    $Username = $_POST['Username'];
    $Password = $_POST['Password'];

    $q = mysql_query("SELECT Username, Password FROM Users
                    where Username = '".$Username."' and
                    Password = '".$Password."'");

    if(mysql_num_rows($q) > 0){
        print json_encode($q);
    }else{
        print "1";
    }
     mysql_close();
   ?>

My java file :

public class Authentication extends Activity implements OnClickListener,
        OnKeyListener, OnCheckedChangeListener {

    /** Called when the activity is first created. */

    ArrayList<NameValuePair> authentication;
    String passIn, userIn, result;
    EditText username, password;
    CheckBox remember;
    Button b_login;
    InputMethodManager inputManager;
    InputStream is;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // userIn = username.getText().toString();
        // passIn = password.getText().toString();
        username = (EditText) findViewById(R.id.usrname);
        password = (EditText) findViewById(R.id.password);
        inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        remember = (CheckBox) findViewById(R.id.remember);
        remember.setOnCheckedChangeListener(this);
        b_login = (Button) findViewById(R.id.login);
        b_login.setOnClickListener(this);
        username = (EditText) findViewById(R.id.usrname);
        username.setOnKeyListener(this);
        password = (EditText) findViewById(R.id.password);
        password.setOnKeyListener(this);

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.login:
            while (true) {
                userIn = username.getText().toString();
                passIn = password.getText().toString();
                try {
                    sendAuthenticationData(userIn, passIn);
                    break;
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (WrongInputException e) {
                    Toast.makeText(Authentication.this, "Invalid username or password", Toast.LENGTH_LONG);

                }
//              break;
            }
            Intent intent = new Intent(this, ApplicationMenus.class);
            this.startActivity(intent);
            clearText(username,password);

            break;
        }
    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            // Perform action on key press

            inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
            return true;
        }
        return false;
    }


    public void clearText(EditText usr, EditText pass) {
        usr.setText("");
        pass.setText("");
    }

    public void sendAuthenticationData(String username, String password)
            throws ClientProtocolException, IOException, WrongInputException {

        authentication = new ArrayList<NameValuePair>();
        authentication.add(new BasicNameValuePair("Username", userIn));
        authentication.add(new BasicNameValuePair("Password", passIn));
        this.sendData(authentication);
    }

    public void sendData(ArrayList<NameValuePair> data)
            throws ClientProtocolException, IOException, WrongInputException {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "path/Authentication.php"); // I use real path here
        httppost.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
//      is = entity.getContent();
        String temp = EntityUtils.toString(entity);

        if (temp.equals("1")) {
            throw new WrongInputException();
        }

    }}

This is my first time to write code that connect with PHP server, and JSON. I pretty confuse with JSON a lot, though I tried to follow many samples. Appreciate for any help

  • 写回答

1条回答 默认 最新

  • douxiyi2418 2011-07-03 16:56
    关注

    The first thing you forgot is mysql_real_escape_string for the incoming variables:

    $Username = $_POST['Username'];
    

    And your second problem is likely this:

       if (mysql_num_rows($q) > 0) {
            print json_encode($q);
    

    The $q variable is a mysql_ result handle. It cannot be JSON-represented. You probably wanted to use:

            print json_encode(mysql_fetch_assoc($q));
    

    So you'll get at least a result array/object with username/password there. Maybe it would be easier if you just send another simple numeric result back - print "2"; or something.

    Also have you checked if you run PHP 5.2 or later, so that json_encode is certainly available?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统