android:fields中的登录问题返回空

时间:2015-05-14 09:07:41

标签: php android json android-asynctask

我目前正在为我们的论文创建和安装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;
    }
}

这是我的登录asynctask:

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);
    }
?>

我改变了方法==&#34; POST&#34; /&#34; GET&#34;进入method.equals(&#34; POST&#34;)。但是,我无法将数据发送到服务器。问题来自jsonparser类,还是来自asynctask的doinbackground()?

1 个答案:

答案 0 :(得分:1)

更改

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

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

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