Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android programming NameValuePair removed for api 23

Tags:

java

android

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.

like image 766
조풍연 Avatar asked Sep 11 '15 07:09

조풍연


2 Answers

Just use Retrofit

OR

Use HttpURLConnection

OR

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

android {
    useLibrary 'org.apache.http.legacy'
}
like image 165
localhost Avatar answered Oct 17 '22 22:10

localhost


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.

like image 32
kgandroid Avatar answered Oct 17 '22 21:10

kgandroid