Reputation: 59
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.
Upvotes: 5
Views: 5317
Reputation: 3016
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;
}
Upvotes: 2
Reputation: 5595
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.
Upvotes: 2
Reputation: 5598
Just use Retrofit
OR
OR
Add this to your build.gradle
(but it is not suggested)
android {
useLibrary 'org.apache.http.legacy'
}
Upvotes: 7
Reputation: 157447
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.
Upvotes: 0