Skip to content

Commit b285d92

Browse files
committed
bug fix for thread check
1 parent 672599c commit b285d92

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

OGParser/src/main/java/com/kedia/ogparser/OpenGraphParser.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class OpenGraphParser(
2626
private val OG_SITE_NAME = "og:site_name"
2727
private val OG_TYPE = "og:type"
2828

29-
private val openGraphResult = OpenGraphResult()
29+
private var openGraphResult: OpenGraphResult? = null
3030

3131
fun parse(url: String) {
3232
this.url = url
3333

34-
GlobalScope.launch {
34+
GlobalScope.launch(Dispatchers.Main) {
3535
val result = fetchContent()
36-
listener.onPostResponse(result)
36+
result?.let { listener.onPostResponse(it) }
3737
}
3838

3939
}
@@ -55,29 +55,30 @@ class OpenGraphParser(
5555
val doc = response.parse()
5656

5757
val ogTags = doc.select(DOC_SELECT_QUERY)
58+
openGraphResult = OpenGraphResult()
5859
when {
5960
ogTags.size > 0 ->
6061
ogTags.forEachIndexed { index, _ ->
6162
val tag = ogTags[index]
6263
val text = tag.attr(PROPERTY)
6364
when (text) {
6465
OG_IMAGE -> {
65-
openGraphResult.image = (tag.attr(OPEN_GRAPH_KEY))
66+
openGraphResult!!.image = (tag.attr(OPEN_GRAPH_KEY))
6667
}
6768
OG_DESCRIPTION -> {
68-
openGraphResult.description = (tag.attr(OPEN_GRAPH_KEY))
69+
openGraphResult!!.description = (tag.attr(OPEN_GRAPH_KEY))
6970
}
7071
OG_URL -> {
71-
openGraphResult.url = (tag.attr(OPEN_GRAPH_KEY))
72+
openGraphResult!!.url = (tag.attr(OPEN_GRAPH_KEY))
7273
}
7374
OG_TITLE -> {
74-
openGraphResult.title = (tag.attr(OPEN_GRAPH_KEY))
75+
openGraphResult!!.title = (tag.attr(OPEN_GRAPH_KEY))
7576
}
7677
OG_SITE_NAME -> {
77-
openGraphResult.siteName = (tag.attr(OPEN_GRAPH_KEY))
78+
openGraphResult!!.siteName = (tag.attr(OPEN_GRAPH_KEY))
7879
}
7980
OG_TYPE -> {
80-
openGraphResult.type = (tag.attr(OPEN_GRAPH_KEY))
81+
openGraphResult!!.type = (tag.attr(OPEN_GRAPH_KEY))
8182
}
8283
}
8384
}

app/src/main/java/com/kedia/opengraphpreview/MainActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.kedia.opengraphpreview
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.os.Looper
56
import android.util.Log
67
import com.kedia.ogparser.OpenGraphCallback
78
import com.kedia.ogparser.OpenGraphParser
@@ -17,10 +18,10 @@ class MainActivity : AppCompatActivity(), OpenGraphCallback {
1718
}
1819

1920
override fun onPostResponse(openGraphResult: OpenGraphResult) {
20-
Log.d("TAG", "onPostResponse: called $openGraphResult")
21+
Log.d("TAG!!!!", "onPostResponse: called $openGraphResult")
2122
}
2223

2324
override fun onError(error: String) {
24-
Log.d("TAG", "$error")
25+
Log.e("TAG!!!!", "$error")
2526
}
2627
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
tools:context=".MainActivity">
88

99
<TextView
10+
android:id="@+id/tview"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
1213
android:text="Hello World!"

0 commit comments

Comments
 (0)