我是安卓新手,我在代码中使用O-Authentication 1.0。获取请求正在工作,但发布请求在我的代码中不起作用。当我签入邮递员时,我得到了响应,但在代码中没有得到请求。这是我的代码: 受保护的字符串 doInBackground(String...参数) {
try {
URL url = new URL(params[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestMethod("POST");
// Send the post body
if (this.postData != null) {
OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
writer.write(postData.toString());
writer.flush();
}
int statusCode = httpURLConnection.getResponseCode();
if (statusCode == 200) {
InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
} else {
// Status code is not 200
// Do something to handle the error
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
提前谢谢。
您可以将这些用于 rest api 请求。
http://square.github.io/retrofit/
http://square.github.io/okhttp/
它减少了您的代码长度和行数,并有效地使您的内容加载速度更快并节省带宽。它支持同步阻塞调用和带回调的异步调用。