任何人都可以帮我解决这个代码块吗?
它在邮差上使用相同的参数。
参数为true标头为true。但我想我错过了配置。
public String readJSONFeed() throws Exception {
errorMessage = "";
URL url;
String response = "";
try {
url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", contentType);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
if (contentType == "application/x-www-form-urlencoded") {
writer.write(getPostDataString( params));
}
else {
conn.setRequestProperty("Authorization", "Bearer " + SharedPrefManager.getInstance(context).getString(
SharedPrefManager.PREF_KEY_UserToken, ""
));
conn.setRequestProperty("WWW-Authenticate", "Bearer");
writer.write(new JSONObject(params).toString());
}
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String readJSONFeed() throws Exception {
errorMessage = "";
URL url;
String response = "";
try {
url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", contentType);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
if (contentType == "application/x-www-form-urlencoded") {
writer.write(getPostDataString( params));
}
else {
conn.setRequestProperty("Authorization", "Bearer " + SharedPrefManager.getInstance(context).getString(
SharedPrefManager.PREF_KEY_UserToken, ""
));
conn.setRequestProperty("WWW-Authenticate", "Bearer");
writer.write(new JSONObject(params).toString());
}
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String readJSONFeed() throws Exception {
errorMessage = "";
URL url;
String response = "";
try {
url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", contentType);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
if (contentType == "application/x-www-form-urlencoded") {
writer.write(getPostDataString( params));
}
else {
conn.setRequestProperty("Authorization", "Bearer " + SharedPrefManager.getInstance(context).getString(
SharedPrefManager.PREF_KEY_UserToken, ""
));
conn.setRequestProperty("WWW-Authenticate", "Bearer");
writer.write(new JSONObject(params).toString());
}
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public String readJSONFeed() throws Exception {
errorMessage = "";
URL url;
String response = "";
try {
url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", contentType);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
if (contentType == "application/x-www-form-urlencoded") {
writer.write(getPostDataString( params));
}
else {
conn.setRequestProperty("Authorization", "Bearer " + SharedPrefManager.getInstance(context).getString(
SharedPrefManager.PREF_KEY_UserToken, ""
));
conn.setRequestProperty("WWW-Authenticate", "Bearer");
writer.write(new JSONObject(params).toString());
}
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}