android 使用sqlite 和 okhttp

本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/53335123 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,使用sqlite


上回初步的使用了下sqlite。
这次使用sqlite创建一个表,并且使用唯一索引。

CREATE TABLE IF NOT EXISTS person (
  id       INTEGER PRIMARY KEY,
  name  VARCHAR(30) not null,
  age         INTEGER
  city  VARCHAR(30) not null
 );

CREATE UNIQUE INDEX IF NOT EXISTS p_name_idx ON person(name);

sql语句增加 if not exists 的判断。
可以避免执行错误。
但是在sqlite上面,不能在建表语句上增加索引。
只能再单独创建一次索引。

使用sqlite的常用命令:
http://www.runoob.com/sqlite/sqlite-commands.html
写的挺好的。
其实

sqlite> .schema
CREATE TABLE person (
  id       INTEGER PRIMARY KEY,
  name  VARCHAR(30) not null,
  age         INTEGER
  city  VARCHAR(30) not null
 );
CREATE UNIQUE INDEX p_name_idx ON person(name);
sqlite> .tables
person

sqlite> .stats
Memory Used:                         506608 (max 515984) bytes
Number of Outstanding Allocations:   164 (max 196)
Number of Pcache Overflow Bytes:     8192 (max 8192) bytes
Number of Scratch Overflow Bytes:    0 (max 0) bytes
Largest Allocation:                  425600 bytes
Largest Pcache Allocation:           4096 bytes
Largest Scratch Allocation:          0 bytes
Lookaside Slots Used:                3 (max 44)
Successful lookaside attempts:       111
Lookaside failures due to size:      74
Lookaside failures due to OOM:       0
Pager Heap Usage:                    17852 bytes
Page cache hits:                     10
Page cache misses:                   0
Page cache writes:                   4
Schema Heap Usage:                   1136 bytes
Statement Heap/Lookaside Usage:      0 bytes

sqlite> .help

经常用到的就几个。忘记了就使用.help 查看下命令。
.schema 查看表,索引语句。

2,使用okttp


在android 使用okhttp还是非常的方便的。
并且。okhttp的官网网站也给出了,很简单的demo。
github 项目地址:
https://github.com/square/okhttp
官方文档:
http://square.github.io/okhttp/
get和post请求都是非常简单的:

##### get 请求
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  Response response = client.newCall(request).execute();
  return response.body().string();
}


###### post请求:
public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

3,在android 上使用okhttp


非常的简单。
但是在android 4.0 上面要求,主线程不能请求http。
要再创建一个线程请求。

Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //okhttp get请求。
                String url = "https://m.baidu.com/";
                final OkHttpClient client = new OkHttpClient();
                final Request request = new Request.Builder()
                        .url(url)
                        .build();
                //android 4.0 以上不支持主线程访问http。
                //android.os.NetworkOnMainThreadException
                //java.lang.SecurityException: Permission denied (missing INTERNET permission?)
                //     <uses-permission android:name="android.permission.INTERNET" />
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //返回结果。
                            Response response = client.newCall(request).execute();
                            String out = response.body().string();
                            Log.v(TAG, out);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

需要引入okhttp的gradle配置。

    //增加okio 和okhttp 依赖。
    compile 'com.squareup.okio:okio:1.11.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.2'

同时在AndroidManifest.xml中加入网络请求的权限。

    <uses-permission android:name="android.permission.INTERNET" />

还有一种方法。就是配合volley使用。
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0720/3209.html

4,总结


本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/53335123 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

android 下面使用sqlite和okhtt还是很方便的。
sqlite就是一个数据库,也可以创建索引,唯一索引。
sqlite没有设置自增的地方,主键会自己增加。
okhtt用的是最多的。代码也比较简单。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值