从php发送两个json_encode数组到

时间:2017-02-26 18:59:22

标签: php android arrays json

我正在尝试将两个数组从PHP发送到android,但是我在android中遇到了json错误" json错误:没有值访问"

以下是我的PHP

function mylogin($data){ 
    $db = new DB_Functions(); global $response;
    if (isset($data['email']) && isset($data['password'])) {
      $email = $data['email'];
      $password = $data['password'];
      $user = $db->getUserByEmailAndPassword($email, $password);
      if ($user != false) { 
         $response["userError"] = FALSE;
         $response["uid"] = $user["unique_id"];
         $response["user"]["name"] = $user["name"];
         $response["user"]["email"] = $user["email"];
         $response["user"]["created_at"] = $user["created_at"];
         $response["user"]["updated_at"] = $user["updated_at"];
         echo json_encode($response);
         echo jp();
       } else {
          $response["error"] = TRUE;
          $response["error_msg"] = "Login credentials are wrong. Please try again!";
          echo json_encode($response); 
          }
      }
 }

function jp(){ 
    $filename = "visited.xml";
    $eachRowValue = array("error" => FALSE);
    $visited = array();
    $jsonData = array();
    $eachCellValue = array();
    $jsonTempData  = array();
    $output = "";
    if (file_exists($filename))
    {
        global $eachRowValue;
        $dom = new DOMDocument(); 
        $dom->load($filename);
        $rows = $dom->getElementsByTagName( 'Row' );
        $counter = 0;
        foreach ($rows as $row)
        {
            $i = 0;
            $cells = $row->getElementsByTagName( 'Cell' );
                foreach( $cells as $cell )
                { 
                    $eachCellValue[$i] = $cell->nodeValue;
                    $i++;
                }
                $jsonTempData['oid']=$eachCellValue[2];
                $jsonTempData['name']= $eachCellValue[3];
                $jsonTempData['geox']=$eachCellValue[4];
                $jsonTempData['geoy']=$eachCellValue[5];
                $jsonData[$counter]=json_encode($jsonTempData);
                $counter++;

        }
        $visited['visited']=$jsonData;
        return json_encode($visited);
    }
    else {
        return 0; }
}

下面是我的android java代码

private void checkLogin(final String email, final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_login";

    pDialog.setMessage("Logging in ...");
    showDialog();

    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.URL_LOGIN, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Login Response: " + response.toString());

            try {
                JSONObject jObj = new JSONObject(response);


                boolean userError = jObj.getBoolean("userError");
                // Check for error node in json
                if ( !userError)  {

                    session.setLogin(true);

                    // Now store the user in SQLite
                    String uid = jObj.getString("uid");
                    JSONObject user = jObj.getJSONObject("user");
                    String name = user.getString("name");
                    String email = user.getString("email");
                    String created_at = user.getString("created_at");
                    db.addUser(name, email, uid, created_at);
                    Toast.makeText(getApplicationContext(), "Login Successful ", Toast.LENGTH_LONG).show();

                    pDialog.setMessage("Download data ...");
                    JSONObject jObjj = new JSONObject(response);
                    JSONArray visited = jObjj.getJSONArray("visited");
                    Toast.makeText(getApplicationContext(), "Downloading Journey Plan ", Toast.LENGTH_LONG).show();
                    for (int i = 0; i < visited.length(); i++)
                    {
                        JSONObject jdata=visited.getJSONObject(i);
                        Integer oid = jdata.getInt("oid");
                        String oname = jdata.getString("name");
                        Double geox = jdata.getDouble("geox");
                        Double geoy = jdata.getDouble("geoy");
                        db.addVisited(oid, oname, geox,geoy);
                    }

                    // Launch main activity
                    Intent intent = new Intent(LoginActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("email", email);
            params.put("password", password);
            params.put("frmtype", "login");
            return params;
        }

    };

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

正如您所看到的,我正在尝试登录用户,一旦用户通过身份验证,我就会下载他访问过的地点。当我运行上面的代码时,用户已经过身份验证,但我没有得到他访问过的地方。我得到了json错误&#34;没有值访问&#34;

0 个答案:

没有答案