카테고리 없음

[자바] Android에서 작동하는 REST 서비스의 종류

행복을전해요 2021. 2. 19. 14:27

JSON은 Jackson 라이브러리를 사용하여 잘 작동합니다. GSON은 옵션 일 수 있지만 직접 시도하지는 않았습니다.

SOAP는 당신이 언급했듯이 거의 의문의 여지가 없지만 REST를 통한 XML 바인딩은 JAXB 대신 SimpleXML 프레임 워크를 사용하여 합리적으로 잘 수행 할 수 있습니다. 그러나 복잡한 객체 아키텍처의 경우 이것은 매우 느릴 수 있습니다. 선택권이 있다면 JSON을 선택하십시오.

추가 옵션 http://en.wikipedia.org/wiki/Comparison_of_data_serialization_formats 를 참조하십시오.

-------------------

내가 이해한다면 앱과 통신하려면 웹 서비스가 필요하며 다음과 같을 것입니다.

Your app calls your WS url (maybe with some POST datas) and your WS responds to your app.

그런 다음 JSON으로 통신하는 REST 웹 서비스를 만들 수 있습니까? Android는 예를 들어 Google GSON 과 같은 매우 좋은 라이브러리로 JSON을 구문 분석 할 수 있습니다 .

WS 기술의 경우 원하는 것을 사용할 수 있습니다.

물건을 구하면 나이와 이름이있는 사람들의 목록처럼 작동합니다. API는에 http://www.yourapi.com/있으므로 사람들을 요청하는 것처럼http://www.yourapi.com/people

API를 호출 할 때 응답은 다음과 같습니다.

[{
    "age": 18,
        "name": "toto"
        },
        {
            "age": 21,
                "name": "foo"
                }]
                

따라서 클라이언트 측 (Android 프로젝트)에서 Person클래스가 있습니다.Person.java

public class Person {
    private int age;
        private String name;
        
            public Person(){
                    this.age = 0;
                            this.name = "";
                                }
                                
                                    public int getAge(){
                                             return age;
                                                 }
                                                 
                                                     public void setAge(int age){
                                                              this.age = age;
                                                                  }
                                                                  
                                                                      public String getName({
                                                                               return name;
                                                                                   }
                                                                                   
                                                                                       public void setName(String name){
                                                                                                this.name = name;
                                                                                                    }
                                                                                                    }
                                                                                                    

귀하의 모델은 무엇이며 AsyncTask다운로드하려면 :

public abstract class PeopleDownloader extends AsyncTask<Integer, Integer, ArrayList<Person>> {

    private static final String URL = "http://www.yourapi.com/people";
    
        @Override
            protected ArrayList<Person> doInBackground(Void... voids) {
                    HttpClient httpclient = new DefaultHttpClient();
                            // Prepare a request object
                                    HttpGet httpget = new HttpGet(URL);
                                            // Execute the request
                                                    HttpResponse response;
                                                            try {
                                                                        Log.d(TAG, "Trying to reach " + httpget.getURI());
                                                                                    response = httpclient.execute(httpget);
                                                                                                // Get the response
                                                                                                            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                                                                                                                        String finalResposne = "";
                                                                                                                                    String line = "";
                                                                                                                                                while ((line = rd.readLine()) != null) {
                                                                                                                                                                finalResposne += line;
                                                                                                                                                                            }
                                                                                                                                                                                        Log.d(TAG, finalResposne);
                                                                                                                                                                                                    return new Gson().fromJson(finalResposne, new TypeToken<List<Person>>() {
                                                                                                                                                                                                                }.getType());
                                                                                                                                                                                                                        } catch (Exception e) {
                                                                                                                                                                                                                                    Log.e(TAG, "Error in doInBackground " + e.getMessage());
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                    return null;
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                        

그리고 끝났습니다.



출처
https://stackoverflow.com/questions/22079897