我正在从称为“ exchangerates api”的API获取ECB货币数据,以转换应用程序中的货币。从API返回的数据的格式为JSON。
在带有虚拟或USB有线设备的Android Studio上运行时,一切正常。但是,当要在Google Play上发布时,按下转换按钮并调用GetRate()
我认为它可能会出现,因为清单中没有像其他API一样使用“ API ID”和“密钥”。但是我找不到任何建议或话题。
我什至试图在Google API上创建一个服务帐户并使用它,但这是完全错误的主意,或者我做错了。
API的地址:
我在相关活动中的kotlin代码:
protected fun GetRate(view: View){
try {
fromFull = spinFrom.selectedItem.toString()
toFull = spinTo.selectedItem.toString()
fromRate = (fromFull as String).substring(0,3)
toRate = toFull.toString().substring(0,3)
val url = "https://api.exchangeratesapi.io/latest?base=$fromRate&symbols=$toRate"
//central bank based api above
MyAsyncTask().execute(url)
}catch (ex:Exception){}
}
inner class MyAsyncTask: AsyncTask<String, String, String>(){
override fun onPreExecute(){
//Before task start
}
override fun doInBackground(vararg params: String?): String {
try {
val url = URL(params[0])
val urlConnet = url.openConnection() as HttpURLConnection
urlConnet.connectTimeout=7000
var inString = convertStreamToString(urlConnet.inputStream)
publishProgress(inString)
}catch (ex:Exception){}
return " "
}
@SuppressLint("SetTextI18n")
override fun onProgressUpdate(vararg values: String?) {
try {
var json=JSONObject(values[0])
val rates= json.getJSONObject("rates")
var curRate = rates.getString(toRate.toString())
var xRate:Double = curRate.toDouble()
var xBase = inText.text.toString().toDouble()
var result = xBase*xRate
tvExchange.text = "1 "+fromRate+ " = " + "%.2f".format(xRate)+ " " +toRate+ "\n" +
"%.2f".format(xBase) +" "+fromRate+ " = " +"%.2f".format(result)+ " " +toRate
}catch (ex:Exception){}
}
override fun onPostExecute(result: String?) {
//after task done
}
}
fun convertStreamToString(inputStream: InputStream):String{
val bufferReader= BufferedReader(InputStreamReader(inputStream))
var line: String
var allString =""
try {
do {
line=bufferReader.readLine()
if (line!=null){
allString+=line
}
}while (line!=null)
inputStream.close()
}catch (ex:Exception){}
return allString
}
Google Play控制台-> Android Vitals->崩溃 报告;
java.lang.IllegalStateException:
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod (AppCompatViewInflater.java:424)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick (AppCompatViewInflater.java:381)
at android.view.View.performClick (View.java:6256)
at android.view.View$PerformClick.run (View.java:24708)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6565)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)
更新;我解决了这个问题!我不知道哪个更改有效,所以下面进行了更改:
buttConvert.setOnClickListener {
try {
fromFull = spinFrom.selectedItem.toString()
toFull = spinTo.selectedItem.toString()
fromRate = (fromFull as String).substring(0,3)
toRate = toFull.toString().substring(0,3)
val url = "https://api.exchangeratesapi.io/latest?base=$fromRate&symbols=$toRate"
MyAsyncTask().execute(url)
}catch (ex:Exception){}
}
XML:
<Button>
-----> <android.support.v7.widget.AppCompatButton>
android:autofillHints=""