How should I implement retrofit into this code to send an image to the server instead of using Volley? I'm a little confused as I'm a bit new to Java/Android. I would like some guidance on how to achieve this using Retrofit as Volley doesnt seem to be working.
I'm basically sending a base64 string of an image to my server along with the image name and a few other things later on. I also need to retrieve the response from my server and display it on my app. Volley seemed to do that easily, not sure about Retrofit.
Thanks in advance
public class MainActivity extends AppCompatActivity {
private ImageButton ImageButton;
private String encoded_string, image_name;
private Bitmap bitmap;
private File file;
private Uri file_uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton = (ImageButton) findViewById(R.id.camera);
ImageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getFileUri();
i.putExtra(MediaStore.EXTRA_OUTPUT,file_uri);
startActivityForResult(i,10);
}
});
}
private void getFileUri() {
image_name = "testing123.jpeg";
file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), image_name);
file_uri = Uri.fromFile(file);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 10 && resultCode == RESULT_OK){
new Encode_image().execute();
}
}
private class Encode_image extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids){
bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array,0);
return null;
}
@Override
protected void onPostExecute(Void aVoid){
makeRequest();
}
}
private void makeRequest() {
final TextView mTextView = (TextView) findViewById(R.id.text);
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://128.199.77.211/server/connection.php";
StringRequest request = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mTextView.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put("encoded_string", encoded_string);
map.put("image_name", image_name);
return map;
}
};
requestQueue.add(request);
}
}