2

I am looking to insert userdata to database by calling URL link to webservice on server: ex: this is the link:

http://mydomain.com/AndroidWebService.asmx/nInsertInfo?id=12&lat=23.2222&log=12322

So I want to call this url in hidden mode.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
AbuQauod
  • 919
  • 14
  • 30

6 Answers6

5

Use android android AsyncTask to post data. It work in android background when application is open. AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Thanks.

Community
  • 1
  • 1
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
4

Thanks all for your valuable solutions.

i have did it by creating WebView

WebView myWebView = (WebView) findViewById(R.id.view1);
myWebView.loadUrl(urll);

then i can manage my WebView as i want: hide it, alignment as error or result returns.

AbuQauod
  • 919
  • 14
  • 30
2

Maybe this example will help you :

public class URLConnectionTask <Result> extends AsyncTask<URL, Void, InputStream> {


        @Override
        protected InputStream doInBackground(URL... params) {
            InputStream is;
            HttpUriRequest request;
            URI uri;
            HttpResponse response;

            if (params.length == 0)
                return null;

            is = null;CredentialsProvider credProvider = new BasicCredentialsProvider();

        if (userName != null && userName.length() > 0 && password != null && password.length() > 0)
            credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(userName, password));

        //
        DefaultHttpClient http = new DefaultHttpClient();
        http.setCredentialsProvider(credProvider);
        //

        uri = URI.create(params[0].toString());

        if (isPost)
            request = new HttpPost(uri);
        else
            request = new HttpGet(uri);

        try {           
            response = http.execute(request);
            is = response.getEntity().getContent();

            //Log.d(TAG, "doInBackground() response: "+EntityUtils.toString(response.getEntity()));
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


       if (processingHandler != null && is != null)
           processingResult = processingHandler.processResponse(is);// processingHandler is an instance which implements ProcessingHandler interface (ex. VizMarket). processResponse() is implemented on this class.

       return is;
    }

    @Override
        protected void onPostExecute (InputStream result) {
            input = result;

            if (result == null)
                return;
                    //instruction for inserting data on db .... 

            try {
                result.close();
            } catch (IOException e) {
            }
        }
    }
gezimi005
  • 381
  • 1
  • 8
  • this solution is advance, but when i used it, it gave me runtime error ?? do you have any idea about ? – AbuQauod Dec 27 '12 at 11:26
  • Man I give this example for creating an idea how AsyncTask works. I use this class on my android project for fetching json results, and showing them on ListActivity. I think it's normal to give runtime error to you, but if you give more details of what do you want to do exactly maybe I will help you. – gezimi005 Dec 27 '12 at 15:08
1

You should call URL in AsyncTask. For instance: Return data from AsyncTask class

Community
  • 1
  • 1
Volodymyr
  • 1,037
  • 1
  • 11
  • 27
0

U have to use a thread to fetch data from server and insert into to DB or in android you can use AsyncTask.

Himanshu Mohta
  • 1,033
  • 9
  • 15
0

You can have a search :ClickableSpan And put your insert code into the OnClick body~ Just like:

String yourTextStr;
    SpannableString sp = new SpannableString(yourTextStr);
    int start;//the start of your url in the text; 
    int end ;//the end of your url in the text; 
    sp.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            //Your Insert Code
        }
    }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Tzig
  • 31
  • 3