network request using urlconnection

try {
    String response = null;
    URL url = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    String payload = "{\"pageSize\":30,\"pageNumber\":1,\"currentPriceRanges\":[],\"filters\":{\"newArrivals\":false,\"onSale\":false,\"articleType\":[],\"filterBy\":{\"Brand\":[]}},\"isFiltersRequired\":true,\"source\":\"listing\",\"collections\":[\"FL-Top Nav-ALL-Mens Shirts\"]}";

    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    writer.write(payload);
    writer.close();
    InputStreamReader isr = new InputStreamReader(
            connection.getInputStream());
    BufferedReader reader = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    response = sb.toString();
    isr.close();
    reader.close();
    return response;
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Comments

Popular posts from this blog

Working with Android Hierarchy Viewer

RecyclerView item animation

change svg android drawable color