음식 분류 프로젝트의 연장으로
안드로이드 애플리케이션에서 이미지를 선택해 서버로 보내면
서버에서 머신러닝 결과를 반환해 안드로이드 애플리케이션에서 출력하는 기능을 구현하고 있다.
이 부분은 안드로이드 OkHttp3 라이브러리를 사용해
서버로 파일을 업로드하는 부분이다.
소스코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class HttpMultiPart {
Context mContext;
String result;
public HttpMultiPart(final Context mContext) {
this.mContext = mContext;
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image", "보낼파일명", RequestBody.create(MultipartBody.FORM, new File(path)))
.build();
Request request = new Request.Builder()
.url("받을 URL")
.post(requestBody)
.build();
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
JSONObject json = null;
try {
json = new JSONObject(response.body().string());
result = json.getString("result");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
|
cs |
'개발 > 안드로이드' 카테고리의 다른 글
[Kotlin] 안드로이드 RecyclerView 누른 item만 색깔 바뀌게 하기 (0) | 2020.10.22 |
---|---|
[Kotlin] 안드로이드 다른 class에서 UI 그리기 (0) | 2020.10.16 |
[Kotlin] 안드로이드 bitmap으로부터 uri가져온 뒤, filepath 찾기 (3) | 2020.10.14 |
[Kotlin] 안드로이드 스튜디오 카메라 회전 및 화질 향상 (0) | 2020.10.14 |
[Kotlin] 안드로이드 카메라 구현 예제 (0) | 2020.10.14 |