import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
+import android.preference.PreferenceManager;
+import android.util.Base64;
import android.util.Log;
/*
* Data to send to the server
*/
private final byte[] data;
+ /**
+ * Server URL path
+ */
+ private final String path;
/**
* Response code from the server
*/
- private int responseCode;
+ private int responseCode=0;
/**
* Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
*
+ * @param path URL path
* @param headers the extra headers
* @param context the context instance
*/
- public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context){
+ public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context){
super();
+ this.path=path;
this.headers=headers;
this.broadcast=null;//NOPMD final field
this.context=context;
/**
* Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
*
+ * @param path URL path
* @param headers the extra headers
* @param context the context instance
* @param data the message to send
*/
- public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
+ public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
super();
+ this.path=path;
this.headers=headers;
this.broadcast=null;//NOPMD final field
this.context=context;
/**
* Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
*
+ * @param path URL path
* @param headers the extra headers
* @param broadcast the broadcast to send
* @param context the context instance
*/
- public SendHttpMessageAsyncTask(final Collection<Header> headers, final String broadcast, final Context context){
+ public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final String broadcast, final Context context){
super();
+ this.path=path;
this.headers=headers;
this.broadcast=broadcast;
this.context=context;
return "";
final byte[] msg;
- if(data==null)
+ if(data==null && args.length > 0)
msg=Utils.join(" ", args).getBytes();
- else
+ else if(data!=null)
msg=data;
+ else
+ msg=null;
try {
- final URL url=Utils.getServerURL(toNonNull(context),"/");
+ final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
- conn.setDoOutput(true);
- conn.setFixedLengthStreamingMode(msg.length);
+ if(msg!=null){
+ conn.setDoOutput(true);
+ conn.setFixedLengthStreamingMode(msg.length);
+ }
conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
+ final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+ final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+ if(user == null || password == null)
+ return toNonNull(context.getString(user_or_password_not_set));
+
+ conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
+ (user+':'+password).getBytes(), Base64.NO_WRAP));
for (Header header : headers)
conn.setRequestProperty(header.name, header.value);
conn.connect();
- final OutputStream stream=conn.getOutputStream();
- stream.write(msg);
- stream.close();
+ if(msg!=null){
+ final OutputStream stream=conn.getOutputStream();
+ stream.write(msg);
+ stream.close();
+ }
Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
conn.disconnect();
final String message=conn.getResponseMessage();