Skip to content

Commit

Permalink
Merge pull request #1802 from lveking/master
Browse files Browse the repository at this point in the history
#6 #460 提交实验6
  • Loading branch information
zengsn authored May 9, 2019
2 parents 6671fb4 + f948027 commit 0aa8c1c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
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;
}


}
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());
}
}
}
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"}]
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>

0 comments on commit 0aa8c1c

Please sign in to comment.