Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

maximerauch/android-json-to-requestparams

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🔧 JSON to RequestParams — Legacy Bridge

Java Android Target Status License

Legacy Android utility to convert JSON objects into application/x-www-form-urlencoded parameters — created to bridge compatibility gaps with older PHP backends that didn’t support raw JSON payloads.

📖 The Story

This small library was built to solve a real interoperability issue: many legacy PHP backends (circa Android 4.x – 5.x) ignored application/json bodies and only accepted application/x-www-form-urlencoded data.

To work around this, I wrote a lightweight converter turning nested JSONObject or JSONArray structures into flattened key-value pairs compatible with libraries such as Loopj Android Async HTTP (RequestParams).

Although obsolete today, it remains a good example of problem-driven engineering, legacy compatibility, and pragmatic design under tight platform constraints.

🧩 Features

  • Converts any JSONObject (including nested objects and arrays) into URL-encoded key/value pairs.
  • Generates PHP-style parameter names:
    • users[0][name]=Jean
    • users[0][age]=24
    • users[0][city]=Strasbourg, …
  • Works out-of-the-box with RequestParams or any HTTP client expecting x-www-form-urlencoded.
  • No external dependencies — simple and portable.

💻 Example

JSONObject params = new JSONObject();
JSONArray users = new JSONArray();
JSONObject user = new JSONObject();

try
{
    user.put("name", "Jean");
    user.put("age", "24");
    user.put("city", "Strasbourg");
    users.put(user);
    params.put("users", users);
}
catch(JSONException e)
{
    // ...
}

RequestParams requestParams = JsonHelper.toRequestParams(params);

AsyncHttpClient client = new AsyncHttpClient();

client.post(context, url, requestParams, "application/x-www-form-urlencoded", new JsonHttpResponseHandler()
{
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response)
    {
        //...       
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject errorResponse)
    {
        // ...
    }
})

🛠️ How to Use

  1. Copy the JsonHelper.java (converter) file into your Android project.
  2. Convert your JSONObject before sending the request.
  3. Use with your preferred HTTP client (Loopj, OkHttp wrapper, etc.).

⚠️ Note: This library exists for legacy compatibility only.
Modern APIs should send raw JSON (application/json) via Retrofit, OkHttp, or similar.

🧠 Engineering Takeaways

  • Backward compatibility: adapting to systems that cannot evolve immediately.
  • Data transformation: mapping nested JSON into flattened form-encoded structures.
  • Pragmatic problem-solving: delivering a working bridge instead of rewriting entire systems.
  • Maintainability: small, dependency-free, single-purpose utility.

🕰️ Why Archived

This project is archived and kept online as a historical example of interoperability between Android clients and legacy PHP servers. It remains a reminder that *clean abstractions and problem-driven design can simplify even constrained environments.

About

Legacy Android JSON → form-encoded converter for old PHP APIs (historical project)

Topics

Resources

License

Stars

Watchers

Forks

Languages