Skip to content

Commit

Permalink
add response body
Browse files Browse the repository at this point in the history
  • Loading branch information
massodasuki committed Dec 19, 2022
1 parent dd367eb commit 42450b4
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -162,11 +163,30 @@ private JSONObject parse(HttpURLConnection httpURLConnection) throws JSONExcepti
Log.d(TAG, String.format("code: %s - %s", httpURLConnection.getResponseCode(), httpURLConnection.getResponseMessage()));
response.put("statusCode", httpURLConnection.getResponseCode());
response.put("responseMessage", httpURLConnection.getResponseMessage());
response.put("responseBody", getResponseBody(httpURLConnection));
return response;
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, String.format("response: %s", stringBuilder));
return new JSONObject(String.format("{\"exception\":\"%s\"}", e.getMessage()));
}
}

public static String getResponseBody(HttpURLConnection conn) {
BufferedReader br = null;
StringBuilder body = null;
String line = "";
try {
br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
body = new StringBuilder();
while ((line = br.readLine()) != null)
body.append(line);
return body.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}


}

0 comments on commit 42450b4

Please sign in to comment.