无法使用Kotlin使用翻新android发布我的数据 [英] Unable to POST my data using retrofit android using kotlin

查看:123
本文介绍了无法使用Kotlin使用翻新android发布我的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android studio和kotlin的新手.我试图将我的数据(对象)发布到服务器上,但得到了404响应代码.

I am new to android studio and kotlin. I have trying to post my data(object) onto the server but I'm getting a 404 response code.

我的改造:

object RetrofitClient {
private var OurInstance : Retrofit?=null
val client = OkHttpClient.Builder()
val instance:Retrofit
get() {
    if (OurInstance==null)
    {
        OurInstance =Retrofit.Builder()
                .baseUrl("http://coreapi.imagin8ors.org:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
    }
    return OurInstance!!
  }
}

我的界面:

interface IMyAPI {
@GET("v1/child/{dynamic}/learningpods_new")
fun getDynamic(@Path("dynamic")dynamic:String):Observable<ArrayList<Data>>

@POST("v1/authenticate/create")
fun createParent(@Body parentDetails: ParentDetails):Call<ParentDetails>
}

我的活动中的代码段:

fun sendNetworkRequest(parentDetails: ParentDetails){
    val retrofit=Retrofit.Builder().baseUrl("http://coreapi.imagin8ors.org:8080/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    val client=retrofit.create(IMyAPI::class.java)
    val call = client.createParent(parentDetails)
    call.enqueue(object :Callback<ParentDetails>{
        override fun onFailure(call: Call<ParentDetails>?, t: Throwable?) {
            Toast.makeText(this@ParentmainActivity,"something went wrong",Toast.LENGTH_LONG).show()
        }

        override fun onResponse(call: Call<ParentDetails>?, response: Response<ParentDetails>?) {
            Toast.makeText(this@ParentmainActivity,"successful :"+response?.code(),Toast.LENGTH_LONG).show()
        }

    })
}

函数调用:sendNetworkRequest(new_parent)

现在,返回的响应代码为404.此问题先前已经提出过,但是这些解决方案对我不起作用.这就是为什么我发布了另一个问题.

Now, the response code returned is 404. This issue has been raised previously but those solutions didn't work for me. That's why I have posted as another question.

我引用的链接: link2 link3 等等.

The links I have referred to: link1, link2, link3 and many more..

最初,我使用的是retrofitclient,但后来却无法正常工作,因此我尝试使用sendNetworkResquest()函数.但是,这也行不通.

Initially I was using the retrofitclient but then it didn't work so I tried with the sendNetworkResquest() function. However, this too doesn't work.

推荐答案

我认为您需要的是PUT请求. 当我尝试使用POST的API时,实际上得到了405 - Method Not Allowed.但是PUT可以工作..除了我没有发送数据,所以得到了400.但绝对不是404

I think what you need is a PUT request. When I try your API with POST I actually get a 405 - Method Not Allowed. But PUT works.. except that I am sending no data, so I get a 400. But definitely not a 404

curl -X POST http://.../create -> Method not allowed
curl -X PUT http://.../create -> Bad Request

还要仔细检查您的身体实际上是否包含后端端点指定的所需格式的有效数据,否则您将得到进一步的错误

Also double check that your body actually contains valid data in the required format, as specified by your backend endpoint, otherwise you'll get further errors

这篇关于无法使用Kotlin使用翻新android发布我的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆