【发布时间】:2017-03-28 16:14:41
【问题描述】:
所以我正在尝试通过 Xserve 连接到我们的数据库,此时我正在尝试访问用户的令牌。我使用了正确的用户名和密码以及上下文类型和授权类型;我知道这一点是因为我通过 google 的 postmaster 扩展尝试了相同的 POST 方法。无论出于何种原因,当我在 Android 上尝试相同的事情时,至少我认为是相同的,它给了我一个 400 响应代码并且不返回任何内容。
这是用于连接的代码:
private HttpURLConnection urlConnection;
@Override
protected Boolean doInBackground(Void... params) {
Boolean blnResult = false;
StringBuilder result = new StringBuilder();
JSONObject passing = new JSONObject();
try {
URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token");
// set up connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
urlConnection.setRequestMethod("POST");
urlConnection.connect();
// set up parameters to pass
passing.put("username", mEmail);
passing.put("password", mPassword);
passing.put("grant_type", "password");
// add parameters to connection
OutputStreamWriter wr= new OutputStreamWriter(urlConnection.getOutputStream());
wr.write(passing.toString());
// If request was good
if (urlConnection.getResponseCode() == 200) {
blnResult = true;
BufferedReader reader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
}
//JSONObject json = new JSONObject(builder.toString());
Log.v("Response Code", String.format("%d", urlConnection.getResponseCode()));
Log.v("Returned String", result.toString());
}catch( Exception e) {
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return blnResult;
}
我还没有将结果存储到 JSONObject 中,因为我稍后会使用它,但我希望通过“Log.v”得到某种输出。
有什么突出的吗?
【问题讨论】:
-
请分享您的邮递员请求和回复
-
grant_type = 密码
-
对不起,我试图回车的 cmets,这就是我试图在 android 中重新创建的内容
标签: java android httpurlconnection xserver