我使用Retrofit2.0.0向rest api发送post请求。
private class AuthTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
Call<Auth> call = service.auth(params[0], params[1]);
try {
Auth authResponse = call.execute().body();
Log.i(AuthActivity.class.getName(), authResponse.public_key);
} catch (IOException ex) {
Log.e(AuthActivity.class.getName(), "Error.");
return "error";
}
return "success";
}
protected void onPostExecute(String result) {
Log.i(AuthTask.class.getName(), "Done");
}
}
public class Auth {
public String public_key;
}
public interface AuthService {
@FormUrlEncoded
@POST("auth")
Call<Auth> auth(@Field("username") String username, @Field("publickey") String publickey);
}
以下是请求的日志输出:
12-14 19:36:54.014 30318-30373/com.app D/OkHttp: --> POST /auth HTTP/1.1
12-14 19:36:54.014 30318-30373/com.app D/OkHttp: username=ghtbznz&publickey=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCedhOhP9MgYufDxPKH1DdtJiBBdBtbsUy8R7kg%0AWm1Edm7c3ThToMxMgjvOAsHn8rP1Ka1eVN34hC4HV2%2BoomRaWH25WbunN9ZVRqBnowsTOd40eEKh%0A6dsO8Cl3u65VsArDyZyEQa7Ofx29i2juOpWRG%2F6tp9FVnJzZt5dBDkWOKwIDAQAB%0A
12-14 19:36:54.014 30318-30373/com.app D/OkHttp: --> END POST (256-byte body)
下面是响应的日志输出:
12-14 19:42:20.358 2549-2804/com.app D/OkHttp: <-- HTTP/1.1 200 OK (1790ms)
12-14 19:42:20.358 2549-2804/com.app D/OkHttp: Date: Mon, 14 Dec 2015 18:42:16 GMT
12-14 19:42:20.358 2549-2804/com.app D/OkHttp: Server: Apache/2.2.22 (Debian)
12-14 19:42:20.358 2549-2804/com.app D/OkHttp: Vary: Accept-Encoding
12-14 19:42:20.358 2549-2804/com.app D/OkHttp: Keep-Alive: timeout=5, max=99
12-14 19:42:20.366 2549-2804/com.app D/OkHttp: Connection: Keep-Alive
12-14 19:42:20.366 2549-2804/com.app D/OkHttp: Content-Type: text/html
12-14 19:42:20.366 2549-2804/com.app D/OkHttp: OkHttp-Selected-Protocol: http/1.1
12-14 19:42:20.366 2549-2804/com.app D/OkHttp: OkHttp-Sent-Millis: 1450118540088
12-14 19:42:20.366 2549-2804/com.app D/OkHttp: OkHttp-Received-Millis: 1450118540358
12-14 19:42:20.373 2549-2804/com.app D/OkHttp:
12-14 19:42:20.373 2549-2804/com.app D/OkHttp:
12-14 19:42:20.373 2549-2804/com.app D/OkHttp:
12-14 19:42:20.373 2549-2804/com.app D/OkHttp: array(0) {
12-14 19:42:20.373 2549-2804/com.app D/OkHttp: }
12-14 19:42:20.373 2549-2804/com.app D/OkHttp: Notice: Undefined index: username in /home/auth/index.php on line 45
12-14 19:42:20.373 2549-2804/com.app D/OkHttp: Notice: Undefined index: publickey in /home/auth/index.php on line 46
12-14 19:42:20.373 2549-2804/com.app D/OkHttp: <-- END HTTP (251-byte body)
在我的php文件中,这是我检索post参数的方式:
$user = $_POST["username"];
$pk = $_POST["publickey"];
奇怪的是,当我使用curl发送post请求时,一切都正常。所以我的问题是为什么我不能发送适当的后参数与改造?
最佳答案:
改造可能与角度类似,因为数据是以mime类型“application/json”发布的,而不是“application/x-www-form-urlencoded”或“multipart/form data”。如果是“application/json”,php将不会将数据解析成$post。所以,你得自己动手。我做了这样的事…
if (isset($_SERVER['CONTENT_TYPE'])
and stripos($_SERVER['CONTENT_TYPE'], 'application/json') !== false
) {
$jsonEncoded = file_get_contents('php://input');
$jsonDecoded = json_decode($jsonEncoded, true);
if (is_array($jsonDecoded)) {
foreach ($jsonDecoded as $varName => $varValue) {
$_POST[$varName] = $varValue;
}
}
}