add network methods
This commit is contained in:
parent
5c94c82895
commit
3873885197
|
@ -0,0 +1,118 @@
|
|||
package at.fhj.swd.dailyhelper.network;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
|
||||
/**
|
||||
* General workflow for connections with UrlConnection:
|
||||
*
|
||||
* The connection object is created by invoking the openConnection method on a URL.
|
||||
* The setup parameters and general request properties are manipulated.
|
||||
* The actual connection to the remote object is made, using the connect method.
|
||||
* The remote object becomes available. The header fields and the contents of the remote object can be accessed.
|
||||
*
|
||||
*/
|
||||
public class HttpsClient {
|
||||
public String get(URL url) throws IOException {
|
||||
InputStream stream = null;
|
||||
HttpsURLConnection connection = null;
|
||||
String result = null;
|
||||
try {
|
||||
connection = (HttpsURLConnection) url.openConnection();
|
||||
connection.setReadTimeout(3000);
|
||||
connection.setConnectTimeout(3000);
|
||||
|
||||
//this is a get request
|
||||
connection.setRequestMethod("GET");
|
||||
//indicate that this connection wants to read from the connection
|
||||
connection.setDoInput(true);
|
||||
|
||||
//connect and therefore initiate network communication
|
||||
connection.connect();
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode != HttpsURLConnection.HTTP_OK) {
|
||||
throw new IOException("HTTP error code: " + responseCode);
|
||||
}
|
||||
stream = connection.getInputStream();
|
||||
|
||||
if(stream != null) {
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
|
||||
StringBuilder total = new StringBuilder();
|
||||
for (String line; (line = r.readLine()) != null; ) {
|
||||
total.append(line).append('\n');
|
||||
}
|
||||
result = total.toString();
|
||||
}
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String post(URL url, String body) throws IOException {
|
||||
InputStream stream = null;
|
||||
HttpsURLConnection connection = null;
|
||||
String result = null;
|
||||
try {
|
||||
connection = (HttpsURLConnection) url.openConnection();
|
||||
connection.setReadTimeout(3000);
|
||||
connection.setConnectTimeout(3000);
|
||||
|
||||
//this is a post request
|
||||
connection.setRequestMethod("POST");
|
||||
//indicate that this connection wants to read from the connection
|
||||
connection.setDoOutput(true);
|
||||
|
||||
//write request body to connection object
|
||||
OutputStream outputStream = connection.getOutputStream();
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
|
||||
outputStreamWriter.write(body);
|
||||
outputStreamWriter.flush();
|
||||
outputStreamWriter.close();
|
||||
outputStream.close();
|
||||
|
||||
//connect and therefore initiate network communication
|
||||
connection.connect();
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode != HttpsURLConnection.HTTP_OK) {
|
||||
throw new IOException("HTTP error code: " + responseCode);
|
||||
}
|
||||
|
||||
stream = connection.getInputStream();
|
||||
|
||||
if(stream != null) {
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
|
||||
StringBuilder total = new StringBuilder();
|
||||
for (String line; (line = r.readLine()) != null; ) {
|
||||
total.append(line).append('\n');
|
||||
}
|
||||
result = total.toString();
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package at.fhj.swd.dailyhelper.network;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
//Async task has 3 generic types for:
|
||||
//Params
|
||||
//Progress
|
||||
//Result
|
||||
public class HttpsGetTask extends AsyncTask<String, Void, String> {
|
||||
private static final String TAG = "HttpsGetTask";
|
||||
private RequestCallback callback;
|
||||
|
||||
public HttpsGetTask(RequestCallback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
//Do preparation here
|
||||
//this is executed on UI thread, so no network communication here!
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
Log.d(TAG, "Preparing ...");
|
||||
if(callback != null) {
|
||||
callback.onRequestStart();
|
||||
}
|
||||
}
|
||||
|
||||
//this runs in background
|
||||
//will be called after onPreExecute
|
||||
@Override
|
||||
protected String doInBackground(String... params) {
|
||||
Log.d(TAG, "Doing background work ...");
|
||||
HttpsClient httpsClient = new HttpsClient();
|
||||
String result = null;
|
||||
try {
|
||||
result = httpsClient.get(new URL(params[0]));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//will be called after doInBackground
|
||||
//runs on UI thread
|
||||
//gets the result of doInBackground as its parameter
|
||||
@Override
|
||||
protected void onPostExecute(String result) {
|
||||
if(callback != null)
|
||||
callback.onResult(result);
|
||||
}
|
||||
|
||||
//will be called if publishProgress is called during doInBackground
|
||||
//runs on UI thread
|
||||
@Override
|
||||
protected void onProgressUpdate(Void... values) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package at.fhj.swd.dailyhelper.network;
|
||||
|
||||
public interface RequestCallback {
|
||||
void onRequestStart();
|
||||
void onResult(String result);
|
||||
}
|
Loading…
Reference in New Issue