1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
6 import java
.io
.OutputStream
;
7 import java
.net
.HttpURLConnection
;
9 import java
.util
.Collection
;
11 import org
.eclipse
.jdt
.annotation
.Nullable
;
13 import android
.content
.Context
;
14 import android
.content
.Intent
;
15 import android
.os
.AsyncTask
;
16 import android
.util
.Log
;
19 * Copyright © 2013 Marius Gavrilescu
21 * This file is part of FonBot.
23 * FonBot is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * FonBot is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
38 * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code
40 * @author Marius Gavrilescu <marius@ieval.ro>
42 public final class SendHttpMessageAsyncTask
extends AsyncTask
<String
, Void
, String
> {
44 * Extra: the response message
46 public static final String EXTRA_RESPONSE_MESSAGE
="response_message";
48 * Extra: the response code
50 public static final String EXTRA_RESPONSE_CODE
="response_code";
53 * List of extra request headers.
55 private final Collection
<Header
> headers
;
57 * Action to send back in the response broadcast
59 private final String broadcast
;
61 * Context instance used by this class
63 private final Context context
;
65 * Data to send to the server
67 private final byte[] data
;
69 * Response code from the server
71 private int responseCode
;
74 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
76 * @param headers the extra headers
77 * @param context the context instance
79 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final Context context
){
82 this.broadcast
=null;//NOPMD final field
84 this.data
=null;//NOPMD final field
88 * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
90 * @param headers the extra headers
91 * @param context the context instance
92 * @param data the message to send
94 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final Context context
, final byte[] data
){//NOPMD array is supposed to be immutable.
97 this.broadcast
=null;//NOPMD final field
103 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
105 * @param headers the extra headers
106 * @param broadcast the broadcast to send
107 * @param context the context instance
109 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final String broadcast
, final Context context
){
111 this.headers
=headers
;
112 this.broadcast
=broadcast
;
113 this.context
=context
;
114 this.data
=null;//NOPMD final field
118 protected String
doInBackground(final @Nullable String
... args
) {
124 msg
=Utils
.join(" ", args
).getBytes();
129 final URL url
=Utils
.getServerURL(toNonNull(context
),"/");
130 final HttpURLConnection conn
=(HttpURLConnection
) url
.openConnection();
131 conn
.setDoOutput(true);
132 conn
.setFixedLengthStreamingMode(msg
.length
);
133 conn
.setRequestProperty("X-ID", FonBotApplication
.instance
.regID
);
134 for (Header header
: headers
)
135 conn
.setRequestProperty(header
.name
, header
.value
);
137 final OutputStream stream
=conn
.getOutputStream();
140 Log
.d(getClass().getName(),"HTTP Response: "+conn
.getResponseCode()+" "+conn
.getResponseMessage());
142 final String message
=conn
.getResponseMessage();
143 responseCode
=conn
.getResponseCode();
145 return toNonNull(context
.getString(no_response_returned_from_server
));
146 return Utils
.parseHttpMessage(message
);
147 } catch (Exception e
) {
149 return toNonNull(context
.getString(connection_error
));
154 protected void onPostExecute(final @Nullable String result
) {
155 super.onPostExecute(result
);
157 final Intent intent
=new Intent(broadcast
);
158 intent
.putExtra(EXTRA_RESPONSE_MESSAGE
, result
);
159 intent
.putExtra(EXTRA_RESPONSE_CODE
, responseCode
);
160 context
.sendBroadcast(intent
);