首页 > 解决方案 > Android Studio:将值从editText传递到URL以调用服务器

问题描述

我需要传递从用于调用服务器的 URL 中的 editText 获得的值。如果我的本地主机是“xxxx:xxxx/”,我需要传递的值是“id”(来自editText),所以,将它们构建在一起,我将获得这个URL“xxxx:xxxx/id”。总之,我需要通过添加插入到 editText 中的 id 来修改我调用的链接,以便根据 id.("xxxx:xxxx/0876", "xxxx:xxxx/8907", " xxxx:xxxx/3456" 等...) 请我需要帮助。

我正在为 json 使用retrofit2,我将向您展示我已经设法用我的代码做什么

    //this is the class 
    private val retrofit = 
    Retrofit.Builder().baseUrl("http://x.x.x.x:xxxx/")
       .addConverterFactory(GsonConverterFactory.create())
       .build()
    private val postApi=retrofit.create(RequestApi::class.java)
    private val response = 
    postApi.getAllPosts(intent.getStringExtra("Username")) // this is the 
                                               //  value from the editText
                                               //(ex: 0907)


     //this is the interface
    interface RequestApi {


     @GET("statistiche/{id}")
      fun getAllPosts(@Query("id") id: String ): Call<ExchangeResponse>
       //this is where I say that the value from editText, passed by 
       //getAllposts, is build with the URL
       //(ex: "statistiche/0907")



     }


     //this is where I prepared the value from editText to be passed to 
     //other activities and interfaces
        val intent = Intent(this@MainActivity,Home::class.java)
            intent.putExtra("Username" , editText.text.toString())
            startActivity(intent)

当我运行此代码时,我收到此错误

 2019-07-17 00:39:36.763 21395-21395/com.example.adamo3 E/AndroidRuntime: 
 FATAL EXCEPTION: main
  Process: com.example.adamo3, PID: 21395
  java.lang.RuntimeException: Unable to instantiate activity 
 ComponentInfo{com.example.adamo3/com.example.adamo3.Risk_Stat}: 
 java.lang.NullPointerException: Attempt to invoke virtual method 
 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' 
  on a null object reference
    at 
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
    at 
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
    (ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
  'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' 
   on a null object reference
    at com.example.adamo3.Risk_Stat.<init>(Risk_Stat.kt:30)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
    at android.app.ActivityThread.performLaunchActivity
    (ActivityThread.java:2538)
    at 
   android.app.ActivityThread.handleLaunchActivity
   (ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
    (ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

有错误的行是这个 postApi.getAllPosts(intent.getStringExtra("Username"))

标签: androidkotlininterfaceparameter-passingretrofit

解决方案


推荐阅读