Example usage for com.squareup.okhttp Response body

List of usage examples for com.squareup.okhttp Response body

Introduction

In this page you can find the example usage for com.squareup.okhttp Response body.

Prototype

ResponseBody body

To view the source code for com.squareup.okhttp Response body.

Click Source Link

Usage

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Void doAddFeed(Call call) throws Exception {
    String body = null;//from w  w  w . ja  v  a 2  s . c  o m
    try {
        Response response = call.execute();
        body = response.body().string();

        if (body.equals("\"\\u8ba2\\u9605\\u5927\\u6210\\u529f\\u2192_\\u2192\"")) {
            return null;
        } else {
            throw new NMBException(ACSite.getInstance(), UNKNOWN);
        }
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Void doDelFeed(Call call) throws Exception {
    String body = null;/*  w w w .  j  av  a2 s .  c  om*/
    try {
        Response response = call.execute();
        body = response.body().string();

        if (body.equals("\"\\u53d6\\u6d88\\u8ba2\\u9605\\u6210\\u529f!\"")) {
            return null;
        } else {
            throw new NMBException(ACSite.getInstance(), UNKNOWN);
        }
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static Void doCreatePost(Call call) throws Exception {
    String body = null;//from  ww  w .java 2  s.c  om
    try {
        Response response = call.execute();
        ResponseUtils.storeCookies(response);
        body = response.body().string();

        try {
            JSONObject jo = JSON.parseObject(body);
            if (jo.getBoolean("success")) {
                return null;
            } else {
                throw new NMBException(ACSite.getInstance(), jo.getString("msg"));
            }
        } catch (Exception e) {
            if (body.contains("class=\"success\"")) {
                return null;
            } else {
                throw e;
            }
        }
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ac.ACEngine.java

License:Apache License

public static List<ACSearchItem> doSearch(Call call) throws Exception {
    String body = null;/*w  ww  . ja  va2  s. c om*/
    try {
        Response response = call.execute();
        body = response.body().string();

        JSONArray ja = JSON.parseObject(body).getJSONObject("hits").getJSONArray("hits");
        List<ACSearchItem> result = new ArrayList<>();
        for (int i = 0, n = ja.size(); i < n; i++) {
            JSONObject jo = ja.getJSONObject(i);
            ACSearchItem item = jo.getObject("_source", ACSearchItem.class);
            item.id = jo.getString("_id");
            item.generate(ACSite.getInstance());
            result.add(item);
        }

        return result;
    } catch (Exception e) {
        throwException(call, body, e);
        throw e;
    }
}

From source file:com.hippo.nimingban.client.ConvertEngine.java

License:Apache License

public static String doConvert(Call call) throws Exception {
    try {//from   w  w  w .  ja  v  a2  s.c  o  m
        Response response = call.execute();
        int code = response.code();
        if (code != 200) {
            throw new ResponseCodeException(code);
        }
        return response.body().string();
    } catch (IOException e) {
        if (call.isCanceled()) {
            throw new CancelledException();
        } else {
            throw e;
        }
    }
}

From source file:com.hippo.nimingban.client.UpdateEngine.java

License:Apache License

public static UpdateStatus doUpdate(Call call) throws Exception {
    try {//w w  w. ja va2s.  c  o m
        Response response = call.execute();
        String body = response.body().string();
        return JSON.parseObject(body, UpdateStatus.class);
    } catch (IOException e) {
        if (call.isCanceled()) {
            throw new CancelledException();
        } else {
            throw e;
        }
    }
}

From source file:com.hitkoDev.chemApp.rest.LoadDataTask.java

@Override
protected String doInBackground(String... urls) {
    String url = buildURL(urls);//from  w  ww  .  j  a  v a  2s . co  m
    file = new File(cache, IOLib.md5(url) + ".json");
    if (checkNetwork()) {
        OkHttpClient client = ChemApp.client;
        Request request = new Request.Builder().url(url).build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    } else if (file.exists()) {
        try (InputStream is = new FileInputStream(file)) {
            return IOLib.readStream(is);
        } catch (Exception ex) {
            return ex.toString();
        }
    } else {
        return "No network or cached files";
    }
}

From source file:com.hitkoDev.chemApp.rest.LoadImageTask.java

@Override
protected Bitmap doInBackground(String... urls) {
    file = new File(cache, IOLib.md5(urls[0]) + ".png");
    if (file.exists()) {
        return BitmapFactory.decodeFile(file.getPath());
    } else if (checkNetwork()) {
        OkHttpClient client = ChemApp.client;
        Request request = new Request.Builder().url(urls[0]).build();
        try {//w w  w. ja va  2  s  .c o m
            Response response = client.newCall(request).execute();
            Bitmap b = BitmapFactory.decodeStream(response.body().byteStream());
            new StoreFileTask().execute(b);
            return b;
        } catch (Exception ex) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:com.hitkoDev.chemApp.rest.SendJSONDataTask.java

@Override
protected String doInBackground(String... urls) {
    if (checkNetwork()) {
        String data = urls[0];//  www  .  ja v a2 s  .c om
        String url = buildURL(urls);
        OkHttpClient client = ChemApp.client;
        RequestBody body = RequestBody.create(IOLib.JSON, data);
        Request request = new Request.Builder().url(url).post(body).build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    } else {
        return "No network or cached files";
    }
}

From source file:com.hkm.root.Tasks.upload_data.java

License:Open Source License

protected String OCokHttpPostData(final String url, final String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder().url(url).post(body).build();
    OkHttpClient use_client = client.clone();
    Response response = use_client.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().string();
    } else//from   w w w  .  j  av a  2  s  .  c om
        throw new IOException("not success on HTTP request.");
}