正在回答 回答被采纳积分+1
这是解析数据的源码
public class SecondActivity extends AppCompatActivity {
TextView tx1,tx2,tx3;
protected Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Content2 content2=(Content2)msg.obj;
tx1.setText(content2.title);
tx2.setText(content2.author);
tx3.setText(content2.content);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tx1=findViewById(R.id.title);
tx2=findViewById(R.id.author);
tx3=findViewById(R.id.content);
new Thread(){
@Override
public void run() {
try {
URL url = new URL("http://www.imooc.com/api/teacher?type=3&cid=1");
HttpURLConnection coon = (HttpURLConnection) url.openConnection();
coon.setRequestMethod("GET");
coon.setReadTimeout(5000);
if(coon.getResponseCode()==200){
InputStream is=coon.getInputStream();
byte[] b=new byte[1024*512];
int n=0;
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
while ((n=is.read(b))> -1){
byteArrayOutputStream.write(b,0,n);
}
is.close();
byteArrayOutputStream.close();
String st=byteArrayOutputStream.toString();
Log.e("mseeage",st);
JSONObject jsonObject=new JSONObject(st);
int status=jsonObject.getInt("status");
String msg=jsonObject.getString("msg");
JSONObject data=jsonObject.getJSONObject("data");
String title=data.getString("title");
String author=data.getString("author");
String content=data.getString("content");
Message message=handler.obtainMessage();
Content2 content2=new Content2(title,author,content);
message.obj=content2;
handler.sendMessage(message);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
这是第二界面的代码
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
new Thread(){
@Override
public void run() {
try {
URL url = new URL("http://www.imooc.com/api/teacher?type=3&cid=1");
HttpURLConnection coon = (HttpURLConnection) url.openConnection();
coon.setRequestMethod("GET");
coon.setReadTimeout(5000);
if(coon.getResponseCode()==200){
InputStream is=coon.getInputStream();
byte[] b=new byte[1024*512];
int n=0;
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
while ((n=is.read(b,0,n))> -1){
byteArrayOutputStream.write(b,0,n);
}
String st=byteArrayOutputStream.toString();
Log.e("mseeage",st);
is.close();
byteArrayOutputStream.close();
}
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
- 参与学习 504 人
- 提交作业 1003 份
- 解答问题 1044 个
本专题是专为有Java语言基础想入坑Android开发的同学们准备的。从AndroidUI基础一直到网络,数据存储,一步到位入门Android开发。如果你会Java,那就不要犹豫了~ ~
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星