How can I send a request from my application to a link with out opening it in a browser?
I tried Intent
But its open in a browser;
"Android"
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
How can I send a request from my application to a link with out opening it in a browser?
I tried Intent
But its open in a browser;
"Android"
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
If you want to display it in the browser then use the following code
if (!url.startsWith("http://")
&& !url.startsWith("https://"))
url = "http://" + url;
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
But if you dont want to open a browser and display it in your app itself then use WebView. There is an excellent tutorial at this link.
You can use a normal Java methods to accomplish this.
Use an HttpURLConnection
, set the headers and content and send the request.
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Set properties for con
// Set headers on this connection if required
// add url form parameters
DataOutputStream ostream = null;
try {
ostream = new DataOutputStream(con.getOutputStream());
String requestContents;
// Set the contents of the request
if (requestContents != null) {
// write the contents to the stream
ostream.writeBytes(requestContents);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ostream != null) {
ostream.flush();
ostream.close();
}
}
try it
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String link ="url";
DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
HttpPost postMethod=new HttpPost(link);
List nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("action","XYZ" ));
nameValuePairs.add(new BasicNameValuePair("udid","XYZ" ));
nameValuePairs.add(new BasicNameValuePair("user", "XYZ"));
try {
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String response=hc.execute(postMethod,res);
System.out.println("response is== "+response);
Toast.makeText(this,response, Toast.LENGTH_SHORT);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
To execute this class:
new RequestTask().execute(insert your Http link );