Why the file I download via GitHub API has no content? - java

I am using the GitHub API to fetch files from a repository. I have the functionality implemented and it does work, I download the needed files but I found something strange, out of the 4 files I get, one is empty (no content inside) even though when I go the to repository and open it there it is clearly with content. The rest of the files have their content in when downloaded. Any idea why that happens?
Here is my code:
public int downloadFromGithub(String repo, String fileName) throws IOException {
URL url = new URL(repo);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", ****);
connection.setRequestProperty("Accept", ****);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK && fileName!=null)
{
return saveFile(connection, fileName);
}
else { return connection.getResponseCode();}
}
public void downloadMultipleFilesFromGithub(String repo,String directoryPath) throws IOException {
URL url = new URL(repo);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", *****);
connection.setRequestProperty("Accept", "***");
String response = getResponseBody(connection);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray array = jsonObj.getJSONArray("tree");
for (int i=0; i < array.length(); i++) {
System.out.println(array.getJSONObject(i).get("path"));
String path = array.getJSONObject(i).get("path").toString();
if(path.contains("Scripts")){
String fileName = path.replace(scriptsDirectoryReplace, "");
downloadFromGithub(scriptsRepositoryDirectory+fileName,fileName);
}
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public 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);
}
}
public int saveFile(HttpURLConnection connection, String fileName) throws IOException {
// opens input stream from the HTTP connection
String saveFilePath = fileSaveDirectory + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream writer = new FileOutputStream(saveFilePath);
InputStream reader = connection.getInputStream();
int bytesRead;
byte[] buffer = new byte[4096];
while ((bytesRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, bytesRead);
}
reader.close();
writer.close();
return connection.getResponseCode();
}

Related

Sending JSON's through HTTP Request, but the Response is NULL

I need to send some JSONs to another application of mine, for it to store them in a database.
My problem is that the other app keeps getting NULL parameters.
Here is my "sender" app:
public class Main {
public static void main(String[] args) {
ConcurrentLinkedQueue<Employee> employeeQueue = new ConcurrentLinkedQueue<>();
for (int i = 0; i <= 10; i++) {
byte[] array = new byte[7]; // length is bounded by 7
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));
long id = new Random().nextLong() & 0xffffffffL;
employeeQueue.add(new Employee(id, generatedString));
}
try {
while (!employeeQueue.isEmpty()) {
JSONObject json = new JSONObject(employeeQueue.remove());
URL url = new URL("http://localhost:8080/postgressApp/createEmp");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
try {
json.write(osw);
} catch (JSONException e) {
e.printStackTrace();
}
osw.flush();
osw.close();
os.close();
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response from server: " + response.toString());
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The "receiver app is a simple spring-boot app waiting for "POST" Requests in a Controller and sending the content of the JSON to a database.
It is working for me.
Use Gson for parsing Java Dto to JSON type. download com.google.code.gson.jar-2.2.4 version.
In your code, I got the issue with parsing.
public class Main {
public static void main(String[] args) {
ConcurrentLinkedQueue<Employee> employeeQueue = new ConcurrentLinkedQueue<>();
for (int i = 0; i <= 10; i++) {
byte[] array = new byte[7]; // length is bounded by 7
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));
long id = new Random().nextLong() & 0xffffffffL;
employeeQueue.add(new Employee(id, generatedString));
}
try {
while (!employeeQueue.isEmpty()) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonEmp = gson.toJson(employeeQueue.remove());
// JSONObject json = new JSONObject(employeeQueue.remove());
URL url = new URL("http://localhost:8080/postgressApp/createEmp");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
try {
osw.write(jsonEmp);
// json.write(osw);
} catch (JSONException e) {
e.printStackTrace();
}
osw.flush();
osw.close();
os.close();
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response from server: " + response.toString());
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Convert JsonObject into byte array then write
byte [] outputBytes=json.toString().getBytes("UTF-8")
OutputStream os = con.getOutputStream();
os.write(outputBytes);

How to solve W/OkHttpClient: A connection was leaked?

I'm trying to create a connection to some remote server, and get the following warning in my LogCat: "W/OkHttpClient: A connection to "some url" was leaked. Did you forget to close a response body?"
Now I don't understand why is it happening.
I use Apache IO commons to count number of bytes sent and received, and it's the only use if mine with OkHTTP.
Can you please look at my code snippet and tell me what's wrong with it?
This is the code where I try to create a connection:
public static JSONObject getConfiguration(Context context) throws HttpRequestException {
long bytes = 0;
long netUsage = HttpUtils.getCurrentNetworkUsage(context);
int statusCode = 0, count = 0;
NetworkRequest.Status netStatus = NetworkRequest.Status.FAILED;
JSONObject commonInformation;
HttpURLConnection connection = null;
CountingInputStream cis = null;
InputStreamReader isr = null;
BufferedReader br = null;
InputStream is = null;
CountingOutputStream cos = null;
// result
JSONObject receivedConfig = null;
try {
commonInformation = ConfigurationProcessor.getCommonInformation(context);
if (commonInformation == null) {
return null;
}
URL url = new URL(BuildConfig.SERVER_CONFIG_URL);
if (BuildConfig.DEBUG) {
LogUtils.d(TAG, "url = " + url.getPath());
}
connection = url.getProtocol().equals("https")? getHttpsConnection(url) : getHttpConnection(url);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Content-Encoding", "gzip");
byte[] gzipped = HttpUtils.gzip(commonInformation.toString());
cos = new CountingOutputStream(connection.getOutputStream());
cos.write(gzipped);
cos.flush();
statusCode = connection.getResponseCode();
switch (statusCode) {
case 200: {
// get the response
is = connection.getInputStream();
cis = new CountingInputStream(is);
isr = new InputStreamReader(cis);
br = new BufferedReader(isr);
StringBuilder builder = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
builder.append(output);
}
receivedConfig = new JSONObject(builder.toString());
netStatus = NetworkRequest.Status.SUCCEEDED;
break;
}
case 502: {
throw new HttpRequestException("Received 502 error (Bad Gateway)", NetworkRequest.Type.GET_REMOTE_CONFIGURATION);
}
}
// analytics about the request
if (BuildConfig.DEBUG) {
LogUtils.d(TAG, "generating analytics about the http request");
}
count = receivedConfig == null ? 0 : 1;
if (cis != null) {
bytes = cis.getByteCount();
}
if (cos != null) {
bytes += cos.getByteCount();
}
} catch (Exception | OutOfMemoryError ex) {
LogUtils.e(TAG, ex.getMessage());
ex.printStackTrace();
} finally {
closeStream(is);
closeStream(br);
closeStream(isr);
closeStream(cis);
closeStream(cos);
if (connection != null) connection.disconnect();
createNetworkRequest(context, statusCode, netUsage, netStatus, count, bytes, NetworkRequest.Type.GET_REMOTE_CONFIGURATION, BuildConfig.SERVER_CONFIG_URL);
}
if (BuildConfig.DEBUG) {
LogUtils.d(TAG, "Received configuration data");
}
return receivedConfig;
}

Trouble with retriving data from a web service

I want to get data from an web service and after that to display it in a listView. So I made a function that get the data from the service, but when I tested it I discovered something unexpectedly. When I tested it as a call in the main function of the java class, it works, it returns me the data, but when I use it in the listView class, it doesn't. After some debugging, I still don't get why it doesn't work, but I observed that the only difference is that when the function is called in the main function, the URLConnection begins with sun.net.www.protocol.http.Http.URLConnection:http://... and when it's called in the listView class it begins with com.android.okhttp.internal.huc.HttpURLConnectionImpl:http//.. .
public static String getDataFromServer(String url) {
BufferedReader inputStream = null;
URL dataUrl = null;
String data = null;
//handle url exception
try {
dataUrl = new URL(url);
try {
URLConnection dc = dataUrl.openConnection();
dc.setConnectTimeout(5000);
dc.setReadTimeout(5000);
try {
inputStream = new BufferedReader(new InputStreamReader(dc.getInputStream(), "UTF-8"));
} catch (UnsupportedEncodingException e) { System.out.println(e.getMessage());}
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = inputStream.readLine())!=null)
sb.append(line + "\r\n");
data = sb.toString();
} catch (IOException e) { System.out.println(e.getMessage());
}
} catch (MalformedURLException e) { System.out.println(e.getMessage());}
return data;
}
do somthing like that :
String url = "http://youaddres.com/path";
URL object = new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
//if it is post
con.setRequestMethod("POST");
String me = "{\"json\":\"" + json+ "\",\"json\":\"" + json+"\"}";
OutputStream os = con.getOutputStream();
os.write(me.getBytes());
os.flush();
InputStream inputStr = con.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStr));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String = response = sb.toString();

java get response from PayU api poland

I'm trying to integrate payu poland payment system to my website but cant get success response. Website done by jsp. Here is link that payu gave for help click im using "create a new order" for configuration. But its not retrieving any answer. Could anywane help me? Here is my jsp code:
`public static String sendPostRequest2(String requestUrl, String payload, String x, String y) {
StringBuilder jsonString = new StringBuilder();
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) {
writer.write(payload);
}
connection.setRequestProperty(x, y);
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
jsonString.append("<br>\n");
}
}
connection.disconnect();
} catch (IOException e) {
try {
URL url = new URL(requestUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
InputStream is;
if (httpConn.getResponseCode() >= 400) {
is = httpConn.getErrorStream();
} else {
is = httpConn.getInputStream();
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
jsonString.append("<br>\n");
}
}
jsonString.append(new RuntimeException(e.getMessage()));
} catch (MalformedURLException ex) {
jsonString.append(new RuntimeException(ex.getMessage()));
Logger.getLogger(PayU.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
jsonString.append(new RuntimeException(ex.getMessage()));
Logger.getLogger(PayU.class.getName()).log(Level.SEVERE, null, ex);
}
}
return jsonString.toString();
}
public static String get() {
String x = "";
String payload2 = "{ 'notifyUrl': 'https://your.eshop.com/notify', 'customerIp': '127.0.0.1', 'merchantPosId': '145227', 'description': 'RTV market', 'currencyCode': 'PLN', 'totalAmount': '21000', 'products': [ { 'name': 'Wireless mouse', 'unitPrice': '15000', 'quantity': '1' }, { 'name': 'HDMI cable', 'unitPrice': '6000', 'quantity': '1' } ]}";
String requestUrl2 = "https://secure.payu.com/api/v2_1/orders/";
x += sendPostRequest2(requestUrl2, payload2, "Authorization", "Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd");
return x;
}`
set the redirect turn off after that you will get the json response. So in your java code set redirect false.
connection.setInstanceFollowRedirects(false);

Using httpUrlConnection: am i setting up my connection correctly?

I am making use of the HttURLconnection and URLConnection api's to connect to a PHP file on my web host (x10host). However the connection does not seem to be established and no string data is sent.
I am sure that my PHP code is correct, so am I using the classes incorrectly in the below code?
#Override
protected String doInBackground(String... params) {
StringBuilder respData = new StringBuilder();
InputStream stream = null;
OutputStream os = null;
HttpURLConnection httpUrlConnection;
URLConnection conn;
URL url;
try {
url = new URL("my_url/recieveString.php");
conn = url.openConnection();
httpUrlConnection = (HttpURLConnection) conn;
httpUrlConnection.setUseCaches(false);
//httpUrlConnection.setRequestProperty("User-Agent", "App");
httpUrlConnection.setConnectTimeout(30000);
httpUrlConnection.setReadTimeout(30000);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setDoOutput(true);
os = httpUrlConnection.getOutputStream();
toSubmit = "test";
stream = new ByteArrayInputStream(toSubmit.getBytes(StandardCharsets.UTF_8));
copy(stream, os);
httpUrlConnection.connect();
int responseCode = httpUrlConnection.getResponseCode();
if (200 == responseCode) {
InputStream is = httpUrlConnection.getInputStream();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(is);
char[] buffer = new char[1024];
int len;
while ((len = isr.read(buffer)) != -1) {
respData.append(buffer, 0, len);
}
} finally {
if (isr != null) {
isr.close();
success = true;
}
}
is.close();
} else {
// use below to get error stream
//inputStream = httpUrlConnection.getErrorStream();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return "done";
}
}

Categories