M64/react-native

REST API 연결

ZEROWIN.TECH 2021. 8. 13. 02:08
728x90
  • 버튼 구현
    <TouchableHighlight onPress={fetchApiCall}>
      <View style={styles.button}>
        <Text style={styles.buttonText}>
          USE Fetch API
        </Text>
      </View>
    </TouchableHighlight>
  • REST API 요청 함수 
const fetchApiCall = () => {
    fetch("http://192.168.121.254:8080/greeting", {
      "method": "GET",
      // "headers": {
      //   "x-rapidapi-host": "quotes15.p.rapidapi.com",
      //   "x-rapidapi-key": "yourapikey"
      // }
    })
      .then(response => response.json())
      .then(response => {
        console.log(response);
        // setQuote(response.content);
      })
      .catch(err => {
        console.log("error="+err);
      });
  }
  • SPRING REST API
@RestController
public class GreetingController {
    private  static  final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name ) {
        System.out.println("greeting");
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}
  • 결과물

'M64 > react-native' 카테고리의 다른 글

구글핏 인증 요청 & 스텝 개수 구하기  (0) 2021.08.13