Skip to content

Commit

Permalink
network on main thread exception
Browse files Browse the repository at this point in the history
  • Loading branch information
javiergamarra committed Nov 21, 2015
1 parent 63aa61d commit 35e627d
Show file tree
Hide file tree
Showing 33 changed files with 742 additions and 0 deletions.
8 changes: 8 additions & 0 deletions AndroidRxInAction/.gitignore
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
1 change: 1 addition & 0 deletions AndroidRxInAction/app/.gitignore
@@ -0,0 +1 @@
/build
41 changes: 41 additions & 0 deletions AndroidRxInAction/app/build.gradle
@@ -0,0 +1,41 @@
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.nhpatt.androidrxinaction"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.16'

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'

compile 'com.android.support:recyclerview-v7:+'
}
17 changes: 17 additions & 0 deletions AndroidRxInAction/app/proguard-rules.pro
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/aelian/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
@@ -0,0 +1,13 @@
package com.nhpatt.androidrxinaction;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
23 changes: 23 additions & 0 deletions AndroidRxInAction/app/src/main/AndroidManifest.xml
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.nhpatt.androidrxinaction"
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
@@ -0,0 +1,59 @@
package com.nhpatt.androidrxinaction;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.nhpatt.androidrxinaction.retrofit.GitHubService;
import com.nhpatt.androidrxinaction.retrofit.Repo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit.Call;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

adapter = new RepositoryAdapter(repos);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
GitHubService service = retrofit.create(GitHubService.class);

Call<List<Repo>> call = service.listReposSync("nhpatt");
try {
Response<List<Repo>> response = call.execute();

repos.addAll(response.body());
}
catch (IOException e) {
Log.e("TAG", "Error!", e);
}

}

private RepositoryAdapter adapter;
private List<Repo> repos = new ArrayList<>();
}
@@ -0,0 +1,55 @@
package com.nhpatt.androidrxinaction;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nhpatt.androidrxinaction.retrofit.Repo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RepositoryAdapter extends RecyclerView.Adapter<RepositoryAdapter.ReposViewHolder> {
public RepositoryAdapter(List<Repo> repos) {
this.repos = repos;
}

@Override
public ReposViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ReposViewHolder(view);
}

@Override
public void onBindViewHolder(ReposViewHolder holder, int position) {
holder.bind(repos.get(position));
}

@Override
public int getItemCount() {
return repos.size();
}

private final List<Repo> repos;

public class ReposViewHolder extends RecyclerView.ViewHolder {

public ReposViewHolder(View itemView) {
super(itemView);

name = (TextView) itemView.findViewById(R.id.item_name);
date = (TextView) itemView.findViewById(R.id.item_date);
}

public void bind(Repo repo) {
name.setText(repo.getName());
date.setText(new SimpleDateFormat("dd/MM").format(new Date()));
}

private final TextView name;
private final TextView date;
}
}
@@ -0,0 +1,14 @@
package com.nhpatt.androidrxinaction.retrofit;

public class Commit {

private String url;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
@@ -0,0 +1,19 @@
package com.nhpatt.androidrxinaction.retrofit;

import java.util.List;

import retrofit.Call;
import retrofit.http.GET;
import retrofit.http.Path;
import rx.Observable;

public interface GitHubService {
@GET("/users/{user}/repos")
Observable<List<Repo>> listRepos(@Path("user") String user);

@GET("/repos/{user}/{repo}/commits")
Observable<List<Commit>> listCommits(@Path("user") String user, @Path("repo") String repo);

@GET("/users/{user}/repos")
Call<List<Repo>> listReposSync(@Path("user") String user);
}
@@ -0,0 +1,31 @@
package com.nhpatt.androidrxinaction.retrofit;

public class Repo {


private String name;

private Commit commit;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Commit getCommit() {
return commit;
}

public void setCommit(Commit commit) {
this.commit = commit;
}

@Override
public String toString() {
return name;
}

}
27 changes: 27 additions & 0 deletions AndroidRxInAction/app/src/main/res/layout/activity_main.xml
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:fitsSystemWindows="true"
tools:context="com.nhpatt.androidrxinaction.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

</android.support.design.widget.CoordinatorLayout>
25 changes: 25 additions & 0 deletions AndroidRxInAction/app/src/main/res/layout/content_main.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.nhpatt.androidrxinaction.MainActivity"
tools:showIn="@layout/activity_main">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
35 changes: 35 additions & 0 deletions AndroidRxInAction/app/src/main/res/layout/item.xml
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<TextView
android:id="@+id/item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<View
android:id="@+id/separator"
android:layout_width="2dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toRightOf="@+id/item_date"
android:background="@android:color/holo_red_dark"/>

<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/separator"/>

<View
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@android:color/holo_blue_light"/>

</RelativeLayout>
10 changes: 10 additions & 0 deletions AndroidRxInAction/app/src/main/res/menu/menu_main.xml
@@ -0,0 +1,10 @@
<menu 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"
tools:context="com.nhpatt.androidrxinaction.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 35e627d

Please sign in to comment.