}
@Override
- public void run() {
+ public void run(){
try {
- final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
- final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
- conn.setReadTimeout(24*60*1000);//24 minutes
- if(data!=null){
- conn.setDoOutput(true);
- conn.setFixedLengthStreamingMode(data.length);
- }
- final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
- final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
- if(user == null || password == null || user.length() == 0 || password.length() == 0){
- if(callback!=null)
- callback.onError(toNonNull(context.getString(user_or_password_not_set)));
- return;
- }
-
- conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
- (user+':'+password).getBytes(), Base64.NO_WRAP));
- if(headers != null)
- for (final Header header : headers)
- conn.setRequestProperty(header.name, header.value);
- conn.connect();
- if(data!=null){
- final OutputStream stream=conn.getOutputStream();
- stream.write(data);
- stream.close();
- }
- Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
- String message=conn.getResponseMessage();
- if(message==null && callback != null)
- callback.onError(toNonNull(context.getString(no_response_returned_from_server)));
- else if(message != null && callback != null){
- if(message.charAt(message.length()-1) == ')')//message is (something)
- message=message.substring(1, message.length()-1);
- else if(message.charAt(0) == '(')//message is (something) something else
- message=message.substring(message.indexOf(')')+2);
- callback.onResult(conn.getResponseCode(), message, conn.getResponseCode() == 200 ? conn.getInputStream() : null);
- }
- conn.disconnect();
+ doRun();
} catch (Exception e) {
e.printStackTrace();
if(callback != null)
retry();
}
}
+
+ public void doRun() throws Exception{
+ final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
+ final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
+ conn.setReadTimeout(24*60*1000);//24 minutes
+ if(data!=null){
+ conn.setDoOutput(true);
+ conn.setFixedLengthStreamingMode(data.length);
+ }
+ final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+ final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+ if(user == null || password == null || user.length() == 0 || password.length() == 0){
+ if(callback!=null)
+ callback.onError(toNonNull(context.getString(user_or_password_not_set)));
+ return;
+ }
+
+ conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString((user+':'+password).getBytes(), Base64.NO_WRAP));
+ if(headers != null)
+ for (final Header header : headers)
+ conn.setRequestProperty(header.name, header.value);
+ conn.connect();
+ if(data!=null){
+ final OutputStream stream=conn.getOutputStream();
+ stream.write(data);
+ stream.close();
+ }
+ Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
+ String message=conn.getResponseMessage();
+ if(message==null && callback != null)
+ callback.onError(toNonNull(context.getString(no_response_returned_from_server)));
+ else if(message != null && callback != null){
+ if(message.charAt(message.length()-1) == ')')//message is (something)
+ message=message.substring(1, message.length()-1);
+ else if(message.charAt(0) == '(')//message is (something) something else
+ message=message.substring(message.indexOf(')')+2);
+ callback.onResult(conn.getResponseCode(), message, conn.getResponseCode() == 200 ? conn.getInputStream() : null);
+ }
+ conn.disconnect();
+ }
}