如何在android java中发出相同的cURL请求?

如何在android java中发出相同的cURL请求?,java,android,curl,Java,Android,Curl,我无法理解以下关于卷曲的要求: curl -X POST -u "client_id:secret" \ https://bitbucket.org/site/oauth2/access_token -d grant_type=password \ -d username={username} -d password={password} 如何在java(Android)中发出相同的请求? 现在,我试着这样做: String auth = vcs.getKey() + ":"

我无法理解以下关于卷曲的要求:

    curl -X POST -u "client_id:secret" \
  https://bitbucket.org/site/oauth2/access_token -d grant_type=password \
  -d username={username} -d password={password}
如何在java(Android)中发出相同的请求? 现在,我试着这样做:

String auth = vcs.getKey() + ":" + vcs.getSecret();
byte[] authEncBytes = Base64.encode(auth.getBytes(), Base64.DEFAULT);
String authStringEnc = new String(authEncBytes);
URL url = new URL("https://bitbucket.org/site/oauth2/access_token");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestMethod("POST");
connection.setRequestProperty("grant_type", "password");
connection.setRequestProperty("username", mLogin);
connection.setRequestProperty("password", mPassword);

但是它不起作用。

您正在通过请求头传递post参数。拆下最后三个并更换为以下部件:

String params = "grant_type=password&username=xyz&password="+ java.net.URLEncoder.encode("urp&^!ass", "UTF-8");
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
writer.write(params);
writer.flush();
writer.close();
os.close();
int responseCode = connection.getResponseCode();

谢谢你的帮助!我试试看!