[英]Is it alright if I use 'org.apache.http.legacy' and continue using deprecated stuffs like NameValuePair and others?
我正在嘗試將CCAvenue付款網關集成到我的android應用程序中。 為此,我下載了他們的集成套件並使用了它。 但是,這是該套裝目前進口org.apache.http並使用它的功能就像httppost,的NameValuePair等,所以,我的問題是,提到這里 ,是正常的,使下面提及的gradle這個變化,並繼續使用過時圖書館?
android {
useLibrary 'org.apache.http.legacy'
}
將此代碼段放入ServiceHandler類中,您無需使用legacy。 您可以根據需要更改它們的代碼,這些只是示例。
/**
*
* @param postUrl
* @param postParams
* @return response in string
*/
public static String makeServiceCall(final String postUrl, final Map<String, String> postParams) {
Log.e("URL#",postUrl);
StringBuilder responseBuilder = new StringBuilder();
HttpURLConnection conn = null;
try {
final URL mUrl = new URL(postUrl);
conn = (HttpURLConnection) mUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android-4.0.3; en-us; Galaxy Nexus Build/IML74K) AppleWebKit/535.7 (KHTML, like Gecko) CrMo/16.0.912.75 Mobile Safari/535.7");
conn.connect();
conn.setReadTimeout(180000);
conn.setConnectTimeout(180000);
final OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(postParams));
writer.flush();
writer.close();
os.close();
final int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
responseBuilder.append(line);
}
} else {
responseBuilder.append("");
}
} catch (MalformedURLException e) {
e.printStackTrace();
responseBuilder.append(e.getMessage());
return responseBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
responseBuilder.append(e.getMessage());
return responseBuilder.toString();
} finally {
if (null != conn) {
conn.disconnect();
}
}
System.gc();
return responseBuilder.toString();
}
/**
* @Param: map , takes in value in key val format
*/
private static String getQuery(final Map<String, String> mPostItems) throws UnsupportedEncodingException {
final StringBuilder result = new StringBuilder();
boolean first = true;
final Set<String> mKeys = mPostItems.keySet();
for (String key : mKeys) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(mPostItems.get(key), "UTF-8"));
Log.e("Key#",key+"#"+mPostItems.get(key));
}
return result.toString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.