解决:java.io.IOException: unexpected end of stream on Connection{...

java.io.IOException: unexpected end of stream on Connection

    public static void postControl(String url, String postName, Boolean value) {
        // 定义okhttp
        OkHttpClient okHttpClient_post = new OkHttpClient.Builder()

                .readTimeout(8, TimeUnit.SECONDS)                  //设置请求超时
                .writeTimeout(8, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
        // 定义请求体 执行okhttp
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        JSONObject json = new JSONObject();
        try {
            Log.d("打印Control传入的值:", postName + "," + value);

            json.put(postName, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //将传入的参数,转换成json格式
        RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
        Request request = new Request.Builder()
                .post(requestBody) //post请求方式
                .url(url)
                .build();
        try {
            Response response = okHttpClient_post.newCall(request).execute();

            if (response.isSuccessful()) {
                String result = response.body().string();
                System.out.println("打印Post 返回的数据:" + result);
                //解析返回值,判断设置是否成功
                if (TextUtils.isEmpty(result)) {         //判断返回数据是否为空
                    Log.d("打印:判断返回结果", "result 为空,数据刷新");
                
                } else {
                    jsonResult(result, postName);           
                }
            } else {
                throw new IOException("Unexpected code " + response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 添加 

.addNetworkInterceptor(new NetInterceptor())                //添加拦截器

 添加后:

    // 定义okhttp
        OkHttpClient okHttpClient_post = new OkHttpClient.Builder()
                .addNetworkInterceptor(new NetInterceptor())                //添加拦截器
                .readTimeout(8, TimeUnit.SECONDS)                  //设置请求超时
                .writeTimeout(8, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();

NetInterceptor:

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 拦截器
 */
public class NetInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request().newBuilder()

                .addHeader("Connection","close").build();

        return chain.proceed(request);

    }
}

最后附上解析,及其get方法:

    /**
     * @param result 解析返回值   
     * @param name   协传的 选项名
     *               {"success":true , "msg":"set success!"}
     */
    private static void jsonResult(String result, String name) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            boolean isSuccess = jsonObject.getBoolean("success");
            String msg = jsonObject.getString("msg");
            System.out.println("打印出解析是否成功的Json值:" + isSuccess + "," + msg);
            //如果  isSuccess ,则弹出友好提示,绑定、锁车、设置成功
            if (isSuccess) {
                // 通知前台,直接刷新下 控制列表

            } else {
                //通知,设置失败
             
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

get请求:

    /**
     * okHttp 请求获取数据
     *
     * @param url 请求地址
     */
    public static void getWorkInfo(String url) {
        // 定义okhttp
        OkHttpClient okHttpClient_get = new OkHttpClient.Builder()
                .addNetworkInterceptor(new NetInterceptor())                //添加拦截器
                .readTimeout(8, TimeUnit.SECONDS)                  //设置请求超时
                .writeTimeout(8, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
        // 请求体
        Request request = new Request.Builder()
                .get()//get请求方式
                .url(url)//网址
                .build();
        try {
            Response response = okHttpClient_get.newCall(request).execute();
            if (response.isSuccessful()) {
                // 打印数据
                String result = response.body().string();
                System.out.println("getWorkInfo的数据 :" + result);
                if (TextUtils.isEmpty(result)) {         //判断返回数据是否为空
                    Log.d("打印:getWorkInfo", "result 为空");
                            //若为空  通知前台无数据返回
    
                } else {
                    jsonWorkInfo(result);
                }
            } else {
                throw new IOException("Unexpected code " + response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

你可能感兴趣的:(error,Java)