2012-04-06 1 views
0

가능한 중복 :
how do perform Http GET in android?Android에서 HTTP GET을 사용하는 방법?

내가 내 안드로이드 응용 프로그램의 Google 제품/쇼핑 API를 사용하고 싶지만 내가 HTTP GET에 대해 아무것도 몰라. 나는 this을 읽고 있는데,이 웹 사이트는이 모든 다른 웹 주소를 사용하고 있습니다. 그렇다면 Android에서 Google 제품/쇼핑 API를 HTTP GET과 함께 사용하려면 어떻게해야하나요?

답변

1

다음은 서버에서 JSON을 가져 오는 샘플 코드입니다. 여기에는 HTTP를 통해 무언가에 연결하기위한 기본 코드 라인이 포함됩니다.

public JSONArray getQuestionsJSONFromUrl(String url, List<NameValuePair> params) { 
    // Making HTTP request 
try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     String jsonData = reader.readLine(); 
     JSONArray jarr = new JSONArray(jsonData); 
     is.close(); 
     return jarr; 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 
    return null; 
}