Search in sources :

Example 1 with KeyValue

use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.

the class BaseParams method getRequestBody.

public RequestBody getRequestBody() throws IOException {
    checkBodyParams();
    if (this.requestBody != null) {
        return this.requestBody;
    }
    RequestBody result = null;
    if (!TextUtils.isEmpty(bodyContent)) {
        result = new StringBody(bodyContent, charset);
    } else if (multipart || fileParams.size() > 0) {
        if (!multipart && fileParams.size() == 1) {
            for (KeyValue kv : fileParams) {
                String contentType = null;
                Object value = kv.value;
                if (value instanceof BodyItemWrapper) {
                    BodyItemWrapper wrapper = (BodyItemWrapper) value;
                    value = wrapper.getValue();
                    contentType = wrapper.getContentType();
                }
                if (value instanceof File) {
                    result = new FileBody((File) value, contentType);
                } else if (value instanceof InputStream) {
                    result = new InputStreamBody((InputStream) value, contentType);
                } else if (value instanceof byte[]) {
                    result = new InputStreamBody(new ByteArrayInputStream((byte[]) value), contentType);
                } else if (value instanceof String) {
                    // invoke addBodyParameter(key, stringValue, contentType)
                    result = new StringBody((String) value, charset);
                    result.setContentType(contentType);
                } else {
                    LogUtil.w("Some params will be ignored for: " + this.toString());
                }
                break;
            }
        } else {
            multipart = true;
            result = new MultipartBody(fileParams, charset);
        }
    } else if (bodyParams.size() > 0) {
        result = new UrlEncodedParamsBody(bodyParams, charset);
    }
    return result;
}
Also used : KeyValue(org.xutils.common.util.KeyValue) FileBody(org.xutils.http.body.FileBody) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BodyItemWrapper(org.xutils.http.body.BodyItemWrapper) UrlEncodedParamsBody(org.xutils.http.body.UrlEncodedParamsBody) StringBody(org.xutils.http.body.StringBody) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.xutils.http.body.MultipartBody) InputStreamBody(org.xutils.http.body.InputStreamBody) JSONObject(org.json.JSONObject) File(java.io.File) RequestBody(org.xutils.http.body.RequestBody)

Example 2 with KeyValue

use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.

the class BaseParams method removeParameter.

public void removeParameter(String name) {
    if (!TextUtils.isEmpty(name)) {
        Iterator<KeyValue> it = queryStringParams.iterator();
        while (it.hasNext()) {
            KeyValue kv = it.next();
            if (name.equals(kv.key)) {
                it.remove();
            }
        }
        it = bodyParams.iterator();
        while (it.hasNext()) {
            KeyValue kv = it.next();
            if (name.equals(kv.key)) {
                it.remove();
            }
        }
        it = fileParams.iterator();
        while (it.hasNext()) {
            KeyValue kv = it.next();
            if (name.equals(kv.key)) {
                it.remove();
            }
        }
    } else {
        bodyContent = null;
    }
}
Also used : KeyValue(org.xutils.common.util.KeyValue)

Example 3 with KeyValue

use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.

the class BaseParams method toJSONString.

public String toJSONString() {
    List<KeyValue> list = new ArrayList<KeyValue>(queryStringParams.size() + bodyParams.size());
    list.addAll(queryStringParams);
    list.addAll(bodyParams);
    try {
        JSONObject jsonObject = null;
        if (!TextUtils.isEmpty(bodyContent)) {
            jsonObject = new JSONObject(bodyContent);
        } else {
            jsonObject = new JSONObject();
        }
        params2Json(jsonObject, list);
        return jsonObject.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : KeyValue(org.xutils.common.util.KeyValue) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 4 with KeyValue

use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.

the class BaseParams method params2Json.

private void params2Json(final JSONObject jsonObject, final List<KeyValue> paramList) throws JSONException {
    HashSet<String> arraySet = new HashSet<String>(paramList.size());
    LinkedHashMap<String, JSONArray> tempData = new LinkedHashMap<String, JSONArray>(paramList.size());
    for (int i = 0; i < paramList.size(); i++) {
        KeyValue kv = paramList.get(i);
        final String key = kv.key;
        if (TextUtils.isEmpty(key))
            continue;
        JSONArray ja = null;
        if (tempData.containsKey(key)) {
            ja = tempData.get(key);
        } else {
            ja = new JSONArray();
            tempData.put(key, ja);
        }
        ja.put(RequestParamsHelper.parseJSONObject(kv.value));
        if (kv instanceof ArrayItem) {
            arraySet.add(key);
        }
    }
    for (Map.Entry<String, JSONArray> entry : tempData.entrySet()) {
        String key = entry.getKey();
        JSONArray ja = entry.getValue();
        if (ja.length() > 1 || arraySet.contains(key)) {
            jsonObject.put(key, ja);
        } else {
            Object value = ja.get(0);
            jsonObject.put(key, value);
        }
    }
}
Also used : KeyValue(org.xutils.common.util.KeyValue) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with KeyValue

use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.

the class SqlInfoBuilder method buildUpdateSqlInfo.

public static SqlInfo buildUpdateSqlInfo(TableEntity<?> table, WhereBuilder whereBuilder, KeyValue... nameValuePairs) throws DbException {
    if (nameValuePairs == null || nameValuePairs.length == 0)
        return null;
    SqlInfo result = new SqlInfo();
    StringBuilder builder = new StringBuilder("UPDATE ");
    builder.append("\"").append(table.getName()).append("\"");
    builder.append(" SET ");
    for (KeyValue kv : nameValuePairs) {
        builder.append("\"").append(kv.key).append("\"").append("=?,");
        result.addBindArg(kv);
    }
    builder.deleteCharAt(builder.length() - 1);
    if (whereBuilder != null && whereBuilder.getWhereItemSize() > 0) {
        builder.append(" WHERE ").append(whereBuilder.toString());
    }
    result.setSql(builder.toString());
    return result;
}
Also used : KeyValue(org.xutils.common.util.KeyValue)

Aggregations

KeyValue (org.xutils.common.util.KeyValue)15 JSONObject (org.json.JSONObject)3 ArrayList (java.util.ArrayList)2 ColumnEntity (org.xutils.db.table.ColumnEntity)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 DbManager (org.xutils.DbManager)1 ColumnConverter (org.xutils.db.converter.ColumnConverter)1 DbModel (org.xutils.db.table.DbModel)1 DbException (org.xutils.ex.DbException)1 BodyItemWrapper (org.xutils.http.body.BodyItemWrapper)1