Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

android - i am using jsonrequest for that but its not working

i have to get json data from a website, by giving my device id . i am using jsonrequest for that but its not working,

class MainActivity : AppCompatActivity() {
    lateinit var  textView: TextView
    lateinit var button: Button
    var jsonObject = JSONObject()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView = findViewById(R.id.txtData)
        button=findViewById(R.id.btnparse)

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.INTERNET), 999)
        }
    }

    fun jsonParse(view: View) {
       val requestQueue = Volley.newRequestQueue(this)
        val url = "http://-----/OrderingSystem.svc/GetData?devid={device_id}&table={login or customer_info}"
        val requestJson = JsonObjectRequest(Request.Method.GET, url, null, { jsonResult ->
            try {
                val x = jsonResult.getJSONObject("devid")
                Toast.makeText(applicationContext, " success ", Toast.LENGTH_SHORT).show()
            } catch (e: Exception) {
                Toast.makeText(applicationContext, "catch error", Toast.LENGTH_SHORT).show()
            }
        },
                { jsonError ->
                    jsonError.printStackTrace()
                    Toast.makeText(applicationContext, jsonError.toString(), Toast.LENGTH_SHORT).show()
                })
        requestQueue.add(requestJson)
    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 999 && grantResults.isNotEmpty()) {
            Toast.makeText(applicationContext, "Permission Granted", Toast.LENGTH_SHORT).show()
        }
    }

}
question from:https://stackoverflow.com/questions/65934839/i-am-using-jsonrequest-for-that-but-its-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...