0

Possible Duplicate:
how do perform Http GET in android?

I want to use the Google Products/Shopping API in my Android app but I don't know anything about HTTP GET. I'm reading this and it's giving me all these different web adresses to use. So how do I use the Google Products/Shopping API in Android with HTTP GET?

Community
  • 1
  • 1
Cole
  • 2,805
  • 9
  • 44
  • 61

2 Answers2

1

It is useful to get familiar with HTTP first, then with URLConnection and Apache HttpClient.

ahanin
  • 892
  • 4
  • 8
1

Here is some sample code where I get JSON from a server. It includes the basic code lines for connecting to something via 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;
}   
Davek804
  • 2,804
  • 4
  • 26
  • 55