Skip to content

Commit 0150dbd

Browse files
Merge pull request #1 from mrcodekiddie/master
Retrofit Client ready,Hope it works 🥴
2 parents e37da38 + 817000e commit 0150dbd

File tree

7 files changed

+259
-3
lines changed

7 files changed

+259
-3
lines changed

app/build.gradle

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ android {
99
buildToolsVersion "29.0.2"
1010
defaultConfig {
1111
applicationId "com.androiddevs.newsflash"
12-
minSdkVersion 15
12+
minSdkVersion 21
1313
targetSdkVersion 29
1414
versionCode 1
1515
versionName "1.0"
1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}
18+
19+
1820
buildTypes {
1921
release {
2022
minifyEnabled false
@@ -23,12 +25,25 @@ android {
2325
}
2426
}
2527

28+
29+
2630
dependencies {
31+
32+
def retrofitVersion="2.6.1"
33+
def rxAndroidVersion="2.1.1"
34+
def okHttpVersion="4.2.2"
35+
2736
implementation fileTree(dir: 'libs', include: ['*.jar'])
2837
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
2938
implementation 'androidx.appcompat:appcompat:1.1.0'
3039
implementation 'androidx.core:core-ktx:1.1.0'
3140
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
41+
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
42+
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
43+
implementation "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion"
44+
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"
45+
implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
46+
implementation "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"
3247
testImplementation 'junit:junit:4.12'
3348
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3449
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.androiddevs.newsflash">
44

5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"

app/src/main/java/com/androiddevs/newsflash/MainActivity.kt

+60-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,69 @@ package com.androiddevs.newsflash
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.util.Log
6+
import com.androiddevs.newsflash.repository.NewsApiService
7+
import io.reactivex.android.schedulers.AndroidSchedulers
8+
import io.reactivex.disposables.Disposable
9+
import io.reactivex.schedulers.Schedulers
510

6-
class MainActivity : AppCompatActivity() {
11+
class MainActivity : AppCompatActivity()
12+
{
713

8-
override fun onCreate(savedInstanceState: Bundle?) {
14+
val newsApiService by lazy { NewsApiService.create() }
15+
16+
override fun onCreate(savedInstanceState: Bundle?)
17+
{
918
super.onCreate(savedInstanceState)
1019
setContentView(R.layout.activity_main)
20+
21+
22+
//delete all the code below before making UI
23+
24+
var disposable: Disposable? = null
25+
26+
disposable = newsApiService.getNewsSources("science", "en", "us")
27+
.subscribeOn(Schedulers.io())
28+
.observeOn(AndroidSchedulers.mainThread())
29+
.subscribe({ result ->
30+
Log.v("success_sources", result.sources[0].description)
31+
}, { error -> Log.v("error_sources", error.message.toString()) })
32+
33+
34+
35+
36+
disposable = newsApiService.getTopHeadlines("us", "", "", "", 3, 0)
37+
.subscribeOn(Schedulers.io())
38+
.observeOn(AndroidSchedulers.mainThread())
39+
.subscribe({ result ->
40+
Log.v("topnews_success", result.articles[0].description)
41+
}, { error -> Log.v("topnews_error", error.message.toString()) })
42+
43+
disposable = newsApiService.getEverything(
44+
"quantum computer",
45+
"",
46+
"",
47+
"",
48+
"",
49+
"",
50+
"",
51+
"en",
52+
"popularity",
53+
3,
54+
1
55+
)
56+
.subscribeOn(Schedulers.io())
57+
.observeOn(AndroidSchedulers.mainThread())
58+
.subscribe({
59+
60+
result ->
61+
62+
63+
for (article in result.articles)
64+
Log.v("news ", article.description)
65+
66+
}, { error -> Log.v("topnews_error", error.message.toString()) })
67+
68+
1169
}
1270
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.androiddevs.newsflash.data
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
object NewsError
6+
{
7+
data class Error(
8+
@SerializedName("status")
9+
val status: String,
10+
@SerializedName("code")
11+
val code: String,
12+
@SerializedName("message")
13+
val message: String
14+
)
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.androiddevs.newsflash.data
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
object NewsResult
6+
{
7+
8+
data class News(
9+
@SerializedName("status")
10+
val status: String,
11+
@SerializedName("totalResults")
12+
val totalResults: Int,
13+
@SerializedName("articles")
14+
val articles: List<Article>
15+
)
16+
{
17+
data class Article(
18+
@SerializedName("source")
19+
val source: Source,
20+
@SerializedName("author")
21+
val author: String,
22+
@SerializedName("title")
23+
val title: String,
24+
@SerializedName("description")
25+
val description: String,
26+
@SerializedName("url")
27+
val url: String,
28+
@SerializedName("urlToImage")
29+
val urlToImage: String,
30+
@SerializedName("publishedAt")
31+
val publishedAt: String,
32+
@SerializedName("content")
33+
val content: String
34+
)
35+
{
36+
data class Source(
37+
@SerializedName("id")
38+
val id: String,
39+
@SerializedName("name")
40+
val name: String
41+
)
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.androiddevs.newsflash.data
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
object NewsSources
6+
{
7+
8+
data class Sources(
9+
@SerializedName("status")
10+
val status: String,
11+
@SerializedName("sources")
12+
val sources: List<Source>
13+
)
14+
{
15+
data class Source(
16+
@SerializedName("id")
17+
val id: String,
18+
@SerializedName("name")
19+
val name: String,
20+
@SerializedName("description")
21+
val description: String,
22+
@SerializedName("url")
23+
val url: String,
24+
@SerializedName("category")
25+
val category: String,
26+
@SerializedName("language")
27+
val language: String,
28+
@SerializedName("country")
29+
val country: String
30+
)
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.androiddevs.newsflash.repository
2+
3+
import com.androiddevs.newsflash.data.NewsResult
4+
import com.androiddevs.newsflash.data.NewsSources
5+
import io.reactivex.Observable
6+
import okhttp3.Interceptor
7+
import okhttp3.OkHttpClient
8+
import okhttp3.Response
9+
import okhttp3.logging.HttpLoggingInterceptor
10+
import retrofit2.Retrofit
11+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
12+
import retrofit2.converter.gson.GsonConverterFactory
13+
import retrofit2.http.GET
14+
import retrofit2.http.Query
15+
import java.util.concurrent.TimeUnit
16+
17+
interface NewsApiService {
18+
@GET("sources")
19+
fun getNewsSources(
20+
@Query("category") category: String,
21+
@Query("language") language: String,
22+
@Query("country") country: String
23+
): Observable<NewsSources.Sources>
24+
25+
@GET("top-headlines")
26+
fun getTopHeadlines(
27+
@Query("country") country: String,
28+
@Query("category") category: String,
29+
@Query("sources") sources: String,
30+
@Query("q") keyword: String,
31+
@Query("pageSize") pageSize: Int,
32+
@Query("page") page: Int
33+
): Observable<NewsResult.News>
34+
35+
@GET("everything")
36+
fun getEverything(
37+
@Query("q") keyword: String,
38+
@Query("qInTitle") keyWordInArtcile : String,
39+
@Query("sources") sources: String,
40+
@Query("domains") domain: String,
41+
@Query("excludeDomains") excludeDomains: String,
42+
@Query("from") from: String,
43+
@Query("to") to: String,
44+
@Query("language") language:String,
45+
@Query("sortBy") sortBy:String,
46+
@Query("pageSize") pageSize: Int,
47+
@Query("page") page: Int):Observable<NewsResult.News>
48+
49+
50+
companion object {
51+
fun create(): NewsApiService {
52+
53+
val httpLoggingInterceptor = HttpLoggingInterceptor()
54+
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
55+
56+
val okHttpClient = OkHttpClient.Builder()
57+
.addInterceptor(httpLoggingInterceptor)
58+
.addInterceptor(object : Interceptor {
59+
override fun intercept(chain: Interceptor.Chain): Response {
60+
val original = chain.request()
61+
62+
val request = original.newBuilder()
63+
.addHeader("X-Api-Key", "PUT YOUR OWN API KEY HERE")
64+
.method(original.method, original.body)
65+
.build()
66+
67+
return chain.proceed(request)
68+
}
69+
})
70+
.connectTimeout(30, TimeUnit.SECONDS)
71+
.readTimeout(30, TimeUnit.SECONDS)
72+
.writeTimeout(30, TimeUnit.SECONDS)
73+
.build()
74+
75+
val retrofit = Retrofit.Builder()
76+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
77+
.addConverterFactory(GsonConverterFactory.create())
78+
.baseUrl("https://newsapi.org/v2/")
79+
.client(okHttpClient)
80+
.build()
81+
82+
83+
return retrofit.create(NewsApiService::class.java)
84+
85+
}
86+
87+
}
88+
89+
90+
}

0 commit comments

Comments
 (0)