2017-01-23 15 views
0

我正在尝试将CCAvenue支付网关集成到我的Android应用程序中。为此,我下载了他们的集成套件并使用它。但是,目前可用的工具包会导入org.apache.http并使用它的功能,例如httppost,namevaluepair等。所以,我的问题是,正如here所述,是否可以在Gradle中进行下面提到的更改并继续使用已弃用的库?如果我使用'org.apache.http.legacy'并继续使用已弃用的东西(如NameValuePair和其他),它会好吗?

android { 
    useLibrary 'org.apache.http.legacy' 
} 
+1

如果他们的套件正在使用,那么没有选择给你,所以你可以使用遗留。 – Spartan

回答

0

将这个代码片段中的ServiceHandler类,您可以不必使用传统的。您可以根据您的要求更改其代码,这些只是样本。

/** 
* 
* @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(); 
} 
+0

为什么OP“将这段代码片段放入ServiceHandler”?一个好的答案**将总是解释所做的事情以及为什么这样做,不仅是为了OP,而且是为了将来SO的访问者。 –

相关问题