Return to Snippet

Revision: 65507
at December 9, 2013 11:35 by cruddell001


Initial Code
 //post data to script
//be sure to declare private String response = ""; in declaration
    public String performPost(final String encodedData, final String theURL){
        Thread performPostThread = new Thread(new Runnable() {

            @Override
            public void run() {

                HttpURLConnection urlc = null;
                OutputStreamWriter out = null;
                DataOutputStream dataout = null;
                BufferedReader in = null;
                try {
                    BT_debugger.showIt(fragmentName + ": posting to:" + theURL);
                    BT_debugger.showIt(fragmentName + ": data: " + encodedData);
                    URL url = new URL(theURL);
                    urlc = (HttpURLConnection) url.openConnection();
                    urlc.setRequestMethod("POST");
                    urlc.setDoOutput(true);
                    urlc.setDoInput(true);
                    urlc.setUseCaches(false);
                    urlc.setAllowUserInteraction(false);
                    //   urlc.setRequestProperty(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
                    urlc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                    dataout = new DataOutputStream(urlc.getOutputStream());
                    // perform POST operation
                    dataout.writeBytes(encodedData);
                    in = new BufferedReader(new InputStreamReader(urlc.getInputStream()),8096);

                    // write html to System.out for debug
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    BT_debugger.showIt(fragmentName + ": the response=");
                    while ((line = in.readLine()) != null) {
                        BT_debugger.showIt(line);
                        sb.append(line + "\n");
                        //return response;

                    }
                    response = sb.toString();
                    in.close();
                    //return response;
                } catch (ProtocolException e) {
                    response = e.toString();
                    //return e.toString();
                } catch (IOException e) {
                    response = e.toString();
                    //return e.toString();
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                            //return  response;
                        } catch (IOException e) {
                            response = e.toString();
                            //return e.toString();
                        }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            //e.printStackTrace();
                            response = e.toString();
                        }
                    }
                }
            }
        });

        performPostThread.start();

        try {
            performPostThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return response;




    }

Initial URL


Initial Description


Initial Title
post data to script for Android 3.0

Initial Tags


Initial Language
Java