【发布时间】:2018-07-30 18:13:39
【问题描述】:
我正在尝试使用 x-www-form-urlencoded 在正文中发布数据,但我失败了
private void sendData(final String toekn) {
RequestParams params = new RequestParams();
params.put("_token", toekn);
StringEntity entity=null;
try {
entity = new StringEntity(params.toString());
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
PropClient.post(getBaseContext(), "", entity, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Log.e("see", response.toString());
Toast.makeText(SplashActivity.this, response.toString()+"", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Pull out the first event on the public timeline
// Do something with the response
}
@Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
}
上面是我尝试过但总是失败的代码.. api 在邮递员中完美运行我附上了图片以了解参数..
静态类
公共类 PropClient { 私有静态最终字符串 BASE_URL = "";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(Context context, String url, StringEntity entity, AsyncHttpResponseHandler responseHandler) {
// client.addHeader("Accept", "application/json");
client.addHeader("Content-Type", "application/x-www-form-urlencoded");
entity.setContentType("application/json");
client.setUserAgent("android");
client.post(context, getAbsoluteUrl(url), entity, "application/json", responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
String url = BASE_URL + relativeUrl;
return url;
}
}
【问题讨论】: