Skip to content

Commit 7398423

Browse files
committed
AddListView JSON Parsing Project files
1 parent 2bfd8c6 commit 7398423

37 files changed

+802
-0
lines changed

ListViewJson/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

ListViewJson/.idea/compiler.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListViewJson/.idea/copyright/profiles_settings.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListViewJson/.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListViewJson/.idea/gradle.xml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListViewJson/.idea/misc.xml

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListViewJson/.idea/modules.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListViewJson/.idea/runConfigurations.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListViewJson/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

ListViewJson/app/build.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 24
5+
buildToolsVersion "24.0.3"
6+
defaultConfig {
7+
applicationId "com.learn2crack.listviewjson"
8+
minSdkVersion 19
9+
targetSdkVersion 24
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
28+
compile 'com.android.support:appcompat-v7:24.2.1'
29+
compile 'com.google.code.gson:gson:2.7'
30+
testCompile 'junit:junit:4.12'
31+
}

ListViewJson/app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/rajamalw/android-sdk-linux/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.learn2crack.listviewjson;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.learn2crack.listviewjson", appContext.getPackageName());
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.learn2crack.listviewjson">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity android:name=".MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.learn2crack.listviewjson;
2+
3+
import android.os.AsyncTask;
4+
5+
import com.google.gson.Gson;
6+
import com.google.gson.JsonSyntaxException;
7+
import com.learn2crack.listviewjson.model.AndroidVersion;
8+
import com.learn2crack.listviewjson.model.Response;
9+
10+
import java.io.BufferedReader;
11+
import java.io.IOException;
12+
import java.io.InputStreamReader;
13+
import java.net.HttpURLConnection;
14+
import java.net.URL;
15+
import java.util.List;
16+
17+
public class LoadJSONTask extends AsyncTask<String, Void, Response> {
18+
19+
public LoadJSONTask(Listener listener) {
20+
21+
mListener = listener;
22+
}
23+
24+
public interface Listener {
25+
26+
void onLoaded(List<AndroidVersion> androidList);
27+
28+
void onError();
29+
}
30+
31+
private Listener mListener;
32+
33+
@Override
34+
protected Response doInBackground(String... strings) {
35+
try {
36+
37+
String stringResponse = loadJSON(strings[0]);
38+
Gson gson = new Gson();
39+
40+
return gson.fromJson(stringResponse, Response.class);
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
return null;
44+
} catch (JsonSyntaxException e) {
45+
e.printStackTrace();
46+
return null;
47+
}
48+
}
49+
50+
@Override
51+
protected void onPostExecute(Response response) {
52+
53+
if (response != null) {
54+
55+
mListener.onLoaded(response.getAndroid());
56+
57+
} else {
58+
59+
mListener.onError();
60+
}
61+
}
62+
63+
private String loadJSON(String jsonURL) throws IOException {
64+
65+
URL url = new URL(jsonURL);
66+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
67+
conn.setReadTimeout(10000);
68+
conn.setConnectTimeout(15000);
69+
conn.setRequestMethod("GET");
70+
conn.setDoInput(true);
71+
conn.connect();
72+
73+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
74+
String line;
75+
StringBuilder response = new StringBuilder();
76+
77+
while ((line = in.readLine()) != null) {
78+
79+
response.append(line);
80+
}
81+
82+
in.close();
83+
return response.toString();
84+
}
85+
}

0 commit comments

Comments
 (0)