get_data.json
[{"id":"5","version":"5.5","name":"haha"},
{"id":"4","version":"5.4","name":"hehe"},
{"id":"3","version":"5.3","name":"hihi"}
]
/**
* JSONObject 解析json
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = findViewById(R.id.button);
textView = findViewById(R.id.textView);
send.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.button) {
sendRequestWithOkHttp();
}
}
private void sendRequestWithOkHttp()
{
// 开子线程
new Thread(new Runnable()
{
@Override
public void run()
{
try {
OkHttpClient client = new OkHttpClient(); // 创建OkHttpClient 实例
Request request = new Request.Builder() // 发送请求并获取服务器返回数据(request)
.url("http://xx.xx.xx.xx/get_data.json") // 获取要解析的xml
.build();
Response response = client.newCall(request).execute();// 返回数据的具体内容
String responseData = response.body().string();
// 解析json
parseJsonWithJSONObject(responseData);
}
catch (Exception e) {
}
}
}).start();
}
private void parseJsonWithJSONObject(String jsonData)
{
try {
// 将数据存到JSONArray中
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0;i < jsonArray.length();i ++)
{
// 取出json的每个数据
JSONObject object = jsonArray.getJSONObject(i);
String id = object.getString("id");
String name = object.getString("name");
String version = object.getString("version");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
2. GSON
2.1 在app/build.gradle的dependencies闭包中添加
//GSON
compile 'com.google.code.gson:gson:2.7'
2.2 定义一个App类,
GSON 会将APP类映射成一个对象
Gson gson = new Gson();
App app = gson.fromJson(jsonData, App.class);
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;
}
}
2.3 解析json数组时候需要使用到typeToken
/**
* GSON解析json
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = findViewById(R.id.button);
textView = findViewById(R.id.textView);
send.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.button) {
sendRequestWithOkHttp();
}
}
private void sendRequestWithOkHttp()
{
// 开子线程
new Thread(new Runnable()
{
@Override
public void run()
{
try {
OkHttpClient client = new OkHttpClient(); // 创建OkHttpClient 实例
Request request = new Request.Builder() // 发送请求并获取服务器返回数据(request)
.url("http://xx.xx.xx.xx/get_data.json") // 获取要解析的xml
.build();
Response response = client.newCall(request).execute();// 返回数据的具体内容
String responseData = response.body().string();
// 解析json
parseJsonWithGSON(responseData);
}
catch (Exception e) {
}
}
}).start();
}
private void parseJsonWithGSON(String jsonData)
{
try {
Gson gson = new Gson();
// 使用TypeToken 将需要解析的json数组传入fromJson
List<App> appList = gson.fromJson(jsonData, new TypeToken<List<App>>(){}.getType());
for(App app: appList)
{
String id = app.getId();
String name = app.getName();
String version = app.getVersion();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}