引用okHttp框架
点击左侧栏的“app”,然后点击顶部导航的“Dependencies”,最后点击“加号”,点击“加号”后,会出现三个选项,选择第一个(添加第三方库),如图:
3、输入,“okhttp”,点击搜索按钮,如图:
4、选择“com.squareup.okhttp:okhttp:2.7.5”,点击“OK”,如图:
5、这时我们可以看到“Dependencies”中增加了一个,“com.squareup.okhttp:okhttp:2.7.5”,点击“OK”按钮,如图:
6、这时看到,app文件夹下的“build.gradle”文件中,"dependencies"下增加了“com.squareup.okhttp:okhttp:2.7.5'”,然后就是少许的等待,如图:
发送get请求
//get请求时键值对放在地址后,地址末尾加?后编写键值对,键值对之间用&连接
final Request request = new Request.Builder()
.url("http://192.168.0.105/Mytest/post.php?name=zero").build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
/**
* 此时还在非UI线程中
* @param call
* @param response
* @throws IOException
*/
@Override
public void onResponse(Call call, Response response) throws IOException {
final String res = response.body().string();
//InputStream inputStream = response.body().byteStream();获取输入流,传输大文件时使用
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(res);
}
});
}
});
get请求加载图片
final Request request = new Request.Builder()
.url("http://pic17.nipic.com/20111122/6759425_152002413138_2.jpg").build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
/**
* 此时还在非UI线程中
* @param call
* @param response
* @throws IOException
*/
@Override
public void onResponse(Call call, Response response) throws IOException {
long length=response.body().contentLength();//获取文件总长度
InputStream inputStream = response.body().byteStream();
final BitmapDrawable d= (BitmapDrawable) BitmapDrawable.createFromStream(inputStream,"hehe.jpg");
runOnUiThread(new Runnable() {
@Override
public void run() {
imageview.setBackground(d);
}
});
}
});
发送post请求
String url="http://192.168.0.105/Mytest/post.php";//发送请求的地址
OkHttpClient client=new OkHttpClient();
FormBody.Builder formBodyBuild=new FormBody.Builder();
formBodyBuild.add("name","zero");//此处添加所需要的键值对
Request request=new Request.Builder().url(url)
.post(formBodyBuild.build()).build();
Call call=client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText("error");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText("success");
}
});
}
});
上面红字部分按实际需求修改:
发送字符串时
RequestBody requestBody = RequestBody
.create(MediaType.parse("text/plan;charset=utf-8)"), "你好啊");
Request request = new Request.Builder().url(url)
.post(requestBody).build();
发送json数据时
JSONObject object=new JSONObject();
try {
object.put("name","zero");
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody requestBody=RequestBody
.create(MediaType.parse("application/json; charset=utf-8"),object.toString());
Request request = new Request.Builder().url(url)
.post(requestBody).build();
需要传多种数据时 使用
MultipartBody.Builder body=new MultipartBody.Builder();
RequestBody requestBody= body.setType(MediaType.parse("multipart/form-data"))
.addFormDataPart("name", "zero")
*******这里插入其他数据例如file、json、键值对等**********
.build();
Request request = new Request.Builder().url(url)
.post(requestBody).build();
不知道发送数据的类型可以百度搜索
HTTP Content-type 对照表
post.php
<?php
require_once('db.php');
$connect = Db::getInstance()->connect();//获取Db类单例并连接上数据库
$sql = "insert into asd values( null ,'{$_POST['name']}')"; //要执行的sql语句
//$sql = "insert into asd values( null ,'{$_GET['name']}')"//get请求时使用
$result = mysql_query($sql, $connect);//获取执行语句后的结果
var_dump($result);
?>
//db.php参考
https://lightlj.wordpress.com/2017/01/24/php-%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e8%bf%9e%e6%8e%a5%e6%95%b0%e6%8d%ae%e5%ba%93/
android端发送post或get请求后再查询数据库可看到新增一条记录(数据库中要预先创建asd这个table)