0

我正在尝试将以下 php 代码迁移到 Android。我已经搜索并尝试了在这里和其他地方发布的几种方法,但没有运气。PHP 代码

    $url ='url';
    $headers = array('Content-Type:application/json');
    $params = json_encode(array(
                               'access_token' => array(
                               'client_id' =>   'clientID',
                                'username' => 'userName',
                                'password' => 'passWord'
                               )
           ));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec($ch);
    echo $result;

这是我尝试过的Android代码

    HttpParams httpParams = new BasicHttpParams();
    HttpClient client = new DefaultHttpClient(httpParams);
    HttpPost httpost = new HttpPost("url");
    httpost.setHeader("Content-Type", "application/json");
    System.out.println("2");
    JSONObject data = new JSONObject();
    JSONObject token = new JSONObject();
    JSONArray accessToken = new JSONArray();
    HttpResponse response = null;
    try {
        token.put("client_id","clientID");
        token.put("username","userName");
        token.put("password","passWord");
        accessToken.put(token);
        data.put("access_token", accessToken);
        System.out.println(data);
        StringEntity se = new StringEntity(data.toString());
        httpost.setEntity(se);
        try {
            response = client.execute(httpost);
            System.out.println(se);
            System.out.println(response.getStatusLine().toString());

这是 curl 命令行

        curl -iH 'Content-Type: application/json' --request POST -d '{"access_token":  {"client_id":
       "clientID","username": "userName","password": "passWord"}}'url

非常感谢任何帮助或指导。

4

0 回答 0