Permalink
Browse files

Added new HttpClient, HttpRequest and HttpResponse classes and using …

…DI to

inject the HttpClient
  • Loading branch information...
jonburney committed Feb 21, 2016
1 parent 3147c2c commit e784ca68377f0c2d2b2645e33e2a520af0c05ede
View
@@ -1,4 +1,4 @@
# KingsgateMediaPlayer
# Kingsgate Media Player
Master: [![Build Status](https://travis-ci.org/jonburney/KingsgateMediaPlayer-Android.svg?branch=master)](https://travis-ci.org/jonburney/KingsgateMediaPlayer-Android) Dev: [![Build Status](https://travis-ci.org/jonburney/KingsgateMediaPlayer-Android.svg?branch=dev)](https://travis-ci.org/jonburney/KingsgateMediaPlayer-Android)
@@ -38,8 +38,7 @@
*/
public class MainActivity extends Activity {
@Inject
EndisRssProvider videoListDataProvider;
@Inject EndisRssProvider videoListDataProvider;
/**
* Executed when the activity is created
@@ -23,12 +23,13 @@
import dagger.Component;
import jonburney.version7.kingsgatemediaplayer.Activities.MainActivity;
import jonburney.version7.kingsgatemediaplayer.Modules.ApplicationModule;
import jonburney.version7.kingsgatemediaplayer.Modules.HttpModule;
/**
* Created by jburney on 16/02/2016.
*/
@Singleton
@Component(modules = ApplicationModule.class)
@Component(modules = {ApplicationModule.class, HttpModule.class})
public interface IApplicationComponent {
void inject(MainActivity mainActivity);
Context context();
@@ -34,16 +34,21 @@
import javax.xml.xpath.XPathFactory;
import dagger.Module;
import dagger.Provides;
import jonburney.version7.kingsgatemediaplayer.Services.Http.HttpRequest;
import jonburney.version7.kingsgatemediaplayer.Services.Http.HttpResponse;
import jonburney.version7.kingsgatemediaplayer.Services.Http.IHttpClient;
/**
* Created by jburney on 06/02/2016.
*/
@Module
public class EndisRssProvider implements IVideoListDataProvider {
@Inject
public EndisRssProvider() {
private IHttpClient httpClient;
@Inject
public EndisRssProvider(IHttpClient httpClient) {
this.httpClient = httpClient;
}
/**
@@ -56,16 +61,14 @@ public EndisRssProvider() {
@Provides public ArrayList<String> FetchVideoList(String rssFeedUrl) {
try {
// @// TODO: 11/02/2016 Wrap this in a HttpClient abstraction for better testing
URL url = new URL(rssFeedUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
HttpRequest request = new HttpRequest();
request.setUrl(rssFeedUrl);
request.setMethod("GET");
InputStream stream = conn.getInputStream();
HttpResponse response = this.httpClient.execute(request);
return fetchTitlesFromRss(stream);
return fetchTitlesFromRss(response.getStream());
} catch (Exception e) {
Log.e("e", "e is not null, toString is " + e + " and message is " + e.getMessage());
@@ -0,0 +1,26 @@
package jonburney.version7.kingsgatemediaplayer.Modules;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import jonburney.version7.kingsgatemediaplayer.Services.Http.HttpClient;
import jonburney.version7.kingsgatemediaplayer.Services.Http.IHttpClient;
/**
* Created by jburney on 21/02/2016.
*/
@Module
public class HttpModule {
public HttpModule() {
}
@Provides
@Singleton
IHttpClient providesIHttpClient() {
return new HttpClient();
}
}
@@ -0,0 +1,44 @@
/*
* Kingsgate Media Player
* Copyright (C) 2016 Jon Burney (jon@version7.co.uk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package jonburney.version7.kingsgatemediaplayer.Services.Http;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
/**
* Created by jburney on 20/02/2016.
*/
public class HttpClient implements IHttpClient {
public HttpClient() {
}
@Override
public HttpResponse execute(HttpRequest request) throws IOException {
HttpURLConnection conn = (HttpURLConnection) request.getUrl().openConnection();
conn.setRequestMethod(request.getMethod());
conn.connect();
return new HttpResponse(conn.getInputStream());
}
}
@@ -0,0 +1,32 @@
package jonburney.version7.kingsgatemediaplayer.Services.Http;
import org.apache.http.protocol.HTTP;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by jburney on 21/02/2016.
*/
public class HttpRequest {
private URL url;
private String method;
public void setUrl(String url) throws MalformedURLException {
this.url = new URL(url);
}
public URL getUrl() {
return this.url;
}
public void setMethod(String method) {
this.method = method;
}
public String getMethod() {
return this.method;
}
}
@@ -0,0 +1,20 @@
package jonburney.version7.kingsgatemediaplayer.Services.Http;
import java.io.InputStream;
/**
* Created by jburney on 21/02/2016.
*/
public class HttpResponse {
private InputStream responseStream;
public HttpResponse(InputStream responseStream) {
this.responseStream = responseStream;
}
public InputStream getStream() {
return this.responseStream;
}
}
@@ -0,0 +1,16 @@
package jonburney.version7.kingsgatemediaplayer.Services.Http;
import java.io.IOException;
import java.net.ProtocolException;
import dagger.Component;
import dagger.Module;
import dagger.Provides;
/**
* Created by jburney on 21/02/2016.
*/
public interface IHttpClient {
HttpResponse execute(HttpRequest request) throws IOException;
}

0 comments on commit e784ca6

Please sign in to comment.