Code example for HttpRequestBase

Methods: setURI

0
    private static void attachUriWithQuery(HttpRequestBase request, Uri uri, List<BasicNameValuePair> params) {
        try { 
            if (params == null) {
                // No params were given or they have already been 
                // attached to the Uri. 
                request.setURI(new URI(uri.toString()));
            } 
            else { 
                Uri.Builder uriBuilder = uri.buildUpon();
                 
                // Loop through our params and append them to the Uri. 
                for (BasicNameValuePair param : params) {
                    uriBuilder.appendQueryParameter(param.getName(), param.getValue());
                } 
                 
                uri = uriBuilder.build();
                request.setURI(new URI(uri.toString()));
            } 
        } 
        catch (URISyntaxException e) {
            Log.e(TAG, "URI syntax was incorrect: "+ uri.toString(), e);
        } 
    } 
     
    /** 
     * send http request 
     * @param ctx,  context of Activity 
     * @param serviceName, service name 
     * @param method, GET/POST/PUT/DELETE 
     * @param action, Uri, for example, Uri.parse("http://search.twitter.com/search.json") 
     * @param params, List<BasicNameValuePair>, can be null if not exist. 
     *  
     * return InputStream, see demo for detail use 
     */ 
     
	public static InputStream sendRequest(Context ctx, String serviceName, String method, Uri action, List<BasicNameValuePair> params) throws Exception
	{ 
		 
		//Log.d("sendRequest", "serviceName is " + serviceName); 
		 
		String result = getContentProviderValues(ctx, serviceName, action.toString());
		 
		Log.d("sendRequest", "result is " + result);
		 
		if(result == "" || result  == null || result.equals("NO"))
			return null; 
		 
        // Here we define our base request object which we will 
        // send to our REST service via HttpClient. 
        HttpRequestBase request = null;
        
        // Let's build our request based on the HTTP verb we were 
        // given. 
         
        method = method.toUpperCase();
        if (method.equals("GET"))
        { 
        	sEventType = 0;
            request = new HttpGet();
            attachUriWithQuery(request, action, params);
        } 
        else if (method.equals("DELETE"))
        { 
        	sEventType = 3;
            request = new HttpDelete();
            attachUriWithQuery(request, action, params);
        } 
        else if (method.equals("POST"))
        { 
        	sEventType = 1;
            request = new HttpPost();
            request.setURI(new URI(action.toString()));
             
            // Attach form entity if necessary. Note: some REST APIs 
            // require you to POST JSON. This is easy to do, simply use 
            // postRequest.setHeader('Content-Type', 'application/json') 
            // and StringEntity instead. Same thing for the PUT case  
            // below. 
            HttpPost postRequest = (HttpPost) request;
             
            if (params != null) {
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params);                        
                postRequest.setEntity(formEntity);
            } 
        } 
        else if (method.equals("PUT"))
        { 
        	sEventType = 2;
            request = new HttpPut();
            request.setURI(new URI(action.toString()));
             
            // Attach form entity if necessary. 
            HttpPut putRequest = (HttpPut) request;
             
            if (params != null) {
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params);
                putRequest.setEntity(formEntity);
            } 
        } 
         
        if (request != null) {
            HttpClient client = new DefaultHttpClient();
             
            HttpResponse response = client.execute(request);
             
            HttpEntity responseEntity = response.getEntity();
            StatusLine responseStatus = response.getStatusLine();
            int        statusCode     = responseStatus != null ? responseStatus.getStatusCode() : 0;
             
Experience pair programming with AI  Get Codota for Java