5

I been following online tutorials on this code. and will publish google play with android api 23 Marshmallow.

 private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<String, String>();
    try {
        URI rawExtras = new URI("?" + extras);
        List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
        for (NameValuePair item : extraList) {
            String name = item.getName();
            int i = 0;
            while (results.containsKey(name)) {
                name = item.getName() + ++i;
            }
            results.put(name, item.getValue());
        }
    } catch (URISyntaxException e) {
        Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
    }
    return results;
}

But NameValuePair and URLEncodedUtils have been deleted in api23 Marshmallow . How can I change this code?

Please help me.

조풍연
  • 59
  • 1
  • 2
  • 1
    possible duplicate of [How to use the legacy Apache HTTP client on Android M?](http://stackoverflow.com/questions/31653002/how-to-use-the-legacy-apache-http-client-on-android-m) – Kane O'Riley Sep 11 '15 at 08:56

5 Answers5

7

Just use Retrofit

OR

Use HttpURLConnection

OR

Add this to your build.gradle (but it is not suggested)

android {
    useLibrary 'org.apache.http.legacy'
}
localhost
  • 5,568
  • 1
  • 33
  • 53
2

Use httpMime.jar send data to server database.

Download the.jar from here and paste it in your libs folder.

Usage:

MultipartEntity multi = new MultipartEntity();
multi.addPart("username", new StringBody("Kgandroid"));
multi.addPart("password", new StringBody("nopass"));

This is a perfect alternative for namevaluepairs.

For URLEncodedUtils:

Read this.

You will find:

This preview removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption.

So now we have to use HttpUrlConnection

A simple tutorial to use HttpUrlConnection will be found here.

kgandroid
  • 5,507
  • 5
  • 39
  • 69
2

Here is the equivalent code using android.net.Uri for API level 11 (Honeycomb) and above. This code is pretty simple, so it really does not make sense to pull in 3rd party APIs or legacy APIs just to avoid this one issue.

private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<>();
    Uri uri = new Uri.Builder().encodedQuery(extras).build();
    Set<String> parameterNames = uri.getQueryParameterNames();
    for (String parameterName : parameterNames) {
        List<String> values = uri.getQueryParameters(parameterName);
        int count = values.size();
        if (count >= 1) {
            results.put(parameterName, values.get(0));
            for (int i = 1; i < count; ++i) {
                results.put(parameterName + i, values.get(i));
            }
        }
    }
    return results;
}
Uli
  • 2,828
  • 20
  • 23
0

You can manually add HttpClient library into your project

yelliver
  • 5,648
  • 5
  • 34
  • 65
0

It's not exactly what you are looking for but it is really close:

Uri.Builder builder = new Uri.Builder()
  .query(extras);
Uri uri = builder.build();
Set<String> keys = uri.getQueryParameterNames();
Iterator<String> iterator = x.iterator();
HashMap<String, List<String>> map = new HashMap<>();
while(iterator.hasNext()) {
       String key = iterator.next();
       List<String> values = uri.getQueryParameters(key);
       map.put(key, values);
}

it creates a Uri from the former parameter extras of your method, that looks like the query part of your url. Upon that you can call getQueryParameterNames to retrieve a Set of the keys and from each key a List<String> which represents every value of your that extras contains for that particular key.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305