new Thread(new Runnable() {
@Override
public void run() {
// 实例化客户端对象
OkHttpClient okHttpClient = new OkHttpClient();
// 创建一个客户端请求对象(请求方法GET,请求路径)
Request request = new Request.Builder().method("GET", null).url("http://apicloud.mob.com/v1/weather/query?key=146d30f8f3b93&city=兰州&province=甘肃").build();
Call call = okHttpClient.newCall(request);
//
try {
Response response = call.execute();
// 返回的是json数据
final String jsonstring = response.body().string();
Log.e("TAG", "run: " + jsonstring);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "请求结果:" + jsonstring, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void httppost() {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("app", "weather.future")
.add("weaid", "1")
.add("appkey", "10003")
.add("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4")
.add("format", "json")
.build();
Request request = new Request.Builder().url("http://api.k780.com:88/").method("POST", requestBody).build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
final String jsonstring = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "post请求结果"+jsonstring, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void run() {
// 实例化客户端对象
OkHttpClient okHttpClient = new OkHttpClient();
// 创建一个客户端请求对象(请求方法GET,请求路径)
Request request = new Request.Builder().method("GET", null).url("http://apicloud.mob.com/v1/weather/query?key=146d30f8f3b93&city=兰州&province=甘肃").build();
Call call = okHttpClient.newCall(request);
//
try {
Response response = call.execute();
// 返回的是json数据
final String jsonstring = response.body().string();
Log.e("TAG", "run: " + jsonstring);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "请求结果:" + jsonstring, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void httppost() {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("app", "weather.future")
.add("weaid", "1")
.add("appkey", "10003")
.add("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4")
.add("format", "json")
.build();
Request request = new Request.Builder().url("http://api.k780.com:88/").method("POST", requestBody).build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
final String jsonstring = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "post请求结果"+jsonstring, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}