Last active
May 9, 2018 13:17
-
-
Save Sankame/848d54b5e12f210811db5b80f8512553 to your computer and use it in GitHub Desktop.
サーバーからのレスポンスデータが多いとエラーになる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public JSONObject post(String urlString) throws Exception{ | |
URL url = new URL(urlString); | |
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); | |
con.setConnectTimeout(Const.HTTP_CONNECT_TIMEOUT); | |
con.setReadTimeout(Const.HTTP_READ_TIMEOUT); | |
con.setUseCaches(false); | |
con.setDoInput(true); | |
con.setDoOutput(true); | |
con.setChunkedStreamingMode(0); | |
con.connect(); | |
OutputStream out = con.getOutputStream(); | |
out.write( this.values.getBytes("UTF-8") ); | |
out.flush(); | |
out.close(); | |
final int status = con.getResponseCode(); | |
if (status != HttpURLConnection.HTTP_OK) { | |
con.disconnect(); | |
throw new Exception("通信に失敗しました\nお手数ですが、しばらくしてから再度お試し下さい"); | |
} | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(con.getInputStream()) | |
); | |
//(1)コネクション切断 | |
con.disconnect(); | |
StringBuilder builder = new StringBuilder(); | |
String body; | |
while ((body = reader.readLine()) != null) { | |
builder.append(body); | |
} | |
//(2)コネクション切断 | |
//con.disconnect(); | |
String jsonData = builder.toString(); | |
return new JSONObject(jsonData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment