I can read full response body (big JSON data, more than 400,000 chars) few times, but after 5-6 times my response is not full.
Here's my code of getting response:
URL address = new URL("https://myurl.com/");
HttpURLConnection Connection = (HttpURLConnection)address.openConnection();
Connection.setRequestMethod("GET");
Connection.setRequestProperty("accept-language", "en-US");
Connection.setRequestProperty("user-agent", UserAgent);
Connection.setRequestProperty("cookie", cookies);
Connection.setUseCaches(false);
Connection.setDoInput(true);
Connection.setDoOutput(true);
if (Connection.getResponseCode() == HttpsURLConnection.HTTP_OK)
{
String Response = new String();
InputStream is = Connection.getInputStream();
int ch;
StringBuffer sb = new StringBuffer();
while (( ch = is.read()) != -1) {
sb.append((char) ch);
}
Response = sb.toString();
is.close();
}
In my original code after is.close(), it is just lot of JSON parsing from Response string
Related
I brief description of what I'm doing, I'm automating creating incidents in ServiceNow using API. I have tested with POSTMAN first and it works fine.
Now after I wrote the code, I try to post data, I got no respond from the server. what could be the problem?
if (method.equals("POST") )
{
url+= "api/now/table/incident";
}
else if (method.equals("GET")|| method.equals("PUT"))
{
url+= "api/now/table/incident/" + incident.getSys_id();
}
URL request_url = new URL(url);
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
connection = (HttpsURLConnection) request_url.openConnection();
// ((HttpsURLConnection) connection).setSSLSocketFactory(sslsocketfactory);
if (method.equals("POST") || method.equals("PUT"))
{
connection.setRequestProperty("Content-Type", "application/json");
}
else
{
connection.setRequestProperty("Accept", "application/json");
}
connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // this was suggested as solution, but doesn't work.
connection.setRequestMethod(method);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Authorization", "Basic "+encoded);
if (method.equals("POST") || method.equals("PUT"))
{
JSONObject json = new JSONObject();
json.put("short_description",incident.getShort_description());
json.put("description", incident.getDescription());
json.put("assignment_group",incident.getAssignment_group());
json.put("priority",incident.getPriority());
json.put("impact",incident.getImpact());
OutputStream os = connection.getOutputStream();
os.write(json.toJSONString().getBytes(StandardCharsets.UTF_8));
os.close();
}
// read the respond
httpsCode = connection.getResponseCode();
if (httpsCode == HttpsURLConnection.HTTP_OK || httpsCode == 201)
{
//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String lines = null;
StringBuilder stringBuilder = new StringBuilder();
while ((lines = bufferedReader.readLine()) != null)
{
stringBuilder.append(lines);
}
bufferedReader.close();
result = stringBuilder.toString();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(result);
String incident_number = json.get("incident_number").toString();
String sys_id = json.get("sys_id").toString();
incident.setIncident_number(incident_number);
incident.setSys_id(sys_id);
return null;
}
the error is related to timeout, a posted solution suggested to set up the header to a browser type. but that's also didn't work.
I have generated the access code by using https://login.mailchimp.com/oauth2/authorize API. But when I try to create the token using https://login.mailchimp.com/oauth2/token, I'm getting unicode result like this.
(?M?? ?0F?UJ?N?NQ? %`??'
"?????nb??f=?&9????i'f??]?~j*$??W??Reg??_T1-???;?oc)
qryStr = {"client_secret":"**********","grant_type":"authorization_code","redirect_uri":"https%3A%2F%2Flocalhost%3A9443%2Fverifymailchimp.sas","client_id":"********","code":"*************"}
HttpURLConnection connection = null;
try
{
URL reqURL = new URL("https://login.mailchimp.com/oauth2/token");
connection = (HttpURLConnection) reqURL.openConnection();
connection.setConnectTimeout(3000); // 3 seconds
connection.setReadTimeout(5000); // 5 seconds
connection.setUseCaches(false);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //No I18N
connection.setRequestProperty("Content-Length", "" + Integer.toString(qryStr.getBytes().length)); //No I18N
connection.setDoOutput(true);
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(qryStr.getBytes(CHARSET));
}
finally
{
try{os.close();}catch(Exception e){}
}
int resCode = connection.getResponseCode();
boolean success = (resCode >= 200 && resCode < 300);
InputStream is = success ? connection.getInputStream() : connection.getErrorStream();
if (is == null)
{
return null;
}
String contentStr = null;
try
{
InputStreamReader reader = new InputStreamReader(is, CHARSET);
StringBuilder buffer = new StringBuilder();
char[] bytes = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(bytes, 0, bytes.length)) > 0)
{
buffer.append(bytes, 0, bytesRead);
}
contentStr = buffer.toString();//?M?? ?0F?UJ?N?NQ? %`??' "?????nb??f=?&9????i'f??]?~j*$??W??Reg??_T1-???;?oc
}
finally
{
try{is.close();}catch(Exception e){}
}
}
Can anyone please tell the cause?
I found the cause of this case. An access code is valid for 30 seconds. Need to generate the token before the expiry. If they conveyed the proper error message, we can able to sort out the problem without any confusion :(
=====Updated========
Actually below code is fine, My problem is at server side.
=====Updated========
Below is my code,I am using HttpURLConnection but not able to send JSON data to server.
Please help me Thanks in advance
JSONObject jo = new JSONObject();
jo.put("ID", "25")
Log.e("test", jo.toString());
url = new URL(URL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(jo.toString());
wr.flush();
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer sb = new StringBuffer();
int chr;
while ((chr = is.read()) != -1) {
sb.append((char) chr);
}
line = sb.toString();
Log.e("json_temp", line);
rd.close();
Your code is fine, it doesn't show a blank array, but a string "Array()". I tried a resource test, and that's exactly what it showed me.
If you type in the URL in the browser, you should see the same thing.
My request executor class gives unidentified characters as response. please help me to sort out this
import java.net.HttpURLConnection;
connection = (HttpURLConnection) neturl.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Accept-Charset", "UTF-8");//ADDED
===here I do my some additions & logics ==
statusCode = connection.getResponseCode();
===here I do my some additions & logics.response code is 200 but response text not shows as correctly==
if ((statusCode == 200) || (statusCode == 201) || (statusCode == 302)) {
is = connection.getInputStream();
}
else {
is = connection.getErrorStream();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String output;
while ((output = br.readLine()) != null) {
retStr += output;
}
this is my response [retStr]
}Umo�6�l���>� I�Y+��ك��b����D"U���/��;J�_�$�B�w���'7�����cHM�����v��,6��R�����/�����|�O���Ԙ��WU�UZ�=�Vg0�2�Y��jTkL����f<F�Q�I��%����,� *:F,��Q��T�K}�N���:gjp�6
��R�e�ca��2/2�D�Pq��m�G����a��H����P���9��T��~�^�'�Kdk�;��֠�[�&m��%fq�XR[)$łn�7\���Z.M�BµQ<*i�C�2#�(TL��!�;��h�)����n�G��h��s���z:� �tB��0����pr��#�E�P���m)�0G<"��5!˸��/yL��U�V+�F%�!(P�\�Tj"�P5��0c����-��î�j��±�Zr4���)�f��f�^Y��&��tT����X)��9݂lV]�������T1č��P5��,��&+�ũ�9.�.m�5Ǫ��
*��t���|r�M�rL f��c����u�xas�#g�:��On��PՁ�_�q���%a4�E��~$���a��s�щC%���"�kA�$ƍƋb��/!3������b����%c5����/����:�i���%�罺���o�貰��Q�s�6G�~o�5���]��o+��{��0��g輀}�V�ڸ�;�ﱙ#�7[�D�|�����L6�v���!�要ce�\��g�t�!�Ia�^��G�h�Hfk���Zr$R�j�Nu��[�\�u+��gr%�\�m�[N;���}�ӳ��2�l����ׯgKa�������ؼ"j�������xBpn6����+��/��G�L�F���R����yBf��N�����ڜZ�킏��i�J�%M�Cۉހ����n�;�G��7m���m;�����)�a�X��'�ћ�l#�GTi$���C��Vf���v��$�Z��93q����ߚ�H�Je�k�t�����+��?��ilb�RAu�.��* �9B^��(�)����
I think you are getting gzip stream as output here .. try below things in your code.
When you call https URL you get HttpsURLConnection as return which dont handle gzip streamsso try below.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip");
...
InputStream inStream = new GZIPInputStream(conn.getInputStream());
I was trying to get the products record from shopify API through REST services. But I was getting this error {"errors":{"product":"can't be blank"}} Below is the snippet of the code.
String getURL = "https://myshop.myshopify.com/admin/products.json";
url = new URL(getURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type",
"application-json");
connection.setRequestProperty("X-Shopify-Access-Token",token);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream w = new DataOutputStream(
connection.getOutputStream());
w.flush();
w.close();
// Get Response
InputStream i = connection.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(i, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null){
System.out.println(inputStr);
responseStrBuilder.append(inputStr);
}
The Request Headers should be this:
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");