Issue
I am trying to send longitude and latitude with autoincremented ID to my database running of Flask server via REST Api. I tested it with Postman and it worked fine. Here is example request body from postman.
{
"longitude":1.877771999,
"latitude":2.01999
}
The Flask code part is as follows:
class LocationValues(Resource):
parser = reqparse.RequestParser()
parser.add_argument(
'longitude',
type=float,
required=True,
help="You must enter longitude"
)
parser.add_argument(
'latitude',
type=float,
required=True,
help="You must enter latitude"
)
def post(self):
data = LocationValues.parser.parse_args()
item = {'longitude': data['longitude'], 'latitude': data['latitude']}
connection = sqlite3.connect('db.sqlite')
cursor = connection.cursor()
query = "INSERT INTO location VALUES(NULL ,?,?)"
result=cursor.execute(query, (item['longitude'], item['latitude']))
connection.commit()
connection.close()
if result:
return {"message":"Posted with success"},201
return {"message":"failure"},400
I think the mistake is in the way I send the data to the server using retrofit. Here in my activity class i created my own test data which I try to send, without success.
val repository = Repository()
val viewModelFactory = MainViewModelFactory(repository)
viewModel = ViewModelProvider(this,viewModelFactory).get(MainViewModel::class.java)
val buttonPost:Button = findViewById(R.id.button_post)
val myPostedData = Post2(Item2(5.2,9.99))
buttonPost.setOnClickListener {
viewModel.pushPost(myPostedJSON)
viewModel.myResponse.observe(this, Observer { response ->
if (response.isSuccessful) {
Toast.makeText(this,"OK",Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this,"Failed to send data",Toast.LENGTH_SHORT).show()
}
})
}
My Retrofit data class
data class Post2(
val item: Item2
)
data class Item2(
val longitude: Double,
val latitude: Double
)
Repository
class Repository {
suspend fun pushPost(post:Post2):Response<Post2>{
return RetrofitInstance.api.pushPost(post)
}
}
MainViewModel
class MainViewModel(private val repository: Repository):ViewModel() {
val myResponse: MutableLiveData<Response<Post>> = MutableLiveData()
val myResponse2: MutableLiveData<Response<Post2>> = MutableLiveData()
fun pushPost(post: Post2){
viewModelScope.launch {
val response : Response<Post2> = repository.pushPost(post)
myResponse2.value = response
}
}
interface SimpleApi {
@POST("adamapi")
suspend fun pushPost(
@Body post: Post2
): Response<Post2>
}
What am I doing wrong here? Screenshot from Postman:
From the Flask side in debug mode while trying to send it with my phone, we see that it fails.
Solution
You're passing wrong data in body of the API. You should pass the Item2
as a body in the API request.
interface SimpleApi {
@POST("adamapi")
suspend fun pushPost(
@Body body: Item2
): Response<Any>
}
To print header and body in logcat. Check this article. This can help you to debug your api request and response easily.
Answered By - Akshay Kalola
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.