【发布时间】:2019-09-12 02:48:10
【问题描述】:
我有一个项目,其中有一些 url,每个都指向一个端点。我已经通过一个带有 JSON 的 post 请求连接到这些端点,我必须在其中插入一个参数(即:“email”:“mail@etc.com”)以获得一个我将放入正文的令牌我要连接的端点的下一个请求。
我尝试使用 addRequestProperty() 和 setRequestProperty() ,但我不知道出了什么问题。在日志中,我在尝试发出 http 请求时出现内部服务器错误(代码 500)。
我有一个端点,我不必向其传递任何参数并且工作正常,提供一个“东西”列表,每个端点的 JSON 结果中都有一个 id。然后我必须获取每个 id,所以当我从屏幕上的列表中单击“东西”时,另一个端点被称为在另一个活动中为我提供该“东西”详细信息的结果 - 对于这个端点,我需要传递任何项目我单击从早期 JSON 结果中获取的特定 ID。
私有静态字符串 makeHttpRequestGetUser(URL url) 抛出 IOException {
String jsonResponse = "";
if(url == null)
return jsonResponse;
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
//urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.addRequestProperty("email", "t1@gmail.com");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
if(urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(TAG, "Error response code in GetUser request: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(TAG, "Problem retrieving the "stuff" JSON result.", e);
} finally {
if(urlConnection != null)
urlConnection.disconnect();
if(inputStream != null)
inputStream.close();
}
return jsonResponse;
}
私有静态字符串 extractTokenFromJson(String spotJSON) {
if(TextUtils.isEmpty(spotJSON))
return null;
String tokenValue = "";
try {
JSONObject baseJsonResponse = new JSONObject(spotJSON);
JSONObject result = baseJsonResponse.getJSONObject("result");
tokenValue = result.getString("token");
} catch (JSONException e) {
Log.e(TAG, "Problem parsing the token", e);
}
return tokenValue;
}
【问题讨论】:
标签: android json post httprequest internal-server-error