μλλ‘μ΄λ μ€νλμ€ AndroidManifest.xml
μ€μ
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
...
android:usesCleartextTraffic="true">
build.gradle
μ Retrofit , okhttp3 μΆκ°
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation com.squareup.retrofit2:converter-scalars:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
public interface OcrFactory {
@GET("ocr/connect") // <=μκΈ° μμ λ€μ΄κ°λκ² μλ² path
Call<String> connect(@Query("userEmail") String email);
@GET("ocr/connecting") // <=μκΈ° μμ λ€μ΄κ°λκ² μλ² path
Call<Semester> connecting(@QueryMap Map<String, String> success);
@POST("ocr/semester") // <=μκΈ° μμ λ€μ΄κ°λκ² μλ² path
Call<Res_semester> semester(@Body Req_semester user);
}
μΈν°νμ΄μ€μμ ν΅μ λΆλΆμμ λλΆλΆμ λ©μλλ€μ λ€ λ£μμλ‘ κΉλ. λλ κΈ°λ₯λ³λ‘ λλμ΄ μΈν°νμ΄μ€ λΆλ₯ νκΈ°.
νλμ μλ²μ ν΅μ μ λ΄λΉνλ νλμ Singletone ν΄λμ€ λ§λ€κΈ°
```java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class Net {
private static Net ourinstance = new Net();
public static Net getInstance(){
return ourinstance;
}
Gson gson = new GsonBuilder().setLenient().create();
private Net(){
}
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor()).build();
private Retrofit retrofit = new Retrofit.Builder()
.baseUrl("") //<= μλ² μ£Όμκ° μ
λ ₯
.client(client) //<= OkHttpClient μ°λ
.addConverterFactory(ScalarsConverterFactory.create()) //<= κ³ μ§μ μΈ μλ¬ :JSON document was not fully consumed ν΄κ²°
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
OcrFactory ocrFactory;
public OcrFactory getOcrFactory(){
if(ocrFactory == null){
ocrFactory = retrofit.create(OcrFactory.class);
}
return ocrFactory;
}
private HttpLoggingInterceptor httpLoggingInterceptor(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
android.util.Log.e("MyLogis :", message + "");
}
});
return interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
}
}
```
νκΈ° μκ°ν λ°μ΄ν°λ₯Ό λ΄λ Semester ν΄λμ€ μμ±
public class Semester {
private String major1,major2;
private String Class1,Class2,Class3,Class4,Class5;
public Semester(){
}
@Override
public String toString(){
return "semester{" +
"major1='" + major1 + '\'' +
", major2=" + major2 +
", Class1='" + Class1 + '\'' +
", Class2='" + Class2 + '\'' +
", Class3='" + Class3 + '\'' +
", Class4='" + Class4 + '\'' +
", Class5='" + Class5 + '\'' +
'}';
}
public Semester(String major1, String major2, String Class1, String Class2, String Class3, String Class4, String Class5){
this.major1= major1;
this.major2 = major2;
this.Class1 = Class1;
this.Class2 = Class2;
this.Class3 = Class3;
this.Class4 = Class4;
this.Class5 = Class5;
}
public String getClass1() {
return Class1;
}
public void setClass1(String class1) {
Class1 = class1;
}
public String getClass2() {
return Class2;
}
public void setClass2(String class2) {
Class2 = class2;
}
public String getClass3() {
return Class3;
}
public void setClass3(String class3) {
Class3 = class3;
}
public String getClass4() {
return Class4;
}
public void setClass4(String class4) {
Class4 = class4;
}
public String getClass5() {
return Class5;
}
public void setClass5(String class5) {
Class5 = class5;
}
public String getMajor1() {
return major1;
}
public void setMajor1(String major1) {
this.major1 = major1;
}
public String getMajor2() {
return major2;
}
public void setMajor2(String major2) {
this.major2 = major2;
}
}
pojo μ¬μ΄νΈλ₯Ό ν΅ν΄ λΉ λ₯΄κ² λ§λ€μ μλ€
connect ν¨μ(GET, Res = String, Req = String )
Call<String> res = Net.getInstance().getOcrFactory().connect("bangjinhyuk");
res.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()){
if(response.body() != null){
String success = response.body().toString();
Log.d("Main ν΅μ ", response.body().toString());
}else{
Log.e("Main ν΅μ ", "μ€ν¨ 1 response λ΄μ©μ΄ μμ");
}
}else{
Log.e("Main ν΅μ ", "μ€ν¨ 2 μλ² μλ¬");
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("Main ν΅μ ", "μ€ν¨ 3 ν΅μ μλ¬ "+t.getLocalizedMessage());
}
});
connecting ν¨μ (GET, Res = String, Req = Map )
Map<String, String> map = new HashMap<String, String>();
map.put("name","bbangi");
map.put("birth", "0103");
Call<Semester> res1 = Net.getInstance().getOcrFactory().connecting(map);
res1.enqueue(new Callback<Semester>() {
@Override
public void onResponse(Call<Semester> call, Response<Semester> response) {
if(response.isSuccessful()){
if(response.body() != null){
Semester semesters = response.body();
Log.d("Map ν΅μ ", semesters.toString());
}else{
Log.e("Map ν΅μ ", "μ€ν¨ 1 response λ΄μ©μ΄ μμ");
}
}else{
Log.e("Map ν΅μ ", "μ€ν¨ 2 μλ² μλ¬");
}
}
@Override
public void onFailure(Call<Semester> call, Throwable t) {
Log.e("Map ν΅μ ", "μ€ν¨ 3 ν΅μ μλ¬ "+t.getLocalizedMessage());
}
});
semester ν¨μ (POST, Res = Res_semester, Req = Req_semester)
Req_semester req_semester = new Req_semester();
req_semester.setSemester(new Semester(
"μννΈμ¨μ΄νκ³Ό",
"λ―Έλμ΄νκ³Ό",
"컴ꡬ",
"μ°½μμ
",
"κΈ°μ°½κ²½",
"",
""
));
Call<Res_semester> res2 = Net.getInstance().getOcrFactory().semester(req_semester);
res2.enqueue(new Callback<Res_semester>() {
@Override
public void onResponse(Call<Res_semester> call, Response<Res_semester> response) {
if(response.isSuccessful()) {
if (response.body() != null) {
Res_semester res_semester = response.body();
Log.d("semester ν΅μ ",res_semester.code+"");
} else {
Log.e("semester ν΅μ ", "μ€ν¨ 1 response λ΄μ©μ΄ μμ");
}
}else{
Log.e("semester ν΅μ ", "μ€ν¨ 2 μλ² μλ¬");
}
}
@Override
public void onFailure(Call<Res_semester> call, Throwable t) {
Log.e("semester ν΅μ ", "μ€ν¨ 3 ν΅μ μλ¬ "+t.getLocalizedMessage());
}
});
Req,Res ν΄λμ€
class Res_semester {
int code; //μλ²λ‘λΆν°μ μλ΅ μ½λ. 404, 500, 200 λ±.
String msg; //μλ²λ‘λΆν°μ μλ΅ λ©μΈμ§.
Semester semester; //곡λΆνμ.
}
class Req_semester {
Semester semester;
public Semester getSemester() {
return semester;
}
public void setSemester(Semester semester) {
this.semester = semester;
}
}
ocr.js
νμΌ μμ±
var express = require('express');
var router = express.Router();
/* GET ocr listing. */
router.get('/connect', function(req, res, next) {
if(req.query.userEmail == 'bangjinhyuk'){
res.send('connect all sucess');
}else{
res.send('connect little sucess');
}
});
/* GET ocr listing. */
router.get('/connecting', function(req, res, next) {
if(req.query.name == 'bbangi' && req.query.birth == '0103'){
res.send(JSON.stringify(
{"major1":"μμ¨",
"major2": "λ―Έλμ΄",
"Class1":"1",
"Class2":"2",
"Class3":"3",
"Class4":"4",
"Class5":"5"}));
}else{
res.send('connecting little sucess');
}
});
/* GET ocr listing. */
router.post('/semester', function(req, res, next) {
res.send('{"code":-1, "msg": "failed"}');
// if(req.body.semester == 'bbangi' && req.query.birth == '0103' ){
// res.send('connecting all sucess');
// }else{
// res.send('connecting little sucess');
// }
});
/* GET ocr listing. */
router.get('/', function(req, res, next) {
// res.send(req.body.userEmail);
res.send('connect');
});
module.exports = router;