I am using cocos2d-js to develop my game which is not support remote loading of images over https protocol, so I developed a small custom routine to download the facebook profile pics to APP42 cloud and then download the pics remotely to the game.
To achive this I open httpconnection and get an inputStream for the picture and then use uploadService.uploadFile to upload the file to APP42, sometimes this is working fine with no issues, but sometimes it load just half of the picture.
URL url;
JSONObject jsonResponse = new JSONObject();
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
try {
String fileName="";
fileName = df.format(d) + "_" + userID+"_pic.jpg";
UploadService uploadService = sp.buildUploadService();
try{
Upload u = uploadService.getFileByName(fileName);
if (u.getFileList().size()>0){
String App42URL = u.getFileList().get(0).getUrl();
jsonResponse.put("url", App42URL);
jsonResponse.put("errorCode", "0");
return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);
}
}catch(Exception ex){
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);
HttpURLConnection.setFollowRedirects(true);
connection.setReadTimeout(0);
int status = connection.getResponseCode();
boolean redirect = false;
if (status != HttpURLConnection.HTTP_OK){
if (status == HttpURLConnection.HTTP_MOVED_TEMP
||status == HttpURLConnection.HTTP_MOVED_PERM
||status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect){
String newURL = connection.getHeaderField("Location");
connection = (HttpURLConnection) new URL(newURL).openConnection();
}
InputStream is = connection.getInputStream();
UploadFileType fileType = UploadFileType.IMAGE;
String desc = "Pic of " + userID;
Upload up = uploadService.uploadFile(fileName, is,fileType, desc);
String App42URL = up.getFileList().get(0).getUrl();
jsonResponse.put("url", App42URL);
jsonResponse.put("errorCode", "0");
return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
jsonResponse.put("errorCode", "-1");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;