2017-12-26 12 views
0

キー値ペアをURLクエリパラメータに変換する必要があります。javaで利用できる関数があります。URLEncodedUtils.format(params, "utf-8") kotlinでの代替方法は何ですか?kotlinのURLEncodedUtils.format(params、 "utf-8")の代替手段は何ですか?

+0

を書きます。また、読みやすくするために 'URLEncodedUtils.format(this、" utf-8 ")'を呼び出す拡張関数 'Map.asQueryParams()'を追加することもできます。 –

+0

https://medium.com/@sudomax/getting-query-parameters-in-vert-x-v3-and-kotlin-1b8def1db566 – Prem

+0

ヘクターが言ったように! >あなたはKotlinでも使うことができますKotlinはJavaと相互運用可能ですので、大したことはありませんか?とにかくここでは、Javaで行われた明白な例です。単純にkotlinに変換すると、あなたのために仕事をします^ _^..それが助けてくれることを願っていますhttp://www.c-sharpcorner.com/UploadFile/1e5156/learn-about-how-従業員情報をローカルに送信する/ –

答えて

0
val param = ArrayList<NameValuePair>() 

その後、あなたのparamsを追加し、すぐ下のコードあなたもkotlinでそれを使用することができます

object Parser { 

var result = HashMap<String, String>() 

fun parseJsonObject(url: String, method: String, params: List<NameValuePair>): HashMap<String, String> { 
    var url = url 
    var iStream: InputStream ?= null 
    var data = "" 

    println("url*************" + url) 
    println("params*************" + params) 

    var httpClient: DefaultHttpClient? = null 
    try { 

     // check for request method 
     if (method === "POST") { 
      // request method is POST 
      // defaultHttpClient 
      if (httpClient == null) { 

       httpClient = DefaultHttpClient() 
      } 

      val httpPost = HttpPost(url) 
      httpPost.entity = UrlEncodedFormEntity(params) 

      // set timeout 
      HttpConnectionParams.setConnectionTimeout(httpClient.params, 20000) 
      HttpConnectionParams.setSoTimeout(httpClient.params, 30000) 

      val httpResponse = httpClient.execute(httpPost) 

      if (httpResponse.statusLine.statusCode === 200) { 
       val httpEntity = httpResponse.entity 
       iStream = httpEntity.content 
       val br = BufferedReader(
         InputStreamReader(iStream!!, "utf8")) 
       val sb = StringBuffer() 
       var line = "" 
       while ((line == br.readLine()) != null) { 
        sb.append(line) 
       } 
       data = sb.toString() 
       br.close() 
       iStream.close() 
       httpClient.connectionManager.closeExpiredConnections() 
       result.put("code", "200") 
       result.put("value", data) 
      } else { 
       httpClient.connectionManager.closeExpiredConnections() 
       result.put("code", "9999") 
       result.put("value", "") 
      } 

     } else if (method === "GET") { 
      // request method is GET 
      if (httpClient == null) { 
       httpClient = DefaultHttpClient() 
      } 
      val paramString = URLEncodedUtils.format(params, "utf-8") 
      url += "?" + paramString 
      val httpGet = HttpGet(url) 

      // set timeout 
      HttpConnectionParams.setConnectionTimeout(httpClient.params, 20000) 
      HttpConnectionParams.setSoTimeout(httpClient.params, 30000) 

      val httpResponse = httpClient.execute(httpGet) 

      if (httpResponse.statusLine.statusCode === 200) { 
       val httpEntity = httpResponse.entity 
       iStream = httpEntity.content 
       val br = BufferedReader(
         InputStreamReader(iStream!!, "utf8")) 
       val sb = StringBuffer() 
       var line = "" 
       while ((line == br.readLine()) != null) { 
        sb.append(line) 
       } 
       data = sb.toString() 
       br.close() 
       iStream.close() 
       httpClient.connectionManager.closeExpiredConnections() 
       result.put("code", "200") 
       result.put("value", data) 
      } else { 
       httpClient.connectionManager.closeExpiredConnections() 
       result.put("code", "9999") 
       result.put("value", "") 
      } 

     } 

    } catch (e: Exception) { 
     result.put("code", "9999") 
     result.put("value", "") 
     e.printStackTrace() 
     return result 
    } 

    return result 
} 

}

関連する問題