2

What i mean is that i want to send an http request to my server like:

http://blahblah.blah/index.php?command=make

in android

0

2 Answers 2

4

You can do it like :

Http Post:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://blahblah.blah/index.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("command", "make"));        
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

Http Get

HttpResponse response = null;
try {    
        // Create http client object to send request to server     
        HttpClient client = new DefaultHttpClient();
        // Create URL string
        String URL = "http://blahblah.blah/index.php?command=make";
        // Create Request to server and get response
        HttpGet httpget= new HttpGet();
        httpget.setURI(new URI(URL));
        response = client.execute(httpget);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } 
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
Sign up to request clarification or add additional context in comments.

4 Comments

This is an HTTP Post. The question is for an HTTP GET
i used your get method and my app crashes every time i push the button
I have updated Http Get in my answer. Hope it works for you now, as it is working for me. :)
Note: DefaultHttpClient is deprecated, by now. Also see stackoverflow.com/questions/31802365/…
1

You can do this simply by setting the URL to be that when you use URL Connection

// you can pass mUrl as http://blahblah.blah/index.php?command=make and if you are expecting a returned value, you can compare that with successVal

public static boolean checkSuccess(String mUrl, String successVal)
{
    InputStream is = null;
    try
    {
        URL url = new URL(mUrl);
        URLConnection ucon = url.openConnection();
        ucon.setConnectTimeout(5000);
        ucon.setReadTimeout(5000);
        is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is, 8192);
        ByteArrayBuffer baf = new ByteArrayBuffer(300);
        int current = 0;
        while ((current = bis.read()) != -1)
        {
            baf.append((byte) current);
        }
        String success = new String(baf.toByteArray());

        return success.equals(successVal);
    }
    catch (Exception e)
    {
        return false;
    }
    finally
    {
        try
        {
            if (is != null)
            {
                is.close();
            }
        }
        catch (Exception e)
        {
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.