1101

mac2025-12-29  7

package com.example.a110101; import android.os.Handler; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class OkUtils { private Handler handler=new Handler(); private static OkUtils okUtils; private OkHttpClient client; private OkUtils(){ client=new OkHttpClient(); } public static OkUtils getInstance(){ if (okUtils==null){ synchronized (Object.class){ if (okUtils==null){ return okUtils=new OkUtils(); } } } return okUtils; } public void get(String url, final Listener listener){ Request request = new Request.Builder().url(url).get().build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { final String message = e.getMessage(); handler.post(new Runnable() { @Override public void run() { listener.error(message); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { listener.json(string); } }); } }); } public void post(String url, HashMap<String, String> map, final Listener listener){ Set<Map.Entry<String, String>> entries = map.entrySet(); FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String, String> i:entries) { builder.add(i.getKey(),i.getValue()); } FormBody formBody = builder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { listener.error( e.getMessage()); } }); } @Override public void onResponse(Call call, Response response) throws IOException { } }); } } package com.example.a110101; import android.os.Handler; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class OkUtils { private Handler handler=new Handler(); private static OkUtils okUtils; private OkHttpClient client; private OkUtils(){ client=new OkHttpClient(); } public static OkUtils getInstance(){ if (okUtils==null){ synchronized (Object.class){ if (okUtils==null){ return okUtils=new OkUtils(); } } } return okUtils; } public void get(String url, final Listener listener){ Request request = new Request.Builder().url(url).get().build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { final String message = e.getMessage(); handler.post(new Runnable() { @Override public void run() { listener.error(message); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { listener.json(string); } }); } }); } public void post(String url, HashMap<String, String> map, final Listener listener){ Set<Map.Entry<String, String>> entries = map.entrySet(); FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String, String> i:entries) { builder.add(i.getKey(),i.getValue()); } FormBody formBody = builder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { listener.error( e.getMessage()); } }); } @Override public void onResponse(Call call, Response response) throws IOException { } }); } } package com.example.a110101; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.ListView; public class Main2Activity extends AppCompatActivity { private static final String TAG = "Main2Activity"; private ListView listView; private String url="https://www.apiopen.top/satinApi?type=1&page=2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); OkUtils.getInstance().get(url, new Listener() { @Override public void error(String message) { } @Override public void json(String json) { Log.i(TAG, "json: " + json); } }); MyAdapter adapter = new MyAdapter(); listView.setAdapter(adapter); new MyAsyncTask(adapter).execute(url); } private void initView() { listView = (ListView) findViewById(R.id.listView); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="match_parent" android:layout_height="50dp" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入您的用户名" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入您的密码" android:inputType="textPassword" /> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" /> <Button android:textColor="#FFFFFF" android:background="#5460C5" android:onClick="login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> <View android:layout_width="match_parent" android:layout_height="10dp"></View> <Button android:textColor="#FFFFFF" android:background="#D43930" android:onClick="sign_in" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击注册" /> <View android:layout_width="match_parent" android:layout_height="10dp"></View> <Button android:background="#35100E" android:textColor="#FFFFFF" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注销" /> </LinearLayout> package com.example.a103101; import android.os.Handler; import android.util.Log; import java.io.File; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class OkHttpUtils { private Handler handler=new Handler(); private static OkHttpUtils okHttpUtils=null; private OkHttpClient client; private OkHttpUtils(){ client=new OkHttpClient.Builder().build(); } public static OkHttpUtils getInstance(){ if (okHttpUtils==null){ synchronized (Object.class){ if (okHttpUtils==null){ okHttpUtils=new OkHttpUtils(); } } } return okHttpUtils; } public void get(String url, final MyOkListener myOkListener){ Request request = new Request.Builder().url(url).build(); Log.i("client", "get: "+client); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { Log.i("TAG", "onFailure: 请求数据失败"+e.getMessage()); handler.post(new Runnable() { @Override public void run() { myOkListener.getError(e.getMessage()); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String string = response.body().string(); Log.i("TAG", "onFailure: 请求数据成功"+string); handler.post(new Runnable() { @Override public void run() { myOkListener.getJson(string); } }); } }); } public void post(String url){ } public void load(String url){ } public void upload(String url){ RequestBody requestBody = RequestBody.create(MediaType.parse("media/mp4"), new File("/storage/emulated/0/Movies/cnm.mp4")); MultipartBody multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "mmm.mp4",requestBody).build(); Request request = new Request.Builder().url("http://169.254.44.2/hfs/") .post(multipartBody) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { } }); } }
最新回复(0)