JSONObject不是应该编码的吗?

时间:2017-06-10 05:34:40

标签: java php android android-studio

这是服务器端代码

<?php
require('connection.php');
require('functions.php');

$inputJSON = file_get_contents('php://input');
$aReuestData = json_decode( $inputJSON, TRUE ); //convert JSON into array

 $user_email = $aReuestData['user_email'];
 $user_password = $aReuestData['user_password'];
  $user_uniq = $aReuestData['user_uniq_id'];

if((($user_password !='') && ($user_email !=''))|| ($user_uniq!=''))
{
    $uname = $user_email;
    $pword = $user_password;
    $format ='json';    
    if(($user_password !='') && ($user_email !='')){
    echo $checkUser = checkLogin($uname,$pword);
    }
    else{
        $checkUser = checkLoginFacebook($user_uniq);
    }
    //print_r($checkUser);
    if($checkUser['id'] > 0)
    {
        $result = $checkUser; 
    }else{
        $result = "false";
    }
}else{

    $result = "Enter username and password";

}

$records = array("result"=> $result); 

echo $_REQUEST['jsoncallback'].  json_encode($records);

?>

我的登录活动代码是

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Login extends MainActivity {
    //private Button button;
    //private TextView welcome;
    //private EditText username;
    //private EditText password;
    //private JSONObject jsonObject;
    private RequestQueue requestQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Cache cache = new DiskBasedCache(getCacheDir(),1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        requestQueue= new RequestQueue(cache,network);
        final TextView send = (TextView)findViewById(R.id.send);
        final TextView hello = (TextView)findViewById(R.id.mess);
        EditText username = (EditText)findViewById(R.id.edituser);
        EditText password = (EditText)findViewById(R.id.editpass);
        Button button = (Button)findViewById(R.id.signin);
        final JSONObject jsonObject = new JSONObject();

        try {
           // jsonObject.put("Content-Type: ","application-json");
            jsonObject.put("user_email",username.getText().toString().trim());
            jsonObject.put("user_password",password.getText().toString().trim());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final HashMap<String,String> params = new HashMap<String, String>();
        params.put("user_email",username.getText().toString().trim());
        params.put("user_password",password.getText().toString().trim());
        String json = "{\"user_email\":\"ankur@gmail.com\",\"user_password\":\"123456\"} ";
        JSONObject json1 = new JSONObject();
        try {
            json1 = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        requestQueue.start();
        final String url = "http://demo4u.org/leaveapp/ws/login.php";
        send.setText(json1.toString());
        final JSONObject finalJson = json1;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(ApiMethods.login, finalJson,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                Toast.makeText(Login.this,"Passed",Toast.LENGTH_LONG).show();
                                hello.setText(response.toString());
                                Toast.makeText(Login.this,jsonObject.toString(),Toast.LENGTH_LONG).show();

                            }
                        }, new Response.ErrorListener() {
                   @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Login.this,"Error",Toast.LENGTH_LONG).show();
                    }
                }

                ){
                        @Override
                        public Map<String,String> getHeaders(){
                        HashMap<String,String> headers = new HashMap<>();
                        headers.put("Accept", "application/json");
                        headers.put("Content-Type","application/json");
                        return headers;

                    }

                };
                requestQueue.add(jsonObjectRequest);
            }
        });





        }



    }

我唯一得到的就是

$result = "Enter username and password";
我做错了什么? 我要包含JSON标题还是什么???

请详细查看,因为我是新手......

输入

{"user_email":"ankur@gmail.com","user_password":"123456"}

和服务器响应

Array{"result":{"id":"1","name":"ankur","email":"ankur@gmail.com","address":"b-block","designation":"devloper","department":"development","balanceleave":"5"}}

登录php服务器是:http://demo4u.org/leaveapp/ws/login.php

1 个答案:

答案 0 :(得分:2)

尝试通过覆盖getHeaders()

中的JsonObjectRequest方法来设置标头
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(...your arguments here) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headerParameters = new HashMap<>();
                    headerParameters.put("Accept", "application/json");
                    headerParameters.put("Content-Type", "application/json");
                    return headerParameters;
                }
            };