学习到android的后面,我们就会学习到连接接口,解析json。在很多的语言方面会解析json非常重要,在android中有原生的api进行解析,也可以使用第三方库进行解析。
如果我们使用原生的api进行解析的话,就需要进行一层一层的进行解析。
直接上代码
//。。。。。
package com.example.lianxijiexi;
import java.io.Serializable;
/**
* Created by Yu on 2017/10/24.
*/
public class MusicInfo implements Serializable{
private String albumname;
private String singername;
private String songname;
public MusicInfo(String songname, String albumname, String singername) {
this.songname = songname;
this.albumname = albumname;
this.singername = singername;
}
public String getAlbumname() {
return albumname;
}
public String getSingername() {
return singername;
}
public String getSongname() {
return songname;
}
public void setAlbumname(String albumname) {
this.albumname = albumname;
}
public void setSingername(String singername) {
this.singername = singername;
}
public void setSongname(String songname) {
this.songname = songname;
}
@Override
public String toString() {
return "MusicInfo{" +
"albumname='" + albumname + '\'' +
", singername='" + singername + '\'' +
", songname='" + songname + '\'' +
'}';
}
}
//这是我使用的一个json
public interface StringUtils {
String url = "{\n" +
" \"curnum\": 20,\n" +
" \"curpage\": 1,\n" +
" \"g_searchid\": \"12902471958383200\",\n" +
" \"keyword\": \"liedehua\",\n" +
" \"list\": [\n" +
" {\n" +
" \"albumid\": 33846,\n" +
" \"albummid\": \"00045jMv2hcbmO\",\n" +
" \"albumname\": \"Wonderful World 2007香港演唱会\",\n" +
" \"conn\": 20,\n" +
" \"download\": 1,\n" +
" \"fromat\": \"qqhq;common;mp3common;wmacommon;crcommon\",\n" +
" \"interval\": 226000,\n" +
" \"musicalbumid\": 33846,\n" +
" \"musicid\": 874881,\n" +
" \"musicsingerid\": 163,\n" +
" \"pubtime\": 1201795200,\n" +
" \"singerid\": 163,\n" +
" \"singermid\": \"003aQYLo2x8izP\",\n" +
" \"singername\": \"刘德华\",\n" +
" \"size\": 1849431,\n" +
" \"songid\": 874881,\n" +
" \"songmid\": \"003dIM7m02Fpo9\",\n" +
" \"songname\": \"暗里着迷 (;暗里着迷(Live);)\",\n" +
" \"songurl\": \"http://stream9.qqmusic.qq.com/12874881.wma\",\n" +
" \"source\": 12\n" +
" },\n" +
" {\n" +
" \"albumid\": 96781,\n" +
" \"albummid\": \"000b79wf3pGa96\",\n" +
" \"albumname\": \"Unforgettable 中国巡回演唱会2011\",\n" +
" \"conn\": 20,\n" +
" \"download\": 1,\n" +
" \"fromat\": \"qqhq;common;mp3common;wmacommon;crcommon\",\n" +
" \"interval\": 333000,\n" +
" \"musicalbumid\": 96781,\n" +
" \"musicid\": 1136768,\n" +
" \"musicsingerid\": 163,\n" +
" \"pubtime\": 1328803200,\n" +
" \"singerid\": 163,\n" +
" \"singermid\": \"003aQYLo2x8izP\",\n" +
" \"singername\": \"刘德华\",\n" +
" \"size\": 2711579,\n" +
" \"songid\": 1136768,\n" +
" \"songmid\": \"000Cnlmm2htNAR\",\n" +
" \"songname\": \"我恨我痴心 (;我恨我痴心(Live);)\",\n" +
" \"songurl\": \"http://stream1.qqmusic.qq.com/13136768.wma\",\n" +
" \"source\": 12\n" +
" }
//我们解析的是这一串简单的数据,如果只是解析简单的数据的话,原生的api会比第三方库更为方便。
public class MainActivity extends AppCompatActivity {
private List<MusicInfo> list;
private ListView list_item;
private Adapter ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
ad = new Adapter(MainActivity.this,list);
list_item.setAdapter(ad);
list_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, ""+list.get(i).toString(), Toast.LENGTH_SHORT).show();
}
});
}
private void initView() {
list=new ArrayList<>();
list_item = (ListView) findViewById(R.id.list_item);
}
private void initData() {
try {
JSONObject jsonObject = new JSONObject(StringUtils.url);
JSONArray list = jsonObject.getJSONArray("list");
for (int i = 0; i < list.length(); i++) {
JSONObject jsonObject1 = list.getJSONObject(i);
String albumname = jsonObject1.getString("albumname");
String singername = jsonObject1.getString("singername");
String songname = jsonObject1.getString("songname");
MusicInfo musicInfo = new MusicInfo(albumname, singername, songname);
this.list.add(musicInfo);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//赋值到布局
public class Adapter extends BaseAdapter {
private Context context;
private List<MusicInfo> musicInfo;
public Adapter(Context context, List<MusicInfo> musicInfo) {
this.context = context;
this.musicInfo = musicInfo;
}
@Override
public int getCount() {
return musicInfo.size();
}
@Override
public Object getItem(int i) {
return musicInfo.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
aa a;
if (view==null){
a = new aa();
view=LayoutInflater.from(context).inflate(R.layout.item,null);
a.textView = view.findViewById(R.id.textView);
a.textView2 =view.findViewById(R.id.textView2);
a.textView3 =view.findViewById(R.id.textView3);
view.setTag(a);
}else {
a= (aa) view.getTag();
}
a.textView.setText(musicInfo.get(i).getSongname().toString());
a.textView2.setText(musicInfo.get(i).getAlbumname().toString());
a.textView3.setText(musicInfo.get(i).getSingername().toString());
return view;
}
class aa{
TextView textView,textView2,textView3;
}
}
//布局文件
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
==================
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_marginTop="22dp"
android:id="@+id/textView2" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_marginTop="27dp"
android:id="@+id/textView3" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_marginStart="72dp"
android:layout_marginTop="34dp"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />