链接web服务器,打开apaquee
收到数据
public void getNewsIndexData() throws Exception{
String sUrl="http://10.107.24.12:8080/newsIndex.json";
URL url=new URL(sUrl);
HttpURLConnection urlConn=(HttpURLConnection) url.openConnection();
InputStream ins=urlConn.getInputStream();
byte[] data=new byte[1500];
int ret=ins.read(data);
ins.close();
urlConn.disconnect();
String s=new String(data,0,ret);
Log.d("test",s);
}
将格式转换为json格式
建立json数据数组
new Thread(new Runnable() {
@Override
public void run() {
try {
JSONArray jArr=getNewsIndexData();
Log.d("test","arr:"+jArr.length());
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
获取到线程,传给
从json数据里面获取位置,并且给标题命名
用第三方库
完成主页面
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
try {
JSONObject jobj = jArr.getJSONObject(position);
holder.tvTitle.setText(jobj.getString("title"));
holder.tvDetail.setText(jobj.getString("detail"));
String imgUrl=jobj.getString("imageUrl");
Glide.with(holder.imgNews).load(imgUrl).into(holder.imgNews);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
获取内容从记事本neckon