MainActivity
package com.baozilichao.test2; import android.database.Cursor; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import bean.MenuBean; import dao.StudentDao; import httpurl.HTTPtest1; public class MainActivity extends AppCompatActivity { StudentOpenHelp helper; StudentDao studentDao; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setDate(); } private void initView() { helper = new StudentOpenHelp(this, "student.db", null, 1); studentDao = new StudentDao(this); } private void setDate() { studentDao.insert("16050103", "名字3", "年龄3", "爱好3"); Cursor cursor = studentDao.query("16050101"); while (cursor.moveToNext()) { Log.i("query", cursor.getString(1)); Log.i("query", cursor.getString(2)); Log.i("query", cursor.getString(3)); Log.i("query", cursor.getString(4)); } Cursor cursor1 = studentDao.queryAll(); while (cursor1.moveToNext()) { Log.i("queryAll", cursor1.getString(1)); Log.i("queryAll", cursor1.getString(3)); Log.i("queryAll", cursor1.getString(2)); Log.i("queryAll", cursor1.getString(4)); } new MyAsyncTask().execute(); } class MyAsyncTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... strings) { String result = HTTPtest1.GetURL("http://mobile.ximalaya.com/mobile/discovery/v1/tabs?device=android"); Log.i("MyAsyncTask", "doInBackground: " + result); MenuBean bean=parseHson(result); Log.i("MenuBean", " " + bean.msg); Log.i("MenuBean", " " + bean.tabs.list.get(0).title); return null; } } // 解析json数据 private MenuBean parseHson(String json) { // 第一步 把json字符串转换成jsonobject对象 try { JSONObject object = new JSONObject(json); // 第二步 需要一个javabean对象 MenuBean bean = new MenuBean(); bean.ret = object.getInt("ret"); bean.msg = object.getString("msg"); // 第三步 把tabs对应的字符串转换成jsonobject JSONObject objectTab = object.getJSONObject("tabs"); // 第三步 因为tab是一个类型,需要把对象创建出来, bean.tabs= bean.new Tab(); bean.tabs.count = objectTab.getInt("count"); // 第五步 因为list是一个jsonArray,需要先转换成jsonArray对象 JSONArray array = objectTab.getJSONArray("list"); bean.tabs.list = new ArrayList<MenuBean.Title>(); for (int i = 0; i < array.length(); i++) { JSONObject objectTitle = array.getJSONObject(i); // 第六步 对应objTitle实例化bean中的Title对象 MenuBean.Title title = bean.new Title(); title.title = objectTitle.getString("title"); title.contentType = objectTitle.getString("contentType"); bean.tabs.list.add(title); } return bean; } catch (JSONException e) { e.printStackTrace(); } return null; } }StudentOpenHelp,
package com.baozilichao.test2; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by user on 2016/8/13. */ public class StudentOpenHelp extends SQLiteOpenHelper { public StudentOpenHelp(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } //第一次创建数据库的时候 @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { String sql="create table student(id integer primary key autoincrement," + "num integer(8)," + "name varchar(10)," + "age integer(2)," + "hobby varchar(50));"; sqLiteDatabase.execSQL(sql); } //版本号增加的时候,版本号只能增加不能减小 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }HTTPtest1
package httpurl; import android.util.Log; import android.widget.ListView; import android.widget.TextView; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * Created by user on 2016/8/13. */ public class HTTPtest1 { public static String GetURL(String path) { HttpURLConnection connection = null; InputStream inputstream = null; ByteArrayOutputStream outputStream = null; try { URL url = new URL(path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8 * 1000); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { inputstream = connection.getInputStream(); outputStream = new ByteArrayOutputStream(); byte[] by = new byte[1024]; int len = 0; while ((len = inputstream.read(by)) != -1) { outputStream.write(by, 0, len); } byte[] by1 = outputStream.toByteArray(); String result = new String(by1); return result; } } catch (Exception e) { e.printStackTrace(); Log.e("TAG", "22222"); } finally { try { if (connection != null) { connection.disconnect(); } if (outputStream != null) { outputStream.close(); } if (inputstream != null) { inputstream.close(); } } catch (IOException e) { e.printStackTrace(); } Log.e("TAG", "333333"); } Log.e("TAG", "444444"); return null; } }StudentDao
package dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.baozilichao.test2.StudentOpenHelp; /** * Created by user on 2016/8/13. */ public class StudentDao { Context context; StudentOpenHelp helper; public StudentDao(Context context) { this.context = context; helper = new StudentOpenHelp(context, "student.db", null, 1); } // 往student表中插入数据的方法 public void insert(String num, String name, String age, String hobby) { SQLiteDatabase db = helper.getWritableDatabase(); String sql = "insert into student(num,name,age,hobby) values (?,?,?,?)"; db.execSQL(sql, new String[]{num, name, age, hobby}); ContentValues values=new ContentValues(); // put方法传递的key值必须和数据库中的字段一样 values.put("num",num); values.put("name",name); values.put("age",age); values.put("hobby",hobby); db.insert("student",null,values); db.close(); }// //查询数据库 public Cursor query(String num) { SQLiteDatabase db = helper.getReadableDatabase(); String sql = "select*from student where num=?"; return db.rawQuery(sql, new String[]{num}); // 使用读数据库权限的时候,不能调用db.close()方法 } //查询全部 public Cursor queryAll() { SQLiteDatabase db = helper.getReadableDatabase(); String sql = "select*from student"; return db.rawQuery(sql, null); } }MenuBean`
package bean; import java.util.List; /** * Created by user on 2016/8/13. */ public class MenuBean { public int ret; public String msg; public Tab tabs; // 因为tab是一个JsonObject,每个jsonObject需要对应一个类型 public class Tab { public int count; // 在json数据中,如果有jsonArray,就用list集合的形式存数据 public List<Title> list; } public class Title { public String title; public String contentType; } } <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.baozilichao.test2.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main_listview"/> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.baozilichao.test2"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>