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
.preference
.PreferenceManager
;
18 import android
.util
.Log
;
21 * Copyright © 2013 Marius Gavrilescu
23 * This file is part of FonBot.
25 * FonBot is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * FonBot is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
40 * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code
42 * @author Marius Gavrilescu <marius@ieval.ro>
44 public final class SendHttpMessageAsyncTask
extends AsyncTask
<String
, Void
, String
> {
46 * Extra: the response message
48 public static final String EXTRA_RESPONSE_MESSAGE
="response_message";
50 * Extra: the response code
52 public static final String EXTRA_RESPONSE_CODE
="response_code";
55 * List of extra request headers.
57 private final Collection
<Header
> headers
;
59 * Action to send back in the response broadcast
61 private final String broadcast
;
63 * Context instance used by this class
65 private final Context context
;
67 * Data to send to the server
69 private final byte[] data
;
71 * Response code from the server
73 private int responseCode
;
76 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
78 * @param headers the extra headers
79 * @param context the context instance
81 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final Context context
){
84 this.broadcast
=null;//NOPMD final field
86 this.data
=null;//NOPMD final field
90 * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
92 * @param headers the extra headers
93 * @param context the context instance
94 * @param data the message to send
96 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final Context context
, final byte[] data
){//NOPMD array is supposed to be immutable.
99 this.broadcast
=null;//NOPMD final field
100 this.context
=context
;
105 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
107 * @param headers the extra headers
108 * @param broadcast the broadcast to send
109 * @param context the context instance
111 public SendHttpMessageAsyncTask(final Collection
<Header
> headers
, final String broadcast
, final Context context
){
113 this.headers
=headers
;
114 this.broadcast
=broadcast
;
115 this.context
=context
;
116 this.data
=null;//NOPMD final field
120 protected String
doInBackground(final @Nullable String
... args
) {
126 msg
=Utils
.join(" ", args
).getBytes();
131 final String hostname
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("hostname", "ieval.ro");
132 final int port
=Integer
.parseInt(PreferenceManager
.getDefaultSharedPreferences(context
).getString("port", "7777"));
133 final URL url
=new URL("http", hostname
, port
, "/");
134 final HttpURLConnection conn
=(HttpURLConnection
) url
.openConnection();
135 conn
.setDoOutput(true);
136 conn
.setFixedLengthStreamingMode(msg
.length
);
137 conn
.setRequestProperty("X-ID", FonBotApplication
.instance
.regID
);
138 for (Header header
: headers
)
139 conn
.setRequestProperty(header
.name
, header
.value
);
141 final OutputStream stream
=conn
.getOutputStream();
144 Log
.d(getClass().getName(),"HTTP Response: "+conn
.getResponseCode()+" "+conn
.getResponseMessage());
146 final String message
=conn
.getResponseMessage();
147 responseCode
=conn
.getResponseCode();
149 return toNonNull(context
.getString(no_response_returned_from_server
));
150 return Utils
.parseHttpMessage(message
);
151 } catch (Exception e
) {
152 return toNonNull(context
.getString(connection_error
));
157 protected void onPostExecute(final @Nullable String result
) {
158 super.onPostExecute(result
);
160 final Intent intent
=new Intent(broadcast
);
161 intent
.putExtra(EXTRA_RESPONSE_MESSAGE
, result
);
162 intent
.putExtra(EXTRA_RESPONSE_CODE
, responseCode
);
163 context
.sendBroadcast(intent
);