OKhttp

来源:互联网 发布:wampserver配置多域名 编辑:程序博客网 时间:2024/06/07 03:20
public class Myapp extends Application{    private static OkHttpClient okHttpClient;    @Override    public void onCreate() {        super.onCreate();        okHttpClient = new OkHttpClient();        okHttpClient = okHttpClient.newBuilder()                .connectTimeout(5, TimeUnit.SECONDS)                .readTimeout(5,TimeUnit.SECONDS)                .build();    }    public static OkHttpClient okHttpClient(){        return okHttpClient;    }
 okHttpClient = Myapp.okHttpClient();        Button btn_get = (Button) findViewById(R.id.btn_get);        Button btn_post = (Button) findViewById(R.id.btn_post);        btn_get.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Request request=new Request.Builder()                        .url("http://www.baidu.com")                        .build();                okHttpClient.newCall(request).enqueue(new Callback() {                    @Override                    public void onFailure(Call call, IOException e) {                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                Toast.makeText(MainActivity.this, "get失败", Toast.LENGTH_SHORT).show();                            }                        });                    }                    @Override                    public void onResponse(Call call, Response response) throws IOException {                        if (response.isSuccessful()){                            String json = response.body().string();                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    Toast.makeText(MainActivity.this, "get成功", Toast.LENGTH_SHORT).show();                                }                            });                        }                    }                });            }        });        btn_post.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                FormBody formBody=new FormBody.Builder()                        .add("type", "yuantong")                        .add("postid", "11111111111")                        .build();                Request request=new Request.Builder()                        .url("http://www.kuaidi100.com/query")                        .post(formBody)                        .build();                okHttpClient.newCall(request).enqueue(new Callback() {                    @Override                    public void onFailure(Call call, IOException e) {                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                Toast.makeText(MainActivity.this, "post失败", Toast.LENGTH_SHORT).show();                            }                        });                    }                    @Override                    public void onResponse(Call call, Response response) throws IOException {                        if (response.isSuccessful()){                            String json = response.body().string();                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    Toast.makeText(MainActivity.this, "post成功", Toast.LENGTH_SHORT).show();                                }                            });                        }                    }                });            }        });    }}

原创粉丝点击