Example usage for com.squareup.okhttp FormEncodingBuilder build

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

Introduction

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

Prototype

public RequestBody build() 

Source Link

Usage

From source file:studio.imedia.vehicleinspection.activity.PersonalInfoActivity.java

/**
 * ?//from  w  ww .  j a v a  2 s.c o  m
 *
 * @param urlSB
 * @param avatarPath
 */
private void uploadInfo(StringBuffer urlSB, String avatarPath) {
    Log.d("submit", "url " + urlSB.toString());
    String url = urlSB.toString();
    int id = (int) SPUtil.get(mContext, Constant.Key.USER_ID, Constant.Type.INTEGER);
    mUsername = tvUsername.getText().toString().trim();
    String gender = tvGender.getText().toString();
    if (gender.equals(""))
        mGender = MALE;
    else
        mGender = FEMALE;
    mSignature = etSignature.getText().toString().trim();

    // ?
    FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
    formEncodingBuilder.add("id", String.valueOf(id)).add("name", mUsername)
            .add("gender", String.valueOf(mGender)).add("signature", mSignature);
    if (isAvatarUpdate && avatarPath != null)
        formEncodingBuilder.add("avatar", avatarPath);

    RequestBody formBody = formEncodingBuilder.build();

    final Request request = new Request.Builder().url(url).post(formBody).build();

    mClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            Toast.makeText(mContext, "??", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful())
                throw new IOException("Unexpected code " + response);

            String jsonStr = response.body().string();
            Log.d("submit", "json " + jsonStr);
            try {
                int status = new JSONObject(jsonStr).getInt("status");
                if (status == 0)
                    mHandler.sendEmptyMessage(MSG_UPLOAD_INFO_SUCCESS);
                else
                    mHandler.sendEmptyMessage(MSG_UPLOAD_INFO_FAIL);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:studio.imedia.vehicleinspection.fragments.CarInfoFragment.java

/**
 * ?/*from   www  .  j a v a 2s .  c  om*/
 */
private void uploadCarInfo(StringBuffer urlSB, String picPath) {
    String url = urlSB.toString();
    // ??
    mLicensePicPath = picPath;
    mDetailedAddress = tvCity.getText().toString().trim();
    mEngineNum = etEngineNum.getText().toString().trim();
    mRegisterTime = tvRegisterDate.getText().toString().trim();

    // ?...
    FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder().add("id", String.valueOf(mId))
            .add("carBrandId", String.valueOf(mCarBrandId)).add("carTypeId", String.valueOf(mCarTypeId))
            .add("engineNum", mEngineNum).add("registerTime", mRegisterTime)
            .add("provinId", String.valueOf(mProvinceId)) // ?id
            .add("cityId", String.valueOf(mCityId)).add("countyId", String.valueOf(mCountyId))
            .add("detailedAddress", mDetailedAddress);
    if (isLicenseUpdate && picPath != null)
        formEncodingBuilder.add("licensePic", mLicensePicPath);

    RequestBody formBody = formEncodingBuilder.build();

    Request request = new Request.Builder().url(url).post(formBody).build();

    mClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONNECT_FAIL);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful())
                throw new IOException("Unexpected code " + response);

            String jsonStr = response.body().string();
            try {
                int status = new JSONObject(jsonStr).getInt("status");
                if (status == 0) {
                    mHandler.sendEmptyMessage(MSG_UP_INFO_SUCCESS);
                } else {
                    mHandler.sendEmptyMessage(MSG_UP_INFO_FAIL);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:tk.breezy64.pantex.core.Util.java

public static String post(String url, Map<String, String> params) throws IOException {
    OkHttpClient client = getClient(DEFAULT_SESSION);
    FormEncodingBuilder b = new FormEncodingBuilder();
    for (Map.Entry<String, String> e : params.entrySet()) {
        b.add(e.getKey(), e.getValue());
    }/*w  ww  .j  ava  2  s .  c  o  m*/

    Request req = new Request.Builder().post(b.build()).url(url).build();

    Response r = client.newCall(req).execute();
    System.out.println("Result: " + r.toString());
    return r.body().string();
}

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

License:Apache License

/**
 * @param paramList// ww w. j  a va 2  s  .c o m
 *            ??
 * @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  w  w . j  a va 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();
}