개발하슈

[android] retrofit2 file upload 이미지 업로드 본문

android

[android] retrofit2 file upload 이미지 업로드

하슈타인 2021. 6. 1. 11:54

[참고자료]

https://velog.io/@dev_thk28/Android-Retrofit2-Multipart%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0-Java

 

[Android] Retrofit2 Multipart사용하기 (Java)

Multipart 사용하기

velog.io

https://machine-woong.tistory.com/171

 

코틀린 안드로이드 retrofit2 이미지 전송

그레이들 추가 //Retrofit ( http 통신 관련 ) implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:retrofit-converters:2.4.0' // Gson 레트로핏 컨버터 레트로 핏..

machine-woong.tistory.com

https://philip1994.tistory.com/15

 

안드로이드 이미지 업로드 (retrofit)

PHP 소스

philip1994.tistory.com

https://blog.naver.com/power95031/222139388370

 

안드로이드 Retrofit2 이미지 업로드 예제

Java(자바) 코드 예제입니다. 1. build.gradle (Module: app)에 추가 2. Retrofit2 Service Interface...

blog.naver.com

https://jaejong.tistory.com/38

 

[안드로이드] Retrofit2 기본 사용법2 -'GET/POST/PUT/DELETE'

HTTP 요청메서드 (Request Method) HTTP 요청 메서드(Request Method) 기본 사용방법 - GET / POST / PUT / DELETE 목적 4가지 HTTP 요청방식(GET / POST / PUT / DELETE)을 각각 통신방법으로 사용 3가지 유형 -..

jaejong.tistory.com

https://link2me.tistory.com/1806

 

Retrofit 2 라이브러리 사용 예제

그동안은 Retrofit을 사용할 일이 전혀 없었는데, 사용할 일이 있어서 관련 자료를 찾아서 테스트하고 적어둔다. 예제를 이해하는데 참 어려웠다. 그래서 유투브 동영상도 보고 이해하고 따라해보

link2me.tistory.com

 

php코드

header("Content-Type:text/html;charset=utf-8");
$data=$_POST["uploaded_file"];

$uploads_dir="./newImage";
$file_path = "./";

$basename = basename( $_FILES['uploaded_file']['name']);

$file_path = $file_path . $basename;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'],"../$uploads_dir/$file_path")) {
    $result =array("result" => "success");
    echo "success";
} else{
    $result = array("result" => "error");
    echo "error";

 

android 코드

안드로이드 코드는 retrofit부분만 업로드함

public class ApiClient {
    private static final String BASE_URL = "서버주소";
    private static Retrofit retrofit;

    public static Retrofit getApiClient()
    {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        if (retrofit == null)
        {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }

        return retrofit;
    }

}
public interface RetrofitInterface {
    //api를 관리해주는 인터페이스
    @Multipart
    @POST("upload_chat.php")
    Call<String> request(@Part MultipartBody.Part file);

}
  private void uploadChat(){
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
        MultipartBody.Part body = MultipartBody.Part.createFormData("uploaded_file", imageFileName, requestFile);
        RetrofitInterface retrofitInterface=ApiClient.getApiClient().create(RetrofitInterface.class);
        Call<String> call=retrofitInterface.request(body);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.e("uploadChat()", "성공 : ");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.e("uploadChat()", "에러 : " + t.getMessage());
            }
        });
    }

 

'android' 카테고리의 다른 글

[android] fragment 생명주기  (0) 2021.12.10
[android] Retrofit2 post 예제  (0) 2021.05.28