1

I'm trying to put a get/post request on link http://103.48.108.35:5005/dataitsno but no request is pinging on my server I'm using following code on my android application. I also want to send it in background request(does not hamper current process).

public  void  SendCont() throws UnsupportedEncodingException {
String Name="Raj";
        String OwnNo="85859657";
        String text="";
        String data = URLEncoder.encode("owndat","UTF-8")+"="+URLEncoder.encode(OwnNo, "UTF-8");
        data += "&" + URLEncoder.encode("cont", "UTF-8") + "=" + URLEncoder.encode(Name, "UTF-8");
        BufferedReader reader=null;
        try
        {

            URL url = new URL("http://103.48.108.35:5005/dataitsno?quer='"+OwnNo+"'");

            // Send POST data request

            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write( data );
            wr.flush();

            // Get the server response

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }


            text = sb.toString();
        }
        catch(Exception ex)
        {

        }
        finally
        {
            try
            {

                reader.close();
            }

            catch(Exception ex) {}
        }

        // Show response on activity
        //content.setText( text  );
    }
Akhilesh Kumar
  • 9,085
  • 13
  • 57
  • 95

1 Answers1

2

This might help u,

Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                try {

                    URL url = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setReadTimeout(10000 /* milliseconds */);
                    conn.setConnectTimeout(15000 /* milliseconds */);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    // Starts the query

                    conn.connect();
                    InputStream stream = conn.getInputStream();

                    String data = convertStreamToString(stream);

                    // u can read data here
                    stream.close();


                } catch (Exception e) {
                }
            }
        });

        thread.start();

& the convertStreamToString is,

static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
Mosharrof Rubel
  • 106
  • 2
  • 7
  • can you merge answer you gave with method at following link so it can run in background? Code is at: http://app4pc.com/static/comment/androidCode.txt – Akhilesh Kumar May 31 '15 at 07:45