Skip to content

Commit

Permalink
Added networking
Browse files Browse the repository at this point in the history
  • Loading branch information
Jatin0312 committed Mar 11, 2018
1 parent 0defda2 commit 3c32d54
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 40 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.0.0-beta1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'com.android.support:design:26.0.1'
compile 'com.amitshekhar.android:android-networking:1.0.1'
implementation 'com.android.support:cardview-v7:27.1.0'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appteam.appteamworkshop">
<uses-permission android:name="android.permission.INTERNET" />


<application
android:allowBackup="true"
Expand Down
81 changes: 65 additions & 16 deletions app/src/main/java/com/appteam/appteamworkshop/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,68 @@
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
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 android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.androidnetworking.interfaces.ParsedRequestListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private PostAdapter adapter ;

private ArrayList<Post> list = new ArrayList<>();
private RecyclerView recyclerView;
private ProgressBar progressBar;
private SwipeRefreshLayout swipeRefreshLayout;

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



ArrayList<Post> list = new ArrayList<>();
list.add(new Post("title", "message"));
list.add(new Post("title", "message"));
list.add(new Post("title", "message"));
list.add(new Post("title", "message"));
list.add(new Post("title", "message"));
list.add(new Post("title", "message"));

adapter = new PostAdapter(list);
progressBar = (ProgressBar) findViewById(R.id.progressBar2);
recyclerView = findViewById(R.id.list);

recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(adapter);

swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiperefreshlayout);

progressBar.setVisibility(View.VISIBLE);
// list.add(new Post("title", "message"));
// list.add(new Post("title", "message"));
// list.add(new Post("title", "message"));
// list.add(new Post("title", "message"));
// list.add(new Post("title", "message"));
// list.add(new Post("title", "message"));
// adapter = new PostAdapter(list);
// recyclerView.setAdapter(adapter);
getList();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// recyclerView.setAdapter(adapter);
getList();
swipeRefreshLayout.setRefreshing(false);

}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -74,5 +96,32 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

return super.onOptionsItemSelected(item);
}
private void getList(){
AndroidNetworking.get("https://appteamworkshop.herokuapp.com/api/confessions")
.setPriority(Priority.LOW)
.build()
.getAsObject(PostList.class, new ParsedRequestListener<PostList>() {
@Override
public void onResponse(PostList response) {
progressBar.setVisibility(View.INVISIBLE);
Log.d("hello", String.valueOf(response.getConfessions()));
if(response.isSuccess() && response.getConfessions().size()>0)
{
list = (ArrayList<Post>) response.getConfessions();
adapter = new PostAdapter(list);
recyclerView.setAdapter(adapter);
}
}

@Override
public void onError(ANError anError) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "Check Connection", Toast.LENGTH_SHORT).show();
}
});



}
}
1 change: 0 additions & 1 deletion app/src/main/java/com/appteam/appteamworkshop/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class Post {

private String message;


public Post(String title, String message) {
this.title = title;
this.message = message;
Expand Down
61 changes: 59 additions & 2 deletions app/src/main/java/com/appteam/appteamworkshop/PostActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.appteam.appteamworkshop;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
Expand All @@ -9,19 +10,35 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONArrayRequestListener;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.androidnetworking.interfaces.ParsedRequestListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class PostActivity extends AppCompatActivity {

private EditText titleEditText, messageEditText;
private Button sbtBtn;
private ProgressDialog progressDialog;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);

AndroidNetworking.initialize(getApplicationContext());
titleEditText = findViewById(R.id.titleEditText);
messageEditText = findViewById(R.id.messageEditText);
sbtBtn = findViewById(R.id.sbtBtn);
progressBar = (ProgressBar) findViewById(R.id.progressBar);

sbtBtn.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -30,7 +47,8 @@ public void onClick(View view) {
String message = messageEditText.getText().toString();

if(!title.isEmpty() && !message.isEmpty()){

progressBar.setVisibility(View.VISIBLE);
sendPost(title,message);
}

else {
Expand All @@ -41,7 +59,46 @@ public void onClick(View view) {
}


private void sendPost(String title, String message){
private void sendPost(final String title, final String message){
AndroidNetworking.post("https://appteamworkshop.herokuapp.com/api/confess")
.addBodyParameter("title", title)
.addBodyParameter("message", message)
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
if(response!=null)
{
try {
if((Boolean) response.get("success")){
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(PostActivity.this, "Post Uploaded successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(PostActivity.this,MainActivity.class));
}
else{
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(PostActivity.this, "Post didn't uploaded successfully", Toast.LENGTH_SHORT).show();
titleEditText.setText("");
messageEditText.setText("");
}
} catch (JSONException e) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(PostActivity.this, "Conncection Error", Toast.LENGTH_SHORT).show();
titleEditText.setText("");
messageEditText.setText("");

}
}
}

@Override
public void onError(ANError anError) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(PostActivity.this, "Conncection Error", Toast.LENGTH_SHORT).show();
titleEditText.setText("");
messageEditText.setText("");
}
});
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/com/appteam/appteamworkshop/PostList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.appteam.appteamworkshop;

import java.util.ArrayList;

/**
* Created by jatin on 11/3/18.
*/

public class PostList {
private boolean success;
private ArrayList<Post> confessions;

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public ArrayList<Post> getConfessions() {
return confessions;
}

public void setConfessions(ArrayList<Post> confessions) {
this.confessions = confessions;
}

public PostList(boolean success, ArrayList<Post> confessions) {

this.success = success;
this.confessions = confessions;
}
}
11 changes: 10 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />


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

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


<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/content_main" />
</android.support.v4.widget.SwipeRefreshLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_post.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"/>

<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:id="@+id/progressBar"
android:visibility="invisible"
/>
</LinearLayout>
17 changes: 13 additions & 4 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.appteam.appteamworkshop.MainActivity"
android:orientation="vertical"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"/>
<android.support.v7.widget.RecyclerView
android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
</LinearLayout>
Loading

0 comments on commit 3c32d54

Please sign in to comment.