我需要帮助才能发布到我的api。我该怎么办?

时间:2019-04-04 08:46:18

标签: android kotlin gson android-volley

我正在为我的应用程序创建登录名。 我目前无法将问题发布到我的API

这是我用来支持登录的API。

{
success: false,
message: "Please provide complete and accurate information.",
data: [ ]
}

fun loginUrlSuccess(urlApi : String) {
    Log.d("login", urlApi)
    authSignin_cgi = gson.fromJson(urlApi, DtoProfile::class.java)
    loginsSuccess = authSignin_cgi.success
    val queue    = Volley.newRequestQueue(context)
    val stringReq              = object : StringRequest(Request.Method.POST,urlApi,Response.Listener<String>{ response ->
        Log.w("response",response)
        Toast.makeText(context,"Loging success..",Toast.LENGTH_SHORT).show()
        if (loginsSuccess){
            Toast.makeText(context,authSignin_cgi.message,Toast.LENGTH_LONG).show()
        } else {
            Toast.makeText(context,authSignin_cgi.message,Toast.LENGTH_LONG).show()
        }
    },Response.ErrorListener { error ->
        Log.w("error", error.toString())
        Toast.makeText(context, "error..$error",Toast.LENGTH_SHORT).show()
    }){
        override fun getParams(): MutableMap<String, String> {
            val param = HashMap<String, String>()
            val userEmail = textEmail.text.toString().trim()
            val userPassword = textPassword.text.toString().trim()
            param["useremail"] = userEmail
            param["userpassword"] = userPassword

            return param
        }
    }
    queue.add(stringReq)
}

我从Logcat屏幕收到错误消息。

那我该怎么办?


04-04 15:31:43.614 8365-8699/com.example.atimeonlin5 E/Volley: [700] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL {"success":false,"message":"โปรดระบุข้อมูลให้ถูกต้องครบถ้วน","data":[]}
java.lang.RuntimeException: Bad URL {"success":false,"message":"โปรดระบุข้อมูลให้ถูกต้องครบถ้วน","data":[]}

2 个答案:

答案 0 :(得分:0)

您应该使用网址(例如"http://www.google.com"),而不是随机字符串。您的urlApi不是网址。

来自doc的示例:

val textView = findViewById<TextView>(R.id.text)
// ...

// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "http://www.google.com"

// Request a string response from the provided URL.
val stringRequest = StringRequest(Request.Method.GET, url,
        Response.Listener<String> { response ->
            // Display the first 500 characters of the response string.
            textView.text = "Response is: ${response.substring(0, 500)}"
        },
        Response.ErrorListener { textView.text = "That didn't work!" })

// Add the request to the RequestQueue.
queue.add(stringRequest)

答案 1 :(得分:0)

好的,您应该尝试构造一个Url对象而不是String类型!