Browse Source

attempting to get http request running

pull/1/head
Nareshkumar Rao 4 years ago
parent
commit
e04df37c91
  1. 6
      .idea/inspectionProfiles/Project_Default.xml
  2. 3
      app/build.gradle
  3. 3
      app/src/main/AndroidManifest.xml
  4. 43
      app/src/main/java/com/nareshkumarrao/eiweblog/MainActivity.kt
  5. 91
      app/src/main/java/com/nareshkumarrao/eiweblog/Utilities.kt
  6. 41
      app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/ItemArticleAdapter.kt
  7. 19
      app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/PageViewModel.kt
  8. 55
      app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/PlaceholderFragment.kt
  9. 31
      app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/SectionsFragment.kt
  10. 10
      app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/SectionsPagerAdapter.kt
  11. 21
      app/src/main/res/layout/fragment_main.xml
  12. 10
      app/src/main/res/layout/fragment_sections.xml
  13. 58
      app/src/main/res/layout/item_article.xml
  14. 4
      app/src/main/res/menu/toolbar_menu.xml
  15. 7
      app/src/main/res/values/strings.xml

6
.idea/inspectionProfiles/Project_Default.xml

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="MarkdownUnresolvedFileReference" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

3
app/build.gradle

@ -44,4 +44,7 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.volley:volley:1.2.0'
implementation "com.squareup.okhttp3:okhttp:4.9.0"
}

3
app/src/main/AndroidManifest.xml

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nareshkumarrao.eiweblog">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
@ -18,7 +19,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

43
app/src/main/java/com/nareshkumarrao/eiweblog/MainActivity.kt

@ -1,13 +1,17 @@
package com.nareshkumarrao.eiweblog
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.viewpager.widget.ViewPager
import com.android.volley.VolleyLog
import com.google.android.material.tabs.TabLayout
import com.nareshkumarrao.eiweblog.ui.main.Article
import com.nareshkumarrao.eiweblog.ui.main.SectionsPagerAdapter
import okhttp3.OkHttpClient
class MainActivity : AppCompatActivity() {
@ -15,6 +19,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
val viewPager: ViewPager = findViewById(R.id.view_pager)
viewPager.adapter = sectionsPagerAdapter
@ -23,9 +28,47 @@ class MainActivity : AppCompatActivity() {
val myToolbar = findViewById<View>(R.id.toolbar) as Toolbar
setSupportActionBar(myToolbar)
VolleyLog.DEBUG = true
/*val queue = Volley.newRequestQueue(this)
val url = "https://www.reddit.com"
val stringRequest = StringRequest(
Request.Method.GET, url,
{ response ->
Log.d("XMLLIST", "got response!")
Log.d("XMLLIST", "$response")
},
{ error -> Log.e("XMLLIST", error.toString()) })
stringRequest.retryPolicy = DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
queue.add(stringRequest)*/
val thread = Thread {
try {
Log.d("XMLLIST", "Starting network request thread.")
val client = OkHttpClient()
val request = okhttp3.Request.Builder().url("https://google.com/").build()
val response = client.newCall(request).execute()
Log.d("XMLLIST", response.toString())
} catch (e: Exception) {
e.printStackTrace()
}
}
thread.start()
}
fun logArticles(articles: List<Article>){
Log.i("XMLLIST", articles.toString())
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
//Utilities.weblogXML(baseContext, ::logArticles)
menuInflater.inflate(R.menu.toolbar_menu, menu);
return true;
}

91
app/src/main/java/com/nareshkumarrao/eiweblog/Utilities.kt

@ -0,0 +1,91 @@
package com.nareshkumarrao.eiweblog
import android.content.ContentValues
import android.content.Context
import android.util.Log
import android.util.Xml
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.nareshkumarrao.eiweblog.ui.main.Article
import org.xmlpull.v1.XmlPullParser
import java.io.StringReader
internal object Utilities {
fun weblogXML(context: Context, function: (d: List<Article>) -> Unit) {
val queue = Volley.newRequestQueue(context)
val url = "https://www.google.com"
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
Log.d("XMLLIST", "got response!")
// Display the first 500 characters of the response string.
Log.d("XMLLIST", "$response")
},
{ error -> Log.e("XMLLIST", error.toString()) })
queue.add(stringRequest)
Log.e("XMLLIST", "Adding request to queue from: $url")
}
private fun parseXML(parser: XmlPullParser): List<Article> {
parser.require(XmlPullParser.START_TAG, null, "xml")
var articles: List<Article> = listOf()
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.eventType != XmlPullParser.START_TAG) {
continue
}
if (parser.name == "rs:data") {
articles = parseRSDATA(parser)
} else {
parseSkip(parser)
}
}
return articles
}
private fun parseRSDATA(parser: XmlPullParser): List<Article> {
val articles = mutableListOf<Article>()
parser.require(XmlPullParser.START_TAG, null, "rs:data")
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.eventType != XmlPullParser.START_TAG) {
continue
}
if (parser.name == "z:row") {
articles.add(parseZROW(parser))
} else {
parseSkip(parser)
}
}
return articles
}
private fun parseZROW(parser: XmlPullParser): Article {
parser.require(XmlPullParser.START_TAG, null, "z:row")
val title = parser.getAttributeValue(null, "ows_Title")
val content = parser.getAttributeValue(null, "ows_Body")
val date = parser.getAttributeValue(null, "ows_Created")
val author = parser.getAttributeValue(null, "ows_Autor2")
parser.nextTag()
parser.require(XmlPullParser.END_TAG, null, "link")
return Article(title, content, date, author)
}
private fun parseSkip(parser: XmlPullParser) {
if (parser.eventType != XmlPullParser.START_TAG) {
throw IllegalStateException()
}
var depth = 1
while (depth != 0) {
when (parser.next()) {
XmlPullParser.END_TAG -> depth--
XmlPullParser.START_TAG -> depth++
}
}
}
}

41
app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/ItemArticleAdapter.kt

@ -0,0 +1,41 @@
package com.nareshkumarrao.eiweblog.ui.main
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.nareshkumarrao.eiweblog.R
data class Article(val title: String, val content: String, val date: String, val author: String)
class ItemArticleAdapter(private val articles: List<Article>) : RecyclerView.Adapter<ItemArticleAdapter.ViewHolder>() {
inner class ViewHolder(inflater: LayoutInflater, parent: ViewGroup) : RecyclerView.ViewHolder(inflater.inflate(R.layout.item_article, parent, false)) {
private var title: TextView? = null
private var content: TextView? = null
init {
title = itemView.findViewById(R.id.titleText)
content = itemView.findViewById(R.id.contentText)
}
fun bind(article: Article) {
title?.text = article.title
content?.text = article.content
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemArticleAdapter.ViewHolder {
var inflater = LayoutInflater.from(parent.context)
return ViewHolder(inflater, parent)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val article: Article = articles[position]
holder.bind(article)
}
override fun getItemCount(): Int {
return articles.size
}
}

19
app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/PageViewModel.kt

@ -1,19 +0,0 @@
package com.nareshkumarrao.eiweblog.ui.main
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
class PageViewModel : ViewModel() {
private val _index = MutableLiveData<Int>()
val text: LiveData<String> = Transformations.map(_index) {
"Hello world from section: $it"
}
fun setIndex(index: Int) {
_index.value = index
}
}

55
app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/PlaceholderFragment.kt

@ -1,55 +0,0 @@
package com.nareshkumarrao.eiweblog.ui.main
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.nareshkumarrao.eiweblog.R
/**
* A placeholder fragment containing a simple view.
*/
class PlaceholderFragment : Fragment() {
private lateinit var pageViewModel: PageViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
pageViewModel = ViewModelProvider(this).get(PageViewModel::class.java).apply {
setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_main, container, false)
return root
}
companion object {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private const val ARG_SECTION_NUMBER = "section_number"
/**
* Returns a new instance of this fragment for the given section
* number.
*/
@JvmStatic
fun newInstance(sectionNumber: Int): PlaceholderFragment {
return PlaceholderFragment().apply {
arguments = Bundle().apply {
putInt(ARG_SECTION_NUMBER, sectionNumber)
}
}
}
}
}

31
app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/SectionsFragment.kt

@ -0,0 +1,31 @@
package com.nareshkumarrao.eiweblog.ui.main
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.nareshkumarrao.eiweblog.R
class SectionsFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_sections, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<RecyclerView>(R.id.sectionsRecylerView).apply {
layoutManager = LinearLayoutManager(activity)
adapter = ItemArticleAdapter(listOf())
}
}
fun updateView(get_articles: List<Article>){
view?.findViewById<RecyclerView>(R.id.sectionsRecylerView)?.apply {
layoutManager = LinearLayoutManager(activity)
adapter = ItemArticleAdapter(get_articles)
}
}
}

10
app/src/main/java/com/nareshkumarrao/eiweblog/ui/main/SectionsPagerAdapter.kt

@ -12,17 +12,11 @@ private val TAB_TITLES = arrayOf(
R.string.tab_sonstiges
)
/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
class SectionsPagerAdapter(private val context: Context, fm: FragmentManager)
: FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1)
return SectionsFragment()
}
override fun getPageTitle(position: Int): CharSequence? {
@ -30,6 +24,6 @@ class SectionsPagerAdapter(private val context: Context, fm: FragmentManager)
}
override fun getCount(): Int {
return 3
return TAB_TITLES.size
}
}

21
app/src/main/res/layout/fragment_main.xml

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="173dp"
tools:layout_editor_absoluteY="241dp">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

10
app/src/main/res/layout/fragment_sections.xml

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sectionsRecylerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

58
app/src/main/res/layout/item_article.xml

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="01.01.2001"
android:textColor="@color/black"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/authorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Prof. Max Mustermann"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="@+id/dateText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/dateText" />
<TextView
android:id="@+id/titleText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Klausurergebnis Android Programmierung - New Android Version"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/authorText"
app:layout_constraintStart_toStartOf="@+id/dateText"
app:layout_constraintTop_toBottomOf="@+id/dateText" />
<TextView
android:id="@+id/contentText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer risus nibh, viverra non dapibus vel, ullamcorper et nulla. Ut semper ullamcorper tellus eget pulvinar. Quisque nisl dolor, porttitor quis leo at, vehicula aliquet tellus. Nulla vulputate libero at tincidunt blandit. Ut et mi diam. Duis eu nibh dapibus, feugiat nibh nec, tempus ligula. Suspendisse mattis porta accumsan. "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/titleText"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="@+id/titleText"
app:layout_constraintTop_toBottomOf="@+id/titleText" />
</androidx.constraintlayout.widget.ConstraintLayout>

4
app/src/main/res/menu/toolbar_menu.xml

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Benachrichtigungen" app:showAsAction="never"/>
<item android:title="Information" app:showAsAction="never"/>
<item android:title="@string/notifications" app:showAsAction="never"/>
<item android:title="@string/about" app:showAsAction="never"/>
</menu>

7
app/src/main/res/values/strings.xml

@ -1,9 +1,12 @@
<resources>
<string name="app_name">EI Weblog</string>
<string name="tab_text_1">Tab 1</string>
<string name="tab_text_2">Tab 2</string>
<string name="tab_lehre">Lehre</string>
<string name="tab_pruefungen">Prüfungen</string>
<string name="tab_sonstiges">Sonstiges</string>
<string name="hsd_symbol">HSD</string>
<string name="weblog_xml_url"><![CDATA[
https://ei.hs-duesseldorf.de/weblog/_vti_bin/owssvr.dll?Cmd=Display&List=%7B169417ED-6982-4CDA-9CAC-F63FCB1757EF%7D&XMLDATA=TRUE&RowLimit=0&Queue=*
]]></string>
<string name="notifications">Benachrichtigungen</string>
<string name="about">Information</string>
</resources>
Loading…
Cancel
Save