各种网络请求

1.volley
依赖:implementation 'com.android.volley:volley:1.1.1'

网络权限:<uses-permission android:name="android.permission.INTERNET"/>

get请求:

RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(
Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
jsonData = response;
button.setClickable(true);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//textView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

post请求:

 String url = "https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist";

 RequestQueue queue = Volley.newRequestQueue(this);
        JSONObject object = new JSONObject();
        try {
            object.put("name", "123");
            object.put("pass", "123456");
            JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i("凉城", response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            queue.add(objectRequest);
            queue.start();
        } catch (JSONException pE) {
            pE.printStackTrace();
        }

2.okhttp
依赖:implementation 'com.squareup.okhttp3:okhttp:3.2.0'

String url = "https://www.wanandroid.com/project/tree/json";
OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        final Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("凉城data失败", e.getMessage());
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String string = response.body().string();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        InfoBean infoBean = new Gson().fromJson(string, InfoBean.class);
                        mAdapter.setBeans(infoBean.getData());
                    }
                });
            }
        });

3.Retrofit
依赖:implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiService.foodUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        Call<FoodBean> food = apiService.getFood("20", "1");
        food.enqueue(new Callback<FoodBean>() {
            @Override
            public void onResponse(Call<FoodBean> call, Response<FoodBean> response) {
                FoodBean body = response.body();
                Log.d(TAG, "onResponse: " + body.toString());
            }

            @Override
            public void onFailure(Call<FoodBean> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t.getMessage());
            }
        });

ApiService:

public static final String foodUrl="http://www.qubaobei.com/ios/";
    @GET("cf/dish_list.php?stage_id=1&")
    Call<FoodBean> getFood(@Query("limit")String limit,@Query("page")String page);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值