Perform rest Request via URL - Android Network

Android examples for Network:URL

Description

Perform rest Request via URL

Demo Code

/*/*from ww  w  .j  a  v a 2 s  .  co  m*/
 * AbsurdEngine (https://bitbucket.org/smpsnr/absurdengine/) 
 * (c) by Sam Posner (http://www.arcadeoftheabsurd.com/)
 *
 * AbsurdEngine is licensed under a
 * Creative Commons Attribution 4.0 International License
 *
 * You should have received a copy of the license along with this
 * work. If not, see http://creativecommons.org/licenses/by/4.0/ 
 */
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;

public class Main{
    public static String restRequest(String request) throws IOException {
        URL url = new URL(request);
        URLConnection conn = url.openConnection();
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", DeviceUtility.getUserAgent());
        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "UTF-8"));

        StringBuilder result = new StringBuilder();
        String line = "";
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        in.close();
        return result.toString();
    }
}

Related Tutorials