Skip to content

Instantly share code, notes, and snippets.

@thealmikey
Last active August 11, 2017 20:43
Show Gist options
  • Save thealmikey/459142af9e64c7a19a160c71a84abade to your computer and use it in GitHub Desktop.
Save thealmikey/459142af9e64c7a19a160c71a84abade to your computer and use it in GitHub Desktop.
package com.biznet
import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.{LinearLayoutManager, RecyclerView}
import android.util.Log
import android.view.{LayoutInflater, View, ViewGroup}
import android.widget.TextView
import okhttp3.MultipartBody
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.{Call, Callback, Response, Retrofit}
import retrofit2.http._
class MainActivity extends Activity {
case class User(val name:String)
trait OnlineService{
@GET("user")
def getUsers():Call[Array[User]]
}
override def onCreate(savedInstanceState: Bundle): Unit = {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
var theRecyclerView= findViewById(R.id.theRecycler).asInstanceOf[RecyclerView]
var theUserList:List[User] = List.empty[User]
theRecyclerView.setLayoutManager(new LinearLayoutManager(this))
theRecyclerView.setAdapter(new MyAdapter(theUserList))
var retrofit:Retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:8080")
.addConverterFactory(GsonConverterFactory.create())
.build()
var service:OnlineService = retrofit.create(classOf[OnlineService])
var call:Call[Array[User]]=service.getUsers()
call.enqueue(new Callback[Array[User]]{
override def onFailure(call: Call[Array[User]], t: Throwable): Unit = {}
override def onResponse(call: Call[Array[User]], response: Response[Array[User]]): Unit = {
if(response.isSuccessful){
var theRes:Array[User] = response.body()
theUserList = theRes.toList
theRecyclerView.setAdapter(new MyAdapter(theUserList))
theRecyclerView.getAdapter.notifyDataSetChanged()
}
}
})
}
class MyViewHolder(v:View) extends RecyclerView.ViewHolder(v){
var theUser:TextView = v.findViewById(R.id.oneUser).asInstanceOf[TextView]
}
class MyAdapter(val userList:List[User]=List(User("one"),User("two"),User("three"))) extends RecyclerView.Adapter[MyViewHolder]{
override def getItemCount: Int = userList.length
override def onBindViewHolder(vh: MyViewHolder, i: Int): Unit = {
var theText = vh.theUser.asInstanceOf[TextView]
theText.setText(userList(i).name)
}
override def onCreateViewHolder(viewGroup: ViewGroup, i: Int): MyViewHolder = {
var layoutIn:View= LayoutInflater.from(getApplicationContext).inflate(R.layout.one_user,viewGroup, false)
new MyViewHolder(layoutIn)
}
}
}
name := "matrix"
version := "1.0"
scalaVersion := "2.12.3"
resolvers += Resolver.bintrayRepo("hseeberger", "maven")
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http" % "10.0.9",
"com.typesafe.akka" %% "akka-http-testkit" % "10.0.9" % Test
)
// https://mvnrepository.com/artifact/org.neo4j/neo4j
libraryDependencies += "org.neo4j" % "neo4j" % "3.2.2"
// https://mvnrepository.com/artifact/org.neo4j.driver/neo4j-java-driver
libraryDependencies += "org.neo4j.driver" % "neo4j-java-driver" % "1.4.2"
libraryDependencies ++= List(
"de.heikoseeberger" %% "akka-http-circe" % "1.17.0"
)
val circeVersion = "0.8.0"
libraryDependencies ++= Seq(
"io.circe" %% "circe-core",
"io.circe" %% "circe-generic",
"io.circe" %% "circe-parser",
"io.circe" %% "circe-optics"
).map(_ % circeVersion)
addCompilerPlugin(
"org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/theRecycler"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import io.circe.syntax._
import akka.stream.ActorMaterializer
import akka.http.scaladsl.server.Directives._
import org.neo4j.driver.v1.{AuthTokens, GraphDatabase}
import de.heikoseeberger.akkahttpcirce. FailFastCirceSupport
import scala.io.StdIn
object MyServer extends FailFastCirceSupport{
case class User(name:String)
def main(args:Array[String]): Unit = {
implicit val system = ActorSystem("mike-actors")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
val driver = GraphDatabase.driver("bolt://localhost/7687", AuthTokens.basic("neo4j", "myNeo4j"))
val session = driver.session
val script = s"MATCH (n:Person) RETURN n LIMIT 25"
var result = session.run(script)
var myUsers: List[User] = List.empty[User]
while(result.hasNext){
var row = result.next().get(0).get("name").asString()
myUsers=User(row)::myUsers
}
val theRoute = path("user") {
get {
complete(myUsers.asJson)
}
}
val bindingFuture = Http().bindAndHandle(theRoute, "localhost", 8080)
println("ze server iz running")
StdIn.readLine()
bindingFuture.flatMap(_.unbind()).onComplete(_ => system.terminate())
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/oneUser"
/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment