在我的数据库中,我有个人资料图片网址。我想得到个人资料图片的网址,连同用户的详细资料,并显示在我的应用程序。下面是我的JSON响应。

{"error":false,"userid":"102372118524277056034","uname":"Asesha G","ulocation":"Location Not Set"
,"ugend":"Not Set","uemail":"[email protected]","oarth":"Google","propic":"https:\/\/lh3.googleusercontent
.com\/-JHsviYEvBU4\/AAAAAAAAAAI\/AAAAAAAAAAA\/SiDYHY21SSA\/s64-c\/102372118524277056034.jpg"}

在我的回复中,URL是
propic":"https:\/\/lh3.googleusercontent
    .com\/-JHsviYEvBU4\/AAAAAAAAAAI\/AAAAAAAAAAA\/SiDYHY21SSA\/s64-c\/102372118524277056034.jpg

在我的活动中,我得到了除URL之外的所有用户详细信息。下面是我获取个人资料图片URL的数据类型。
private void SetsessionValues(final String userid) {

String tag_string_req = "fetch_login_data";

StringRequest strReq = new StringRequest(Request.Method.POST,
        AppURLs.fetch_data_URL, new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {

        try {
            JSONObject jObj = new JSONObject(response);
            boolean error = jObj.getBoolean("error");

            if (!error) {
                String userId = jObj.getString("userid");
                String uname = jObj.getString("uname");
                String ulocation = jObj.getString("ulocation");
                String ugend = jObj.getString("ugend");
                String uemail = jObj.getString("uemail");
                String oarth = jObj.getString("oarth");
                String profilepic = jObj.getString("propic");

                Log.e(TAG,"Profile Pic : "+profilepic);

                session.setLogin(true);
                session.setMember(userId, uname, ulocation, ugend, uemail, oarth, profilepic);
            } else {
                String errorMsg = jObj.getString("error_msg");
                Toast.makeText(getApplicationContext(),
                        errorMsg, Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getApplicationContext(),
                error.getMessage(), Toast.LENGTH_LONG).show();
    }
}) {

    @Override
    protected Map<String, String> getParams() {
        // Post params to login url
        Map<String, String> params = new HashMap<String, String>();
        params.put("tag", "login_data");
        params.put("username", userid);
        return params;
    }

};

// Adding request to  queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);


}

profilepic中获取空值
日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                       at libcore.net.UriCodec.encode(UriCodec.java:132)
                                                                       at java.net.URLEncoder.encode(URLEncoder.java:57)
                                                                       at com.android.volley.Request.encodeParameters(Request.java:484)
                                                                       at com.android.volley.Request.getBody(Request.java:470)
                                                                       at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253)
                                                                       at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227)
                                                                       at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
                                                                       at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
                                                                       at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

我的JSON php代码
if($tag == 'login_data'){

            $userid=$_POST['username'];

            //$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql=$dbh->prepare("SELECT * FROM member WHERE oauth_uid=?");
            $sql->bindValue(1,$userid);
            $sql->execute();

            if ($sql->rowCount() > 0) {
                // user stored successfully
                $userrow = $sql->fetch(PDO::FETCH_ASSOC);
                $response["error"] = FALSE;
                $response["userid"]= $userrow['oauth_uid'];
                $response["uname"]= ucwords(strtolower($userrow['mname']));
                $response["ulocation"]= $userrow['location'];
                $response["ugend"]= $userrow['gender'];
                $response["uemail"]= $userrow['email'];
                $response["oarth"]= $userrow['oauth_provider'];
                $response["propic"]= $userrow['propic'];

                header('Content-Type:Application/json');
                //$array[] = $response;
                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = TRUE;
                $response["error_msg"] = "Error occured in Getting Personal Data";
                echo json_encode($response);
            }
       }
}

最佳答案

要删除反斜杠,只需在String profilepic = jObj.getString("propic");之后的行下面添加:

profilepic = profilepic.replace("\\","");

10-08 04:41