简体   繁体   English

如何在Android中使用Java从php获取Json?

[英]How to get Json from php with java in android?

I want to go through json and check each name to see if what I type in edittext will match with what i have in my json from get_user.php... 我想遍历json并检查每个名称以查看我在edittext中键入的内容是否与get_user.php中json中的内容匹配...

//json
    class AttemptLogin extends AsyncTask<String, String, String> {


        JSONArray jArray = new JSONArray( );

        @Override protected void onPreExecute()
        { super.onPreExecute();
            pDialog = new ProgressDialog(LoginWithAccount.this);
            pDialog.setMessage("Attempting for login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show(); }
        @Override
        protected String doInBackground(String... params) {
            int success;
           //here i will get the name and password from my edittexts
            String username = EditName.getText().toString();
            String password = EditPassword.getText().toString();
            try {  
                List<NameValuePair> params2 = new ArrayList<NameValuePair>();

                params2.add(new BasicNameValuePair("nume", username));
                params2.add(new BasicNameValuePair("parola", password));
                Log.d("request!", "starting");
                JSONObject json = jsonParser.makeHttpRequest( LOGIN_URL, "POST", params2);
                Log.d("Login attempt", json.toString());
                success = json.getInt(TAG_SUCCESS);

                String nume_json, parola_json;

                if (success == 1)
                {
                    Log.d("Successfully Login!", json.toString());
                   // return json.getString(TAG_MESSAGE);
                    for(int i = 0 ; i<jArray.length() ;i++)
                    {Log.d("here1", json.toString());
                        json = jArray.getJSONObject(i);
                        nume_json = json.getString("nume");
                        parola_json = json.getString("parola");
                   //     Toast.makeText(LoginWithAccount.this,     nume_json, Toast.LENGTH_SHORT).show();
                        if ( EditName.getText().toString().matches(nume_json)&& EditPassword.getText().toString().matches(parola_json))
                        {  Log.d("yess", json.toString());
                            get_user_json = true;
                        }
                    }

                }
                else
                {
                  //  get_user_json = false;
                    return json.getString(TAG_MESSAGE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
         }
        protected void onPostExecute(String message)
        { pDialog.dismiss();
            if (message != null)
            { Toast.makeText(LoginWithAccount.this, message, Toast.LENGTH_LONG).show();
            }
        }
    }

What am I doing wrong? 我究竟做错了什么? I can't go through the json to check each name, my json has only names and passwords, but I can't check them. 我无法通过json检查每个名称,我的json只有名称和密码,但是我无法检查它们。

why are you not waiting for the complete result to be fetched from the url? 为什么不等待从URL提取完整结果? should not it be checked in onPostExecute? 是否应该在onPostExecute中对其进行检查? and why are you getting edittext values inside the doinbackground method? 以及为什么要在doinbackground方法中获取edittext值? the ui related things should be done in other places. ui相关的事情应该在其他地方完成。 Using a common public static variable to store them and then using them would be a better approach. 使用公用的公共静态变量来存储它们,然后再使用它们将是更好的方法。 Let's say you have an activity Login.java and you have an AsyncTask class LoginAsyncTask.java. 假设您有一个活动Login.java,并且有一个AsyncTask类LoginAsyncTask.java。 Now, get the values of edittexts(that is your id and password) in two variables. 现在,在两个变量中获取edittexts的值(即您的ID和密码)。 Now instantiate an object of the loginasynctask class and send the context, uid and pwd values in it's constructor. 现在实例化loginasynctask类的对象,并在其构造函数中发送上下文,uid和pwd值。 In onPreexecute of the asynctask, create your progressdialog. 在异步任务的onPreexecute中,创建进度对话框。 In doinbackground hit the url you need to hit, to get the values. 在doinbackground中,您需要点击网址以获取值。 Use connection timeout, socket timeout and general exception. 使用连接超时,套接字超时和一般异常。 Create another method for parsing the json return. 创建另一个方法来解析json返回。 Create a simple arraylist to store all the returned and parsed values. 创建一个简单的arraylist来存储所有返回和解析的值。 In onpostExecute call this json parsing method. 在onpostExecute中,调用此json解析方法。 Now, run a loop and check for the match. 现在,运行一个循环并检查匹配项。 Use an interface method for creating a proper callback to your activity. 使用接口方法为您的活动创建适当的回调。 If a match was found set a (maybe boolean or a string "success" or "failure")value to the parameter of the interface's method. 如果找到匹配项,则为接口方法的参数设置一个值(可能是布尔值或字符串“ success”或“ failure”)。 Implement the interface on the activity and in the overridden method check for the value came(that boolean or string). 在活动上实现接口,并在覆盖的方法中检查值come(该布尔值或字符串)。 Take action accordingly. 采取相应的措施。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM