简体   繁体   English

为什么此 Web 服务代码不输出?

[英]Why does this web service code not output?

I am a beginner Android developer, I wrote this code related to web service, but this program has no output (I am not getting any error).我是一名初学者 Android 开发人员,我编写了与 Web 服务相关的代码,但该程序没有输出(我没有收到任何错误)。 And I'm sure that the API is correct, but it doesn't show anything in the text view.And when the app is running on the device, the internet is on.而且我确定 API 是正确的,但它在文本视图中没有显示任何内容。并且当应用程序在设备上运行时,互联网已打开。

mainActiviy:主要活动:

class MainActivity : AppCompatActivity() {
  lateinit var binding : ActivityMainBinding
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
    binding.button.setOnClickListener(){
        get()

    }


}
fun get() {
    var cityname = binding.editTextTextPersonName.text.toString()
    val endpoint = "http://api.weatherapi.com/v1/current.json? ****** &q=ya&aqi=no"
    myAsyncTask().execute(endpoint)

}
    inner class myAsyncTask : AsyncTask<String, String, String>() {

        override fun doInBackground(vararg params: String?): String {
            try {

                val url = URL(params[0])
                val urlconnect=url.openConnection() as HttpURLConnection
                urlconnect.connectTimeout=100000
                val instring=convertsteamtoString(urlconnect.inputStream)
                publishProgress(instring)

            }catch (ex:Exception){}
            return ""
        }

        override fun onProgressUpdate(vararg values: String) {
            try {
               
                val json =JSONObject(values[0])
                var current=json.getJSONObject("current")
                var temp_c =current.getString("temp_c")
                 binding.textviewtemperature.text=temp_c

            }catch (ex:Exception){}

        }
        fun convertsteamtoString(inputStream :InputStream):String{


            val bufferReader =BufferedReader(InputStreamReader(inputStream))
            var line:String
            var allString :String =""
            try {
                do{
                    line = bufferReader.readLine()
                    allString += line

                }while (line != null)
            }catch (ex:Exception){}
            return allString

        }
    }

} }

androidmanifest:机器人清单:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     package="com.example.webservice">
     <uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.WebService"
    tools:targetApi="31">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

When you write a catch block instead of ignoring the exception you get, you should log the exception so you dont miss events occuring in your code.当你编写一个 catch 块而不是忽略你得到的异常时,你应该记录异常,这样你就不会错过代码中发生的事件。 Try something like this in your catch blocks:在你的 catch 块中尝试这样的事情:

Log.e("tag", exception)

And then checkout your Logcat for more info of whats happening to your app.然后检查您的 Logcat 以了解有关您的应用程序发生了什么的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM