2010-11-22 4 views
3

웹 서비스를 기반으로 안드로이드 응용 프로그램을 개발 중입니다. 클릭 한 항목을 기반으로 쿼리 (PHP 및 MySQL을 사용하는 서버)를 실행하고 웹에서 결과를 얻으려면 어떻게해야합니까? 또한 일부 값을 목록 (목록 레이아웃)으로 표시하려고합니다. 이 작업을 수행 할 여지가 있습니까?안드로이드 응용 프로그램에 대한 값을 얻는 방법

답변

2

당신이하고 싶은 일은 안드로이드 애플 리케이션을위한 데이터를 제공하는 나머지 API를 개발하는 것입니다. 예 : 당신은 귀하의 응용 프로그램에서 사용하고자하는 몇 가지 콘텐츠가 웹 사이트, 그리고 당신은 단지 특정 형식으로 그 데이터를 반환 PHP 스크립트를 작성할 수 있습니다.

예. mysite.net/rest/fetchAllLocations.php?maybe_some_parameters

예를 들어,

[{ "ID": JSON 형식은, 여기에이처럼 보이는 방법을 예를 들어 1, "shop_lng": 8.5317153930664, "shop_lat"52.024803161621, "shop_zipcode"33602 "shop_city": "빌레펠트" "shop_street": "Arndtstra \의 u00dfe", "shop_snumber"3 "shop_name": "M의 \의 u00fcller", "shop_desc": "Kaufhaus"} 여기서

휴식 API 요청에 대한 예이다 :

http://shoqproject.supervisionbielefeld.de/public/gateway/gateway/get-shops-by-city/city/Bielefeld

그래서 당신은 당신의 나머지 API는 당신이 당신의 안드로이드 전화와 데이터를 수신 처리 할 수 ​​설정할 때.

JSONArray 시험 = (JSONArray) JsonGrabber.receiveData을 (:

public class JsonGrabber{ 

public static JSONArray receiveData(){ 
    String url = "your url"; 
    String result = ""; 

    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpGet method = new HttpGet(url); 
    HttpResponse res = null; 

    try { 
     res = client.execute(method); 
    } catch (ClientProtocolException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 

    try{ 
     InputStream is = res.getEntity().getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1")); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    JSONArray jArray = null; 

    try{ 
     jArray = new JSONArray(result); 
    }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    return jArray; 
} 
} 

그럼 당신이 JSON 형식의 데이터를 일단 당신은 단지 그것을 구문 분석해야 할 모든 이잖아 :이 데이터를 얻을 수있는 정적 인 방법을 사용)

try { 
    for(int i=0;i<test.length();i++){ 
    JSONObject json_data = test.getJSONObject(i); 
    int id = json_data.getInt("id"); 
    } 
} 

웹 요청은 시간이 많이 걸리는 프로세스가 될 수 있으므로 다른 스레드에서 실행해야합니다. 따라서 AsyncTask를 처리해야합니다. 여기에 일부 리소스는 다음과 같습니다

Painless Threading Multithreading for performance Hello Android Tutorial

+0

감사 ArtWorkAD. 내가 이것을 시도하자. 안드로이드에서 SAX를 사용하여 XML을 구문 분석하는 방법을 알고 계십니까? 몇 가지 링크를 시도했지만 더 나은 해결책을 찾을 수 없습니다. XML 파일에서 일부 카테고리 이름을 가져 와서 Android 휴대 전화에 나열하고 싶습니다. –

+1

그럼 다른 질문에서 물어볼 수 있습니다. –

관련 문제