Tuesday, November 27, 2012

download data from url using http post

HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(mcontext.getString(R.string.find_link));
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("name", value);
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        HttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        InputStream is = null;
        try {
            is = response.getEntity().getContent();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        StringBuilder sb = new StringBuilder();
        byte[] buffer = new byte[512];
        int bytes_read = 0;

        try {
            while ((bytes_read = is.read(buffer, 0, buffer.length)) > 0) {
                sb.append(new String(buffer, 0, bytes_read));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mresult=sb.toString();

Add focus to a view

mhandler.post(new Runnable() {
            public void run() {
                    (EditText ref).requestFocus();                           
               
            }
        });