1.解析普通的json对象
package com.studio.imoocdemo;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DetailActivity extends AppCompatActivity {
private TextView nameView, authorView, contentView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Essay essay = (Essay) msg.obj;
nameView.setText(essay.getTitle());
authorView.setText(essay.getAuthor());
contentView.setText(essay.getContent());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
initView();
initData();
}
public void initView() {
nameView = (TextView) findViewById(R.id.name);
authorView = (TextView) findViewById(R.id.author);
contentView = (TextView) findViewById(R.id.content);
}
public void initData() {
//HttpUrlConnection
/**
* 1.实例化一个url对象
* 2.获取HttpUrlConnection对象
* 3.设置请求连接属性
* 4.获取相应码,判断是否连接成功
* 5.读取输入流并解析
*/
//参数:你要访问的接口地址
new Thread() {
@Override
public void run() {
super.run();
try {
URL url = new URL("http://www.imooc.com/api/teacher?type=3&cid=1");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(6000);
//获取相应码
if (httpURLConnection.getResponseCode() == 200) {
//获取输入流
InputStream inputStream = httpURLConnection.getInputStream();
byte[] bytes = new byte[1024 * 512];
int len = 0;
//建立缓存流,保存所读取的字节数组
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(bytes)) > -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
String msg = byteArrayOutputStream.toString();
Log.e("TAG", msg);
//JSON数据的解析:
JSONObject jsonObject = new JSONObject(msg);
int status = jsonObject.getInt("status");
String msg2 = jsonObject.getString("msg");
Log.e("TAG", status + " " + msg2);
//创建Gson对象
Gson gson = new Gson();
//参数1:满足json对象格式的字符串
String data = jsonObject.getString("data");
Essay e = gson.fromJson(data, Essay.class);
//将操作权交还给主线程
Message message = handler.obtainMessage();
message.obj = e;
//调用此方法,则会触发主线程中handler对象里重写的handleMessage方法
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}.start();
}
}
2.解析json数组
Outline.java
package com.studio.imoocdemo;
public class Outline {
private int id;
private String name;
private String picSmall;
private String picBid;
private String description;
private String learner;
public Outline(int id, String name, String picSmall, String picBid, String description, String learner) {
this.id = id;
this.name = name;
this.picSmall = picSmall;
this.picBid = picBid;
this.description = description;
this.learner = learner;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPicSmall() {
return picSmall;
}
public void setPicSmall(String picSmall) {
this.picSmall = picSmall;
}
public String getPicBid() {
return picBid;
}
public void setPicBid(String picBid) {
this.picBid = picBid;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLearner() {
return learner;
}
public void setLearner(String learner) {
this.learner = learner;
}
}
package com.studio.imoocdemo;
import android.app.Activity;
import android.content.Intent;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initData();
}
public void initview() {
findViewById(R.id.header).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, DetailActivity.class));
}
});
}
public void initData() {
new Thread() {
@Override
public void run() {
super.run();
try {
URL url = new URL("http://www.imooc.com/api/teacher?type=2");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(6000);
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
byte[] b = new byte[1024 * 512];
int len = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = in.read(b)) > -1) {
baos.write(b, 0, len);
}
String result = baos.toString();
Log.e("TAG", result);
String data = new JSONObject(result).getString("data");
//Gson
//1.解析普通的json对象
//2.解析json数组
Gson gson = new Gson();
//参数1:满足json数组形式的字符串
//参数2:Type对象,泛型将会决定你的json字符串最后被转化成的类型
ArrayList<Outline> outlines = gson.fromJson(data, new TypeToken<ArrayList<Outline>>() {
}.getType());
for (int i = 0; i < outlines.size(); i++) {
Outline o = outlines.get(i);
Log.e("TAG", "id:" + o.getId() + ",标题:" + o.getName());
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}.start();
}
}