-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1802 from lveking/master
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...ts/soft1714080902114/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902114/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package edu.hzuapps.androidlabs.soft1714080902114; | ||
|
||
public class App { | ||
|
||
private String id; | ||
|
||
private String name; | ||
|
||
private String version; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(String version) { | ||
this.version = version; | ||
} | ||
|
||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...4080902114/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902114/Main5Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package edu.hzuapps.androidlabs.soft1714080902114; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Toast; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import java.util.List; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class Main5Activity extends AppCompatActivity implements View.OnClickListener { | ||
|
||
private void sendRequestWithOkHttp() { | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
try{ | ||
OkHttpClient client = new OkHttpClient(); | ||
Request request = new Request.Builder().url("https://raw.githubusercontent.com/lveking/android-labs-2019/master/students/soft1714080902114/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902114/get_data.json").build(); | ||
Response response = client.newCall(request).execute(); | ||
String responseData = response.body().string(); | ||
parseJSONWithGSON(responseData); | ||
}catch(Exception e) | ||
|
||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
|
||
} | ||
|
||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main5); | ||
|
||
} | ||
|
||
@Override | ||
public void onClick(View v) | ||
{ | ||
if (v.getId() == R.id.jx){ | ||
sendRequestWithOkHttp(); | ||
Toast.makeText(Main5Activity.this,"解析json成功",Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
private void parseJSONWithGSON(String jsonData){ | ||
Gson gson=new Gson(); | ||
List<App> applist=gson.fromJson(jsonData,new TypeToken<List<App>>(){}.getType()); | ||
for(App app :applist){ | ||
Log.d("Main5Activity","id is" + app.getId()); | ||
Log.d("Main5Activity","name is" + app.getName()); | ||
Log.d("Main5Activity","version is " + app.getVersion()); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ft1714080902114/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902114/get_data.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[{"id":"5","version":"5.5","name":"Clash of"}, | ||
{"id":"6","version":"7.0","name":"Boom Beach"}, | ||
{"id":"7","version":"3.5","name":"Clash Royale"}] |
17 changes: 17 additions & 0 deletions
17
students/soft1714080902114/app/src/main/res/layout/activity_main5.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?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" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".Main5Activity"> | ||
|
||
|
||
<Button | ||
android:id="@+id/jx" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="解析" | ||
/> | ||
|
||
</LinearLayout> |