1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
5 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
7 import java
.io
.OutputStream
;
8 import java
.net
.HttpURLConnection
;
10 import java
.util
.Collection
;
12 import org
.eclipse
.jdt
.annotation
.Nullable
;
14 import android
.content
.Context
;
15 import android
.content
.Intent
;
16 import android
.os
.AsyncTask
;
17 import android
.util
.Log
;
20 * Copyright © 2013 Marius Gavrilescu
22 * This file is part of FonBot.
24 * FonBot is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * FonBot is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
39 * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code
41 * @author Marius Gavrilescu <marius@ieval.ro>
43 public final class SendHttpMessageAsyncTask
extends AsyncTask
<String
, Void
, String
> {
45 * Extra: the response message
47 public static final String EXTRA_RESPONSE_MESSAGE
="response_message";
49 * Extra: the response code
51 public static final String EXTRA_RESPONSE_CODE
="response_code";
54 * List of extra request headers.
56 private final Collection
<Header
> headers
;
58 * Action to send back in the response broadcast
60 private final String broadcast
;
62 * Context instance used by this class
64 private final Context context
;
66 * Data to send to the server
68 private final byte[] data
;
70 * Response code from the server
72 private int responseCode
;
75 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
77 * @param headers the extra headers
78 * @param context the context instance
80 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final Context context
){
83 this.broadcast
=null;//NOPMD final field
85 this.data
=null;//NOPMD final field
89 * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
91 * @param headers the extra headers
92 * @param context the context instance
93 * @param data the message to send
95 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final Context context
, final byte[] data
){//NOPMD array is supposed to be immutable.
98 this.broadcast
=null;//NOPMD final field
104 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
106 * @param headers the extra headers
107 * @param broadcast the broadcast to send
108 * @param context the context instance
110 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final String broadcast
, final Context context
){
112 this.headers
=headers
;
113 this.broadcast
=broadcast
;
114 this.context
=context
;
115 this.data
=null;//NOPMD final field
119 protected String
doInBackground(final @Nullable String
... args
) {
125 msg
=Utils
.join(" ", args
).getBytes();
130 final URL url
=new URL("http://ieval.ro:7777/");
131 final HttpURLConnection conn
=(HttpURLConnection
) url
.openConnection();
132 conn
.setDoOutput(true);
133 conn
.setFixedLengthStreamingMode(msg
.length
);
134 conn
.setRequestProperty("X-ID", FonBotApplication
.instance
.regID
);
135 for (Header header
: headers
)
136 conn
.setRequestProperty(header
.name
, header
.value
);
138 final OutputStream stream
=conn
.getOutputStream();
141 Log
.d(getClass().getName(),"HTTP Response: "+conn
.getResponseCode()+" "+conn
.getResponseMessage());
143 final String message
=conn
.getResponseMessage();
144 responseCode
=conn
.getResponseCode();
146 return toNonNull(context
.getString(no_response_returned_from_server
));
147 return Utils
.parseHttpMessage(message
);
148 } 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
);