【发布时间】:2017-07-07 23:12:13
【问题描述】:
请我已经解决了来自 stackoverflow 的所有问题,但这些问题不适用于我的问题。
但是当我尝试使用android 代码时,它不起作用。
我的示例 Android 代码在这里。
@Override
protected String doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
try {
URL url = new URL("sample url");
String postData = "key1=valu1&key2=valu2";
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(55000 /* milliseconds */);
conn.setConnectTimeout(55000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os));
writer.write(postData);
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
Log.e("response ",line);
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
} catch (Exception e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
}
}
}
}
以上java代码返回405错误代码。我也尝试过OkHttp。
请帮忙。
【问题讨论】: