+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get data from server using DefaultHttpclient?

  1. #1

    How to get data from server using DefaultHttpclient?

    I want to know how to request data from server in android using DefaultHttpClient whenever i request data it is returning null.It is urgent please reply me as soon as possible.

  2. #2
    Junior Member
    Join Date
    Aug 2012
    Posts
    15

    DefaultHttpClient

    Hi code-extractor,

    check this example and modify as per your requirements.

    Code:
    public String sendRequest(String url) {
    			InputStream is = null;
    	    	String response = "";
    	    	
    	       
    	        try {
    	            DefaultHttpClient httpClient = new DefaultHttpClient();
                        //for post use HttpPost httpPost=new HttpPost(url);
    	            HttpGet httpGet = new HttpGet(url);
    	            HttpResponse httpResponse = httpClient.execute(httpGet);
    	            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);
    	            StringBuilder sb = new StringBuilder();
    	            String line = null;
    	            while ((line = reader.readLine()) != null) {
    	                sb.append(line);
    	            }
    	            is.close();
    	            response = sb.toString();
    	            //System.out.println(response);
    	        } catch (Exception e) {
    	            Log.e("Buffer Error", "Error converting result " + e.toString());
    	        }
    	        return response;
    		}

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts