Example usage for com.squareup.okhttp FormEncodingBuilder add

List of usage examples for com.squareup.okhttp FormEncodingBuilder add

Introduction

In this page you can find the example usage for com.squareup.okhttp FormEncodingBuilder add.

Prototype

public FormEncodingBuilder add(String name, String value) 

Source Link

Document

Add new key-value pair.

Usage

From source file:zblibrary.demo.manager.HttpRequest.java

License:Apache License

/**
 * @param paramList/*from   ww w. j  a  va2  s.c om*/
 *            ??
 * @param url
 *            ?url
 * @param requestCode
 *            ?onActivityResult??activity??<br/>
 *             ???
 *            {@link OnHttpResponseListener#onHttpRequestError(int, Exception)}
 *             <br/>
 *            {@link OnHttpResponseListener#onHttpRequestError(int, Exception)}
 * <br/>
 *            activity?requestCode??serverResultCode???
 *            json?json? 
 *
 * @param listener
 */
private void httpPost(final List<Parameter> paramList, final String url, final int requestCode,
        final OnHttpResponseListener listener) {

    new AsyncTask<Void, Void, Exception>() {

        int resultCode;
        String resultData;

        @Override
        protected Exception doInBackground(Void... params) {
            OkHttpClient client = getHttpClient(url);
            if (client == null) {
                return new Exception("httpPost  AsyncTask.doInBackground  client == null >> return;");
            }

            FormEncodingBuilder fBuilder = new FormEncodingBuilder();
            if (paramList != null) {
                for (Parameter p : paramList) {
                    fBuilder.add(p.key, p.value);
                }
            }

            JSONObject jsonObject = null;
            try {
                jsonObject = getResponseObject(client,
                        new Request.Builder().addHeader("token", getToken(paramList))
                                .url(StringUtil.getNoBlankString(url)).post(fBuilder.build()).build());
            } catch (Exception e) {
                Log.e(TAG, "httpPost  AsyncTask.doInBackground  try {  jsonObject = getResponseObject(..."
                        + "} catch (Exception e) {\n" + e.getMessage());
                return e;
            }

            resultCode = getResponseCode(jsonObject);
            resultData = getResponseData(jsonObject);

            return null;
        }

        @Override
        protected void onPostExecute(Exception exception) {
            super.onPostExecute(exception);
            httpPostExecute(listener, requestCode, exception, resultCode, resultData);
        }

    }.execute();
}

From source file:zuo.biao.library.manager.HttpManager.java

License:Apache License

/**POST
 * @param paramList ??//from   w ww. j  a v a 2  s. c  o  m
 * @param url ?url
 * @param requestCode
 *            ?onActivityResult??activity?????
 *            {@link OnHttpResponseListener#onHttpResponse(int, String, Exception)}<br>  
 *            ??requestCode??
 * @param listener
 */
public void post(final List<Parameter> paramList, final String url, final int requestCode,
        final OnHttpResponseListener listener) {

    new AsyncTask<Void, Void, Exception>() {

        String result;

        @Override
        protected Exception doInBackground(Void... params) {
            OkHttpClient client = getHttpClient(url);
            if (client == null) {
                return new Exception(TAG + ".post  AsyncTask.doInBackground  client == null >> return;");
            }

            FormEncodingBuilder fBuilder = new FormEncodingBuilder();
            if (paramList != null) {
                for (Parameter p : paramList) {
                    fBuilder.add(StringUtil.getTrimedString(p.key), StringUtil.getTrimedString(p.value));
                }
            }

            try {
                result = getResponseJson(client, new Request.Builder().addHeader(KEY_TOKEN, getToken(url))
                        .url(StringUtil.getNoBlankString(url)).post(fBuilder.build()).build());
                // result = "{\"code\":102}";
            } catch (Exception e) {
                Log.e(TAG, "post  AsyncTask.doInBackground  try {  result = getResponseJson(..."
                        + "} catch (Exception e) {\n" + e.getMessage());
                return e;
            }

            return null;
        }

        @Override
        protected void onPostExecute(Exception exception) {
            super.onPostExecute(exception);
            listener.onHttpResponse(requestCode, result, exception);
        }

    }.execute();
}