okhttpUtils
使用okhttpUtils
String params2 = "?/service/service/list?pageNum=1&pageSize=10";
OkHttpUtils okHttp2 = OkHttpUtils.getInstance();
okHttp2.syncJsonStringByURL(Connectinfo.queryurl + params2, new OkHttpUtils.FuncJsonString() {
private static final String TAG = "HomeFragment";
@Override
public void onResponse(String result) {
queryData = new Gson().fromJson(result, QueryData.class);
for (int i = 0; i < queryData.getRows().size(); i++) {
queryList_img.add(Connectinfo.contexturl + queryData.getRows().get(i).getImgUrl());
}
initQuery();
}
});
picasso使用
PicassoUtils.loadImageViewCrop2(getActivity(), contexturl, imageView);
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Map;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttpUtils {
private static final String TAG = OkHttpUtils.class.getSimpleName();
private OkHttpClient client;
private volatile static OkHttpUtils okHttpUtils;
private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
private static final MediaType STRING = MediaType.parse("text/x-markdown;charset=utf-8");
private Handler handler;
private OkHttpUtils() {
client = new OkHttpClient();
handler = new Handler(Looper.getMainLooper());
}
public static OkHttpUtils getInstance() {
OkHttpUtils okUtils = new OkHttpUtils();
return okUtils;
}
public static void GetDataByUrl(final String url, final Callback callback) {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
}).start();
}
public void GetDataByUrl(final String url, Map<String, String> params, final Callback callback) {
final String u2;
String urls=url+"?";
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
urls=urls+entry.getKey()+"="+entry.getValue()+"&";
}
}
u2=urls;
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder().url(u2).build();
okHttpClient.newCall(request).enqueue(callback);
}
}).start();
}
private void onsuccessJsonStringMethod(final String jsonValue, final FuncJsonString callBack) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
try {
callBack.onResponse(jsonValue);
} catch (Exception e) {
}
}
}
});
}
private void onsuccessJsonObjectMethod(final String jsonValue, final FuncJsonObject callBack) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
try {
callBack.onResponse(new JSONObject(jsonValue));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
}
private void onsuccessJsonByteMethod(final byte[] data, final FuncJsonObjectByte callBack) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
callBack.onResponse(data);
}
}
});
}
public String syncGetByURL(String url) {
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
}
return null;
}
public void syncJsonStringByURL(String url, final FuncJsonString callback) {
final Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
Log.i(TAG, "请求失败");
}
@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onsuccessJsonStringMethod(response.body().string(), callback);
}
}
});
}
public void syscJsonObjectByURL(String url, final FuncJsonObject callback) {
final Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
Log.i(TAG, "解析失败");
}
@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onsuccessJsonObjectMethod(response.body().string(), callback);
}
}
});
}
public void syscGetByteByURL(String url, final FuncJsonObjectByte callback) {
final Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
Log.i(TAG, "解析失败");
}
@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onsuccessJsonByteMethod(response.body().bytes(), callback);
}
}
});
}
public void syscDownloadImageByURL(String url, final FuncJsonObjectBitmap callback) {
final Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
Log.i(TAG, "解析失败");
}
@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull Response response) throws IOException {
if (response != null && response.isSuccessful()) {
byte[] data = response.body().bytes();
Bitmap bitmap = new PicassoUtils.CropImageView().transform(BitmapFactory.decodeByteArray(data, 0, data.length));
callback.onResponse(bitmap);
}
}
});
}
public void sendDatafForClicent(String url, Map<String, String> params, final FuncJsonObject callback) {
FormBody.Builder from = new FormBody.Builder();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
from.add(entry.getKey(), entry.getValue());
}
}
RequestBody body = from.build();
Request request = new Request.Builder().url(url).post(body).build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
Log.i(TAG, "解析失败");
}
@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onsuccessJsonObjectMethod(response.body().string(), callback);
}
}
});
}
public interface FuncJsonString {
void onResponse(String result);
}
interface FuncJsonObject {
void onResponse(JSONObject jsonObject);
}
interface FuncJsonObjectByte {
void onResponse(byte[] result);
}
interface FuncJsonObjectBitmap {
void onResponse(Bitmap bitmap);
}
}
Picasso
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
public class PicassoUtils {
public static void loadImageViewSize(Context mContext, String path, int width, int height, ImageView mImageView) {
Picasso.get().load(path).resize(width, height).centerCrop().into(mImageView);
}
public static void loadImageViewSize2(Context mContext, String path, ImageView mImageView) {
Picasso.get().load(path).centerCrop().into(mImageView);
}
public static void loadImageViewHolder(Context mContext, String path, ImageView mImageView) {
Picasso.get().load(path).fit().into(mImageView);
}
public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView) {
}
public static void loadImageViewCrop2(Context mContext, String path, ImageView mImageView) {
Picasso.get().load(path).into(mImageView);
}
public static class CropImageView implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap newBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (newBitmap != null) {
source.recycle();
}
return newBitmap;
}
@Override
public String key() {
return null;
}
}
}