本文介绍了org.json.JSONException:无值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*

所以我问过PHP Guy,是的,他忘了在他的代码中写"fullname"字符串.对不起,我的错.

So I've asked the PHP Guy, and yes he forgot to write the "fullname" string in his code. Sorry for my mistake.

我是Java Android的新手,因此对此代码有点困惑.我想做的就是获取值,但是它总是使Android Studio中的此错误消失:

I'm new in Java Android, so a lil bit confuse about this code. What I'm trying to do is get the value, but it always whot this error in 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);
        }
    }

感谢您的关注,如果我发布的主题相同的新话题,很抱歉,因为我是新来的.

Thanks for the attention, and sorry if I post a new thread with same topic because I'm new here.

谢谢.

推荐答案

因为您正试图获取名为全名"的值,而该值甚至在HttpResponse中都不存在.确保响应的json包含一个名为全名"(告诉服务器人员)的密钥,然后重试.

Because you are trying to get value called "fullname" that doesn't even exist in HttpResponse. Make sure the responsed json contains a key called "fullname" (telling server guy) and try again.

这篇关于org.json.JSONException:无值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:22