Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun uploadImage(imagePath: String)
- {
- var textToPost = nameToPostET.text.toString()
- val URL = "http://192.168.100.10/"
- val gsonBuilder = GsonBuilder().setLenient()
- val gson = gsonBuilder.create()
- val retrofit = Retrofit.Builder()
- .baseUrl(URL)
- .addConverterFactory(GsonConverterFactory.create(gson))
- .build()
- val webApiInterface = retrofit.create(WEBApiInterface::class.java)
- val file = File(imagePath)
- val requestBody = RequestBody.create(MediaType.parse("*/*"), file)
- val fileToUpload = MultipartBody.Part.createFormData("upload_file", file.name, requestBody)
- val requestPOST = RequestBody.create(MediaType.parse("plain/text"), textToPost)
- var call = webApiInterface.uploadFile(fileToUpload, requestPOST)
- call.enqueue(object : Callback<FileResponse> {
- override fun onFailure(call: Call<FileResponse>?, t: Throwable?) {
- Log.e("MainActivity", t.toString())
- }
- override fun onResponse(call: Call<FileResponse>?, response: Response<FileResponse>?) {
- if(response!!.isSuccessful)
- {
- Log.v("MainActivity", "RESPONSE IS SUCCESSFUL")
- var message = response.body().message
- Toast.makeText(applicationContext, "MESSAGE --> "+message, Toast.LENGTH_LONG).show()
- var postTestMessage = response.body().postTestMessage
- Toast.makeText(applicationContext, "POST TEST MESSAGE --> "+postTestMessage, Toast.LENGTH_LONG).show()
- }
- else
- {
- Log.e("MainActivity", "RESPONSE ISNT SUCCESSFUL")
- }
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement