Android中的登录问题:字段正在空返回



我目前正在为我们的论文创建和Android应用程序。当我开始在登录方面遇到问题时,我将完成该应用程序。当我尝试烤面包时,这些字段正在接受输入,但是当我将数据发送到服务器时,请向我发送"字段为空"响应。我被困在这方面,因此任何帮助都将不胜感激。

这是我的JSONPARSER类:

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    // constructor
    public JSONParser() {
    }
    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {
        // Making HTTP request
        try {
            // check for request method
            if(method.equals("POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            } else if (method.equals("GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;
    }
}

这是我的登录外法函数:

class AttemptLogin extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    boolean failure = false;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();   
    }
    @Override
    protected String doInBackground(String... args) {
        int success;
        String emailaddress = EADD.getText().toString();
        String password = PASS.getText().toString();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("mem_email", emailaddress));
        params.add(new BasicNameValuePair("mem_password", password));
        Log.d("request!", "starting");
        JSONObject json = jsonParser.makeHttpRequest("http://builtcycles.com/built_mobile/login.php", "POST", params);
        // check your log for json response
        Log.d("Login attempt", json.toString());
        // json success tag
        try{
            success = json.getInt("success");
            if (success == 1) {
                String memberId = json.getString("id");
                String memberName = json.getString("uid");
                Log.d("Login Successful!", json.toString());
                pDialog.dismiss();
                intent = new Intent(Login.this, MainActivity.class);
                intent.putExtra("id", memberId);
                intent.putExtra("uid", memberName);
                startActivity(intent);
                finish();
                return json.getString("message");       
            } else {
                Log.d("Login Failure!", json.getString("message"));
                return json.getString("message");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(Login.this, file_url, Toast.LENGTH_SHORT).show();
        }
    }
}

这是我的登录php:

<?php
    $response = array();
    if (!empty($_POST)) {
        $email= $_POST['mem_email'];
        // include db connect class
        require_once __DIR__ . '/db_connect.php';
        // connecting to db
        $db = new DB_CONNECT();
        // mysql inserting a new row
        $result = mysql_query("SELECT * from tblmembers where mem_email='$email'");
        if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_array($result)) {
                $response["id"] = $row["mem_id"];
                $response["uid"] = $row["mem_fname"];
                if ($row["mem_active"]==1) {
                    if (md5($_POST['mem_password'])===$row['mem_password']) {
                        $response["success"] = 1;
                        $response["message"] = "Login successfully!";
                        echo json_encode($response);
                    } else {
                        $response["success"] = 0;
                        $response["message"] = "Invalid email or password";
                        echo json_encode($response);                             
                    }
                } else {
                    $response["success"] = 3;
                    $response["message"] = "Check your email for verification! Thanks.";
                    echo json_encode($response); 
                }
            }
        } else {
            // no products found
            $response["success"] = 4;
            $response["message"] = "No user found";
            // echo no users JSON
            echo json_encode($response);
        }   
    } else {
        $response["success"] = 4;
        $response["message"] = "Fields are empty.";
        // echo no users JSON
        echo json_encode($response);
    }
?>

我已经更改了方法==" post"/" get"到method.equals(" post")中。不过,我无法将数据发送到服务器。问题来自JSONPARSER类还是在异步的Doinbackground()中?

更改

中的字符串比较语句
makeHttpRequest()

来自

// check for request method
if(method == "POST"){
  ...
} else if (method == "GET") {

to

// check for request method
if(method.equals("POST")){
  ...
} else if (method.equals("GET")) {

最新更新