我在这方面遇到了一些Stack问题,但找不到答案。
以下是我在PostMan中的请求和回复
以下是我的改造代码。我有一个LoginModel POJO类,其中包含用户名和令牌作为字段及其各自的getter和setter:
object ApiClient {
val BASE_URL = "http://MyIpaddress:3005/api/"
private var retrofit: Retrofit? = null
val client: Retrofit
get() {
if (retrofit == null) {
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit!!
}
}
public interface LoginApiInterface {
@Headers("Content-Type: application/json")
@POST("v1/account/login")
Call<LoginModel> saveLoginModel(@Body String body);
}
以下是用户单击LoginButton
时调用的代码fun attemptLogin(){
val email = et_email_activity_login.getText().toString()
val password = et_password_activity_login.getText().toString()
val apiInterface = ApiClient.client.create(LoginApiInterface::class.java)
val jsonBody = JSONObject()
jsonBody.put("email", email)
jsonBody.put("password", password)
val mRequestBody = jsonBody.toString()
apiInterface.saveLoginModel(mRequestBody).enqueue(object: Callback<LoginModel>{
override fun onFailure(call: Call<LoginModel>?, t: Throwable?) {
Log.i("TAG",t!!.localizedMessage)
}
override fun onResponse(call: Call<LoginModel>?, response: Response<LoginModel>?) {
Log.i("TAG","Post submitted"+response!!.body().toString())
}
} )
我在response!!.body().toString()
中变为空。当我做的时候
if (response!!.code() == 400) {
Log.v("Error code 400",response!!.errorBody()!!.string());
}
我收到了这个回复:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token # in JSON at position 0<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (/home/pritish/Music/foodtruck-api/foodtruck-api/node_modules/body-parser/lib/types/json.js:157:10)<br> at parse (/home/pritish/Music/foodtruck-api/foodtruck-api/node_modules/body-parser/lib/types/json.js:83:15)<br> at /home/pritish/Music/foodtruck-api/foodtruck-api/node_modules/body-parser/lib/read.js:121:18<br> at invokeCallback (/home/pritish/Music/foodtruck-api/foodtruck-api/node_modules/raw-body/index.js:224:16)<br> at done (/home/pritish/Music/foodtruck-api/foodtruck-api/node_modules/raw-body/index.js:213:7)<br> at IncomingMessage.onEnd (/home/pritish/Music/foodtruck-api/foodtruck-api/node_modules/raw-body/index.js:273:7)<br> at emitNone (events.js:115:13)<br> at IncomingMessage.emit (events.js:218:7)<br> at endReadableNT (_stream_readable.js:1054:12)<br> at _combinedTickCallback (internal/process/next_tick.js:138:11)<br> at process._tickDomainCallback (internal/process/next_tick.js:218:9)</pre>
</body>
</html>
答案 0 :(得分:0)
您确定请求是否成功?
添加http登录拦截器以查看在发出实际请求时您发送和接收的内容。 (https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor)
还要记住,对于4xx http响应代码调用Retrofits onResponse,然后你的body()为null。对于这些情况,您应该使用errorBody。 (onFailure需要500个错误代码和其他问题,如超时等......
在onRespose中也要进行以下检查。
if (response.isSuccessful()) { // checks if res code is in [200..300)
// Parse the response.body()
} else {
// Parse the response.errorBody()
}
答案 1 :(得分:0)
问题出在我的服务器端代码中。我应该在发布数据后传递JSON响应。
此链接也有很多帮助:
Is there really more messages than one page capacity (default is 20)?
Check that pagination is declared before postman in the INSTALLED_APPS setting.
See if it’s better by disabling postman/templatetags/pagination_tags.py and .pyc (rename or move the files).