org.json.JSONException:没有值

时间:2016-01-18 03:25:56

标签: java android json

*编辑:

所以我问过PHP Guy,是的,他忘了在代码中写"fullname"字符串。抱歉我的错误。

我是Java Android的新手,因此对这段代码感到十分困惑。我想要做的就是获取价值,但它总是在Android Studio中发现此错误:

W/System.err: org.json.JSONException: No value for fullname
W/System.err:     at org.json.JSONObject.get(JSONObject.java:355)
W/System.err:     at org.json.JSONObject.getString(JSONObject.java:515)
W/System.err:     at com.skafen.bayarin.ServerRequests$fetchUserDataAsyncTask.doInBackground(ServerRequests.java:130)
W/System.err:     at com.skafen.bayarin.ServerRequests$fetchUserDataAsyncTask.doInBackground(ServerRequests.java:93)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:841)

java是:

public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> { //line #93

        User user;
        GetUserCallback userCallback;

        public fetchUserDataAsyncTask(User user, GetUserCallback userCallback) {
            this.user = user;
            this.userCallback = userCallback;

        }

        @Override
        protected User doInBackground(Void... params) {
            ArrayList<NameValuePair> dataToSend = new ArrayList<>();
            dataToSend.add(new BasicNameValuePair("username", user.username));
            dataToSend.add(new BasicNameValuePair("password", user.password));

            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

            HttpClient client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");


            User returnedUser = null;
            try {
                post.setEntity(new UrlEncodedFormEntity(dataToSend));
                HttpResponse httpResponse =  client.execute(post);

                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);
                JSONObject jObject = new JSONObject(result);

                if (jObject.length() == 0 ){
                    returnedUser = null;
                }else {
                    String fullname = jObject.getString("fullname"); //line #130
                    int birthday = jObject.getInt("birthday");
                    String email = jObject.getString("email");

                    returnedUser = new User(fullname, birthday, email, user.username, user.password);
                }

            }catch (Exception e){
                e.printStackTrace();
            }

            return returnedUser;
        }

        @Override
        protected void onPostExecute(User returnedUser) {
            progressDialog.dismiss();
            userCallback.done(returnedUser);
            super.onPostExecute(returnedUser);
        }
    }

感谢您的关注,如果我发布一个包含相同主题的新帖子,我很抱歉,因为我是新来的。

感谢。

2 个答案:

答案 0 :(得分:3)

使用

jsonObject.optString("SELECTOR"); 

insted of

jsonObject.getString("SELECTOR");

答案 1 :(得分:0)

因为您正在尝试获取名为“fullname”的值,该值在HttpResponse中甚至不存在。确保响应的json包含一个名为“fullname”的密钥(告诉服务器人员),然后再试一次。