更换成OkHttp3网络库
This commit is contained in:
parent
643c51b2ff
commit
aedda7b297
@ -77,4 +77,6 @@ dependencies {
|
|||||||
implementation "androidx.room:room-runtime:2.2.6"
|
implementation "androidx.room:room-runtime:2.2.6"
|
||||||
annotationProcessor "androidx.room:room-compiler:2.2.6"
|
annotationProcessor "androidx.room:room-compiler:2.2.6"
|
||||||
|
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.9.3")
|
||||||
|
|
||||||
}
|
}
|
@ -12,6 +12,7 @@ import com.yutou.nas_music_player.Interfaces.DownloadInterface;
|
|||||||
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
|
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
|
||||||
import com.yutou.nas_music_player.MyApplication;
|
import com.yutou.nas_music_player.MyApplication;
|
||||||
import com.yutou.nas_music_player.R;
|
import com.yutou.nas_music_player.R;
|
||||||
|
import com.yutou.nas_music_player.network.HttpManager;
|
||||||
import com.yutou.nas_music_player.tools.AppData;
|
import com.yutou.nas_music_player.tools.AppData;
|
||||||
import com.yutou.nas_music_player.tools.AppTools;
|
import com.yutou.nas_music_player.tools.AppTools;
|
||||||
import com.yutou.nas_music_player.tools.NetworkTool;
|
import com.yutou.nas_music_player.tools.NetworkTool;
|
||||||
@ -222,15 +223,15 @@ public class MusicData {
|
|||||||
|
|
||||||
|
|
||||||
public String getPlayUrl() {
|
public String getPlayUrl() {
|
||||||
System.out.println("播放地址:" + NetworkTool.NetworkAPI.HOME + NetworkTool.NetworkAPI.MUSIC_PLAY
|
System.out.println("播放地址:" + HttpManager.NetworkAPI.HOME + HttpManager.NetworkAPI.MUSIC_PLAY
|
||||||
+ "?random=false&token=" + NetworkTool.NetworkAPI.HTTP_KEY + "&filePath=" + getFileBase64());
|
+ "?random=false&token=" + HttpManager.NetworkAPI.HTTP_KEY + "&filePath=" + getFileBase64());
|
||||||
return NetworkTool.NetworkAPI.HOME + NetworkTool.NetworkAPI.MUSIC_PLAY
|
return HttpManager.NetworkAPI.HOME + HttpManager.NetworkAPI.MUSIC_PLAY
|
||||||
+ "?random=false&token=" + NetworkTool.NetworkAPI.HTTP_KEY + "&filePath=" + getFileBase64();
|
+ "?random=false&token=" + HttpManager.NetworkAPI.HTTP_KEY + "&filePath=" + getFileBase64();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getImageUrl() {
|
public String getImageUrl() {
|
||||||
return NetworkTool.NetworkAPI.MUSIC_IMAGE + "?fileName=" + getFileBase64();
|
return HttpManager.NetworkAPI.MUSIC_IMAGE + "?fileName=" + getFileBase64();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap getImg() {
|
public Bitmap getImg() {
|
||||||
@ -280,8 +281,8 @@ public class MusicData {
|
|||||||
json.put("fileName", AppTools.getUrlBase64(getAlbum()));
|
json.put("fileName", AppTools.getUrlBase64(getAlbum()));
|
||||||
json.put("type", "album");
|
json.put("type", "album");
|
||||||
}
|
}
|
||||||
System.out.println("下载图片:" + NetworkTool.NetworkAPI.MUSIC_IMAGE + "?" + NetworkTool.toGetSplice(json));
|
System.out.println("下载图片:" + HttpManager.NetworkAPI.MUSIC_IMAGE + "?" + NetworkTool.toGetSplice(json));
|
||||||
NetworkTool.init().downloadImage(NetworkTool.NetworkAPI.MUSIC_IMAGE, json, imageSaveName, "post", new DownloadInterface() {
|
HttpManager.getInstance().downloadImage(HttpManager.NetworkAPI.MUSIC_IMAGE, json, imageSaveName, "post", new DownloadInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void onDownloadOver(File oldJar) {
|
public void onDownloadOver(File oldJar) {
|
||||||
AppTools.saveBitmap(oldJar, imageSaveName);
|
AppTools.saveBitmap(oldJar, imageSaveName);
|
||||||
@ -304,7 +305,7 @@ public class MusicData {
|
|||||||
img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher);
|
img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/* NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_IMAGE, json, new NetworkInterface() {
|
/* HttpManager.getInstance().httpGet(HttpManager.NetworkAPI.MUSIC_IMAGE, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject(data.toString());
|
JSONObject json = JSONObject.parseObject(data.toString());
|
||||||
|
@ -6,6 +6,8 @@ import android.content.Context;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.support.v4.media.MediaMetadataCompat;
|
import android.support.v4.media.MediaMetadataCompat;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -18,6 +20,7 @@ import com.yutou.nas_music_player.Datas.MusicData;
|
|||||||
import com.yutou.nas_music_player.Datas.PreviousPlayerList;
|
import com.yutou.nas_music_player.Datas.PreviousPlayerList;
|
||||||
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
|
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
|
||||||
import com.yutou.nas_music_player.MyApplication;
|
import com.yutou.nas_music_player.MyApplication;
|
||||||
|
import com.yutou.nas_music_player.network.HttpManager;
|
||||||
import com.yutou.nas_music_player.tools.AppTools;
|
import com.yutou.nas_music_player.tools.AppTools;
|
||||||
import com.yutou.nas_music_player.tools.CollectionTools;
|
import com.yutou.nas_music_player.tools.CollectionTools;
|
||||||
import com.yutou.nas_music_player.tools.ConfigTools;
|
import com.yutou.nas_music_player.tools.ConfigTools;
|
||||||
@ -143,8 +146,8 @@ public class MusicContainer {
|
|||||||
public static MusicContainer getInstance(MusicLibsInitInterface initInterface) {
|
public static MusicContainer getInstance(MusicLibsInitInterface initInterface) {
|
||||||
if (container == null) {
|
if (container == null) {
|
||||||
container = new MusicContainer(initInterface);
|
container = new MusicContainer(initInterface);
|
||||||
}else{
|
} else {
|
||||||
if(initInterface!=null)
|
if (initInterface != null)
|
||||||
initInterface.init();
|
initInterface.init();
|
||||||
}
|
}
|
||||||
return container;
|
return container;
|
||||||
@ -162,12 +165,13 @@ public class MusicContainer {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机下一首歌
|
* 随机下一首歌
|
||||||
|
*
|
||||||
* @param play 是否直接播放
|
* @param play 是否直接播放
|
||||||
* @param networkInterface 获取完成
|
* @param networkInterface 获取完成
|
||||||
*/
|
*/
|
||||||
private void playOfAllRandom(boolean play,NetworkInterface networkInterface) {
|
private void playOfAllRandom(boolean play, NetworkInterface networkInterface) {
|
||||||
System.out.println("随机下一首");
|
System.out.println("随机下一首");
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_RANDOM, new JSONObject(), new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_RANDOM, new JSONObject(), new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
@ -179,14 +183,15 @@ public class MusicContainer {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
json.put("path", baseData);
|
json.put("path", baseData);
|
||||||
}
|
}
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_METADATA, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_METADATA, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
|
System.err.println(data);
|
||||||
if (json.getInteger("code") == 0) {
|
if (json.getInteger("code") == 0) {
|
||||||
nowPlayData = JSONArray.parseObject(json.getString("data"), MusicData.class);
|
nowPlayData = JSONArray.parseObject(json.getString("data"), MusicData.class);
|
||||||
if(networkInterface!=null){
|
if (networkInterface != null) {
|
||||||
networkInterface.httpGetData(nowPlayData,state);
|
networkInterface.httpGetData(nowPlayData, state);
|
||||||
}
|
}
|
||||||
if (play) {
|
if (play) {
|
||||||
play(nowPlayData);
|
play(nowPlayData);
|
||||||
@ -197,7 +202,7 @@ public class MusicContainer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void httpError(Exception e) {
|
public void httpError(Exception e) {
|
||||||
if(networkInterface!=null)
|
if (networkInterface != null)
|
||||||
networkInterface.httpError(e);
|
networkInterface.httpError(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -215,7 +220,7 @@ public class MusicContainer {
|
|||||||
* 默认直接播放下一首
|
* 默认直接播放下一首
|
||||||
*/
|
*/
|
||||||
public void playOfAllRandom() {
|
public void playOfAllRandom() {
|
||||||
playOfAllRandom(true,null);
|
playOfAllRandom(true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void play(MusicData data) {
|
public void play(MusicData data) {
|
||||||
@ -250,14 +255,14 @@ public class MusicContainer {
|
|||||||
}
|
}
|
||||||
}, 0, 1000);
|
}, 0, 1000);
|
||||||
}
|
}
|
||||||
MusicData tmp=data.clone();
|
MusicData tmp = data.clone();
|
||||||
tmp.setImg(null);
|
tmp.setImg(null);
|
||||||
ConfigTools.getPreferences().edit().putString(ConfigTools.previous_music, JSONObject.toJSONString(tmp)).apply();
|
ConfigTools.getPreferences().edit().putString(ConfigTools.previous_music, JSONObject.toJSONString(tmp)).apply();
|
||||||
MediaNotificationManager manager=MediaNotificationManager.getInstance();
|
MediaNotificationManager manager = MediaNotificationManager.getInstance();
|
||||||
if(manager!=null){
|
if (manager != null) {
|
||||||
manager.getService().updateNotification();
|
manager.getService().updateNotification();
|
||||||
}
|
}
|
||||||
tmp=null;
|
tmp = null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -395,10 +400,10 @@ public class MusicContainer {
|
|||||||
} else {
|
} else {
|
||||||
playIndex = 0;
|
playIndex = 0;
|
||||||
}
|
}
|
||||||
if(libs.mainData.isEmpty()){
|
if (libs.mainData.isEmpty()) {
|
||||||
playOfAllRandom();
|
playOfAllRandom();
|
||||||
AppTools.toast("当前播放列表为空,随机播放");
|
AppTools.toast("当前播放列表为空,随机播放");
|
||||||
}else {
|
} else {
|
||||||
play(libs.mainData.get(playIndex));
|
play(libs.mainData.get(playIndex));
|
||||||
}
|
}
|
||||||
} else {//播放列表中的歌单
|
} else {//播放列表中的歌单
|
||||||
@ -459,7 +464,7 @@ public class MusicContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addTmpList(MusicData data) {
|
public void addTmpList(MusicData data) {
|
||||||
if(data.getTitle().equals("返回")&&data.getTrack().equals("-1")||data.isDir()){
|
if (data.getTitle().equals("返回") && data.getTrack().equals("-1") || data.isDir()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tmpList.add(data);
|
tmpList.add(data);
|
||||||
@ -527,7 +532,7 @@ public class MusicContainer {
|
|||||||
}
|
}
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put("favorite", editText.getText().toString());
|
json.put("favorite", editText.getText().toString());
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Dir_Add, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_Collection_Dir_Add, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
@ -553,7 +558,7 @@ public class MusicContainer {
|
|||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put("fid", musicData.getId());
|
json.put("fid", musicData.getId());
|
||||||
json.put("md5s", array);
|
json.put("md5s", array);
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Add, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_Collection_Add, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
@ -589,15 +594,15 @@ public class MusicContainer {
|
|||||||
|
|
||||||
public void onCollectionList(String collection, NetworkInterface networkInterface) {
|
public void onCollectionList(String collection, NetworkInterface networkInterface) {
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
String url = NetworkTool.NetworkAPI.MUSIC_Collection_Dir_List;
|
String url = HttpManager.NetworkAPI.MUSIC_Collection_Dir_List;
|
||||||
if (collection != null) {
|
if (collection != null) {
|
||||||
url = NetworkTool.NetworkAPI.MUSIC_Collection_Get;
|
url = HttpManager.NetworkAPI.MUSIC_Collection_Get;
|
||||||
json.put("fid", Integer.parseInt(collection));
|
json.put("fid", Integer.parseInt(collection));
|
||||||
}
|
}
|
||||||
NetworkTool.init().httpGet(url, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(url, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
if(StringUtil.isEmpty((String)data)){
|
if (StringUtil.isEmpty((String) data)) {
|
||||||
networkInterface.httpGetData(new ArrayList<>(), 0);
|
networkInterface.httpGetData(new ArrayList<>(), 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -617,7 +622,14 @@ public class MusicContainer {
|
|||||||
musicData.setDir(false);
|
musicData.setDir(false);
|
||||||
list.add(musicData);
|
list.add(musicData);
|
||||||
}
|
}
|
||||||
|
new Handler(Looper.getMainLooper())
|
||||||
|
.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
networkInterface.httpGetData(list, 0);
|
networkInterface.httpGetData(list, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
networkInterface.httpGetData(new ArrayList<>(), 0);
|
networkInterface.httpGetData(new ArrayList<>(), 0);
|
||||||
@ -642,7 +654,7 @@ public class MusicContainer {
|
|||||||
}
|
}
|
||||||
json.put("data", array);
|
json.put("data", array);
|
||||||
|
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Remove, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_Collection_Remove, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
@ -693,7 +705,7 @@ public class MusicContainer {
|
|||||||
} else {
|
} else {
|
||||||
json.put("album", album);
|
json.put("album", album);
|
||||||
}
|
}
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Album, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_Album, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
@ -708,7 +720,14 @@ public class MusicContainer {
|
|||||||
musicData.setAlbum((String) album);
|
musicData.setAlbum((String) album);
|
||||||
list.add(musicData);
|
list.add(musicData);
|
||||||
}
|
}
|
||||||
|
new Handler(Looper.getMainLooper())
|
||||||
|
.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
networkInterface.httpGetData(list, 0);
|
networkInterface.httpGetData(list, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
networkInterface.httpGetData(new ArrayList<>(), 0);
|
networkInterface.httpGetData(new ArrayList<>(), 0);
|
||||||
@ -723,7 +742,7 @@ public class MusicContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onDirMusicData(JSONObject json, NetworkInterface networkInterface) {
|
public void onDirMusicData(JSONObject json, NetworkInterface networkInterface) {
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_LIST, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_LIST, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
@ -753,7 +772,7 @@ public class MusicContainer {
|
|||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put("path", path);
|
json.put("path", path);
|
||||||
json.put("type", type);
|
json.put("type", type);
|
||||||
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_LIST, json, new NetworkInterface() {
|
HttpManager.getInstance().httpPost(HttpManager.NetworkAPI.MUSIC_LIST, json, new NetworkInterface() {
|
||||||
@Override
|
@Override
|
||||||
public void httpGetData(Object data, int state) {
|
public void httpGetData(Object data, int state) {
|
||||||
JSONObject json = JSONObject.parseObject((String) data);
|
JSONObject json = JSONObject.parseObject((String) data);
|
||||||
|
@ -0,0 +1,334 @@
|
|||||||
|
package com.yutou.nas_music_player.network;
|
||||||
|
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONException;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.yutou.nas_music_player.Interfaces.DownloadInterface;
|
||||||
|
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
|
||||||
|
import com.yutou.nas_music_player.MyApplication;
|
||||||
|
import com.yutou.nas_music_player.tools.AppData;
|
||||||
|
import com.yutou.nas_music_player.tools.AppTools;
|
||||||
|
import com.yutou.nas_music_player.tools.Log;
|
||||||
|
import com.yutou.nas_music_player.tools.NetworkTool;
|
||||||
|
import com.yutou.nas_music_player.tools.StringUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.temporal.TemporalUnit;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
|
import okhttp3.FormBody;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.OkHttp;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class HttpManager {
|
||||||
|
private static HttpManager manager;
|
||||||
|
public static String downloadPath = AppData.dir + AppData.download_dir + File.separator;
|
||||||
|
|
||||||
|
public static class NetworkAPI {
|
||||||
|
public static final String HTTP_KEY = "zIrsh9TUZP2lfRW753PannG49E7VJvor";
|
||||||
|
public static String HOME = "";
|
||||||
|
public static String MUSIC_LIST = HOME + "/music/list.do";
|
||||||
|
public static String MUSIC_Album = HOME + "/music/getAlbum.do";
|
||||||
|
public static String MUSIC_Collection_Dir_List = HOME + "/music/favorite/dir/list.do";
|
||||||
|
public static String MUSIC_Collection_Dir_Add = HOME + "/music/favorite/dir/add.do";
|
||||||
|
public static String MUSIC_Collection_Dir_ReName = HOME + "/music/favorite/dir/rename.do";
|
||||||
|
public static String MUSIC_Collection_Get = HOME + "/music/favorite/get.do";
|
||||||
|
public static String MUSIC_Collection_Add = HOME + "/music/favorite/add.do";
|
||||||
|
public static String MUSIC_Collection_Remove = HOME + "/music/favorite/remove.do";
|
||||||
|
public static String MUSIC_ALL = HOME + "/music/all.do";
|
||||||
|
public static String MUSIC_METADATA = HOME + "/music/find/file.do";
|
||||||
|
public static String MUSIC_IMAGE = HOME + "/music/web/image.do";
|
||||||
|
public static String MUSIC_PLAY = HOME + "/music/play.do";
|
||||||
|
public static String MUSIC_RANDOM = HOME + "/music/random.do";
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpManager() {
|
||||||
|
System.out.println("初始化NetWork");
|
||||||
|
httpGet("http://192.168.31.88:8000/public/version.do", new NetworkInterface() {
|
||||||
|
@Override
|
||||||
|
public void httpGetData(Object data, int state) {
|
||||||
|
System.out.println("state = " + state);
|
||||||
|
if (state == 200) {
|
||||||
|
NetworkAPI.HOME = "http://192.168.31.88:8000/nas";
|
||||||
|
AppTools.toast("检测到局域网在线,使用局域网模式");
|
||||||
|
} else {
|
||||||
|
toPublicNetWork();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void httpError(Exception e) {
|
||||||
|
toPublicNetWork();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void toPublicNetWork() {
|
||||||
|
System.out.println("使用互联网模式");
|
||||||
|
httpPost("http://tools.yutou233.cn/nas/music/getlocalhost.do?token=" + NetworkAPI.HTTP_KEY, new JSONObject(), new NetworkInterface() {
|
||||||
|
@Override
|
||||||
|
public void httpGetData(Object data, int state) {
|
||||||
|
try {
|
||||||
|
JSONObject json = JSONObject.parseObject(data.toString());
|
||||||
|
if (json.getInteger("code") == 0) {
|
||||||
|
NetworkAPI.HOME = json.getString("data") + "/nas";
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void httpError(Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HttpManager getInstance() {
|
||||||
|
if (manager == null) {
|
||||||
|
manager = new HttpManager();
|
||||||
|
}
|
||||||
|
return manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void httpGet(String url, NetworkInterface networkInterface) {
|
||||||
|
if (StringUtil.isEmpty(url) || !url.startsWith("http")) {
|
||||||
|
new Timer()
|
||||||
|
.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (!StringUtil.isEmpty(NetworkAPI.HOME)) {
|
||||||
|
httpGet(NetworkAPI.HOME + url, networkInterface);
|
||||||
|
} else {
|
||||||
|
httpGet(url, networkInterface);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}, 300);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
OkHttpClient client = getClient();
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.build();
|
||||||
|
client.newCall(request).enqueue(new Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||||
|
networkInterface.httpError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||||
|
networkInterface.httpGetData(response, response.code());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void httpPost(String url, JSONObject body, NetworkInterface networkInterface) {
|
||||||
|
System.out.println("url = " + StringUtil.isEmpty(url) + " starts = " + !url.startsWith("http") + " data = " + url);
|
||||||
|
if (StringUtil.isEmpty(url) || !url.startsWith("http")) {
|
||||||
|
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (!StringUtil.isEmpty(NetworkAPI.HOME)) {
|
||||||
|
httpPost(NetworkAPI.HOME + url, body, networkInterface);
|
||||||
|
} else {
|
||||||
|
httpPost(url, body, networkInterface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 300);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
OkHttpClient client = getClient();
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(RequestBody.create(body.toJSONString(), MediaType.parse("application/json; charset=utf-8")))
|
||||||
|
.build();
|
||||||
|
client.newCall(request)
|
||||||
|
.enqueue(new Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||||
|
networkInterface.httpError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||||
|
networkInterface.httpGetData(response.body().string(), response.code());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void download(String url, JSONObject body, String saveName, String method, DownloadInterface downloadInterface) {
|
||||||
|
OkHttpClient client = getClient();
|
||||||
|
Request request;
|
||||||
|
Request.Builder requestBuilder = new Request.Builder();
|
||||||
|
requestBuilder.url(url);
|
||||||
|
if (method.equals("post")) {
|
||||||
|
requestBuilder.post(RequestBody.create(body.toJSONString(), MediaType.parse("application/json; charset=utf-8")));
|
||||||
|
}
|
||||||
|
request = requestBuilder.build();
|
||||||
|
client.newCall(request)
|
||||||
|
.enqueue(new Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||||
|
downloadInterface.onError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||||
|
File jar = null;
|
||||||
|
try {
|
||||||
|
File savePath = new File(downloadPath + saveName + "_tmp.tmp");
|
||||||
|
if (!savePath.exists()) {
|
||||||
|
savePath.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
Log.i("Download", "下载文件:" + url + " 保存文件:" + saveName);
|
||||||
|
|
||||||
|
InputStream inputStream = response.body().byteStream();
|
||||||
|
jar = new File(downloadPath + saveName + "_tmp.tmp");
|
||||||
|
jar.createNewFile();
|
||||||
|
Log.i(NetworkTool.class, "临时保存文件:" + jar.getAbsolutePath());
|
||||||
|
OutputStream outputStream = new FileOutputStream(jar);
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = inputStream.read(bytes)) > 0) {
|
||||||
|
outputStream.write(bytes, 0, len);
|
||||||
|
}
|
||||||
|
outputStream.close();
|
||||||
|
inputStream.close();
|
||||||
|
File oldJar = new File(downloadPath + saveName);
|
||||||
|
if (oldJar.exists()) {
|
||||||
|
oldJar.delete();
|
||||||
|
}
|
||||||
|
jar.renameTo(oldJar);
|
||||||
|
Log.i(NetworkTool.class, "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
|
||||||
|
Log.toast(MyApplication.application, "下载完成,重启生效");
|
||||||
|
|
||||||
|
if (downloadInterface != null) {
|
||||||
|
downloadInterface.onDownloadOver(oldJar);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
if (downloadInterface != null)
|
||||||
|
downloadInterface.onError(e);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> downloadImageList = new ArrayList<>();
|
||||||
|
|
||||||
|
public synchronized void downloadImage(String url, JSONObject body, String saveName, String method, DownloadInterface downloadInterface) {
|
||||||
|
if (!StringUtil.isEmpty(NetworkAPI.HOME) && !url.startsWith("http")) {
|
||||||
|
downloadImage(NetworkAPI.HOME + url, body, saveName, method, downloadInterface);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (downloadImageList.contains(url + "?" + body.toJSONString())) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
downloadImageList.add(url + "?" + body.toJSONString());
|
||||||
|
}
|
||||||
|
OkHttpClient client = getClient();
|
||||||
|
Request request;
|
||||||
|
Request.Builder requestBuilder = new Request.Builder();
|
||||||
|
requestBuilder.url(url);
|
||||||
|
if (method.equals("post")) {
|
||||||
|
FormBody.Builder bodyBuilder = new FormBody.Builder();
|
||||||
|
for (String key : body.keySet()) {
|
||||||
|
bodyBuilder.add(key, body.getString(key));
|
||||||
|
}
|
||||||
|
requestBuilder.post(bodyBuilder.build());
|
||||||
|
}
|
||||||
|
request = requestBuilder.build();
|
||||||
|
client.newCall(request)
|
||||||
|
.enqueue(new Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||||
|
downloadInterface.onError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||||
|
File jar = null;
|
||||||
|
try {
|
||||||
|
File savePath = new File(downloadPath + saveName + "_tmp.tmp");
|
||||||
|
if (!savePath.exists()) {
|
||||||
|
savePath.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
Log.i("donwloadImage", "下载文件:" + url + "?" + body + " 保存文件:" + saveName);
|
||||||
|
Log.i("downloadImageCode", response.code() + "");
|
||||||
|
|
||||||
|
InputStream inputStream = response.body().byteStream();
|
||||||
|
jar = new File(downloadPath + File.separator + "image_cache" + File.separator + saveName + "_tmp.tmp");
|
||||||
|
if (!new File(downloadPath + File.separator + "image_cache").exists()) {
|
||||||
|
new File(downloadPath + File.separator + "image_cache").mkdirs();
|
||||||
|
}
|
||||||
|
if (!jar.exists()) {
|
||||||
|
jar.createNewFile();
|
||||||
|
}
|
||||||
|
Log.i(NetworkTool.class, "临时保存文件:" + jar.getAbsolutePath());
|
||||||
|
OutputStream outputStream = new FileOutputStream(jar);
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = inputStream.read(bytes)) > 0) {
|
||||||
|
outputStream.write(bytes, 0, len);
|
||||||
|
}
|
||||||
|
outputStream.close();
|
||||||
|
inputStream.close();
|
||||||
|
File oldJar = new File(downloadPath + saveName);
|
||||||
|
/*if (oldJar.exists()) {
|
||||||
|
oldJar.delete();
|
||||||
|
}*/
|
||||||
|
jar.renameTo(oldJar);
|
||||||
|
Log.i(NetworkTool.class, "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
|
||||||
|
Log.toast(MyApplication.application, "下载完成,重启生效");
|
||||||
|
|
||||||
|
if (downloadInterface != null) {
|
||||||
|
downloadInterface.onDownloadOver(oldJar);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
if (downloadInterface != null)
|
||||||
|
downloadInterface.onError(e);
|
||||||
|
}
|
||||||
|
downloadImageList.remove(url + "?" + body.toJSONString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private OkHttpClient getClient() {
|
||||||
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(3, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user