2019.12.28更新
1.只需要在AndroidManifest.xml application 属性中添加 ,就可以访问 http,而不是https
android:usesCleartextTraffic="true"
2.检查项目网络权限有没有开
3.检查模拟机网络权限有没有开
4.卸载软件,点击重新安装。
我选择的免费的接口
https://api.apiopen.top/musicBroadcasting
json格式比较复杂,所以实体类较多
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.okhttp3:okhttp:4.2.0'
import java.util.List;
public class Music {
private String code;
private String message;
private List result;
@Override
public String toString() {
return "Music{" +
"code='" + code + '\'' +
", message='" + message + '\'' +
", result=" + result +
'}';
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List getResult() {
return result;
}
public void setResult(List result) {
this.result = result;
}
}
import java.util.List;
public class Result {
private List channellist;
private String title;
private String cid;
@Override
public String toString() {
return "Result{" +
"channellist=" + channellist +
", title='" + title + '\'' +
", cid='" + cid + '\'' +
'}';
}
public List getChannellist() {
return channellist;
}
public void setChannellist(List channellist) {
this.channellist = channellist;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
}
package com.example.myapplication.movies;
public class Channellist {
private String thumb;
private String name;
private String cate_name;
private String cate_sname;
private int value;
private String channelid;
@Override
public String toString() {
return "Channellist{" +
"thumb='" + thumb + '\'' +
", name='" + name + '\'' +
", cate_name='" + cate_name + '\'' +
", cate_sname='" + cate_sname + '\'' +
", value=" + value +
", channelid='" + channelid + '\'' +
'}';
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCate_name() {
return cate_name;
}
public void setCate_name(String cate_name) {
this.cate_name = cate_name;
}
public String getCate_sname() {
return cate_sname;
}
public void setCate_sname(String cate_sname) {
this.cate_sname = cate_sname;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getChannelid() {
return channelid;
}
public void setChannelid(String channelid) {
this.channelid = channelid;
}
}
OKHttpUtils 类
package com.example.vae.webcontroller;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import retrofit2.http.Url;
/**
* OKHttp网络请求工具类
* get
* post
* 上传图片
*/
public class OKHttpUtils {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private OKHttpGetListener onOKHttpGetListener;
private MyHandler myHandler = new MyHandler();
private static final String TAG = "OKHttpUitls";
private OkHttpClient client = null;
private String BaseUrl="https://api.apiopen.top/";
// private String BaseUrl="http://47.100.104.187:8080/ssm05/";
// /get
public void get(String url) {
url=BaseUrl+url;
try {
if (client == null) {
client = new OkHttpClient();
}
//创建请求对象
Request request = new Request.Builder().url(url).build();
//创建Call请求队列
//请求都是放到一个队列里面的
Call call = client.newCall(request);
Log.d(TAG, "get() returned: " + call + "------------");
//开始请求
call.enqueue(new Callback() {
// 失败,成功的方法都是在子线程里面,不能直接更新UI
@Override
public void onFailure(Call call, IOException e) {
Message message = myHandler.obtainMessage();
message.obj = "请求失败";
message.what = 0;
myHandler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = myHandler.obtainMessage();
String json = response.body().string();
message.obj = json;
message.what = 1;
myHandler.sendMessage(message);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
// post 请求
public void post(String url, Map params) {
try {
if (client == null) {
client = new OkHttpClient();
}
url=BaseUrl+url;
FormBody.Builder builder = new FormBody.Builder();
if (params != null) {
for (Map.Entry entry : params.entrySet()) {
Log.i("参数:", entry.getKey() + ":" + entry.getValue());
builder.add(entry.getKey(), entry.getValue().toString());
}
}
RequestBody requestBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Message message = myHandler.obtainMessage();
message.obj = "请求失败";
message.what = 0;
myHandler.sendMessage(message);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Message message = myHandler.obtainMessage();
String json = response.body().string();
message.obj = json;
message.what = 1;
myHandler.sendMessage(message);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void POST_JSON(String url, Map params) {
try {
if (client == null) {
client = new OkHttpClient();
}
url=BaseUrl+url;
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
String json=gson.toJson(params);
Log.d("json",json);
RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Message message = myHandler.obtainMessage();
message.obj = "请求失败";
message.what = 0;
myHandler.sendMessage(message);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Message message = myHandler.obtainMessage();
String json = response.body().string();
message.obj = json;
message.what = 1;
myHandler.sendMessage(message);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*");
// 上传图片地址
public void photo(String url, List photoPath, Map params) {
try {
url=BaseUrl+url;
if (client == null) {
client = new OkHttpClient();
}
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
// 添加参数
if (params != null) {
for (Map.Entry entry : params.entrySet()) {
Log.i("参数:", entry.getKey() + ":" + entry.getValue());
builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
}
}
// 添加图片
if (photoPath.size() > 0) {
for (int i = 0; i < photoPath.size(); i++) {
File f = new File(photoPath.get(i));
if (f == null) break;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath(), options);
bm.compress(Bitmap.CompressFormat.JPEG, 90, new FileOutputStream(f));
bm.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
builder.addFormDataPart("multipartFile", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
}
}
MultipartBody requesBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(requesBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Message message = myHandler.obtainMessage();
message.obj = "请求失败";
message.what = 0;
myHandler.sendMessage(message);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Message message = myHandler.obtainMessage();
String json = response.body().string();
message.obj = json;
message.what = 1;
myHandler.sendMessage(message);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
//使用接口回到,将数据返回
public interface OKHttpGetListener {
void error(String error);
void success(String json);
}
//给外部调用的方法
public void setOnOKHttpGetListener(OKHttpGetListener onOKHttpGetListener) {
this.onOKHttpGetListener = onOKHttpGetListener;
}
//使用Handler,将数据在主线程返回
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
int w = msg.what;
Log.d(TAG, "handleMessage() returned: " + msg);
if (w == 0) {
String error = (String) msg.obj;
onOKHttpGetListener.error(error);
}
if (w == 1) {
String json = (String) msg.obj;
onOKHttpGetListener.success(json);
}
}
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.myapplication.movies.Music;
import com.example.myapplication.weather.Weather;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
private Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OKHttpUitls okHttpUitls = new OKHttpUitls();
okHttpUitls.get("musicBroadcasting");
// okHttpUitls.get("http://47.100.104.187:8080/interface/hello");
okHttpUitls.setOnOKHttpGetListener(new OKHttpUitls.OKHttpGetListener() {
@Override
public void error(String error) {
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
Log.d("Person", "-----error-----");
}
@Override
public void success(String json) {
Toast.makeText(MainActivity.this, json, Toast.LENGTH_SHORT).show();
System.out.println(json);
Gson gson = new Gson();
Music music = gson.fromJson(json, Music.class);
System.out.println(music);
Log.d("Person", music.toString());
}
});
}
});
}
}
首先找到关于免费的json接口
这个是我自己写的接口,针对http格式,
多多点赞,谢谢各位看客老爷!!!!