0

如何从我的应用程序向链接发送请求而不在浏览器中打开它?我试过Intent但它在浏览器中打开;“安卓”

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
4

4 回答 4

0

如果要在浏览器中显示,请使用以下代码

if (!url.startsWith("http://")
                    && !url.startsWith("https://"))
                url = "http://" + url;
            browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);

但是,如果您不想打开浏览器并将其显示在您的应用程序本身中,请使用 WebView。这个链接有一个很好的教程。

于 2012-10-03T05:49:42.500 回答
0

您可以使用普通的 Java 方法来完成此操作。

使用HttpURLConnection,设置标题和内容并发送请求。

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();
    }
}
于 2012-10-03T05:50:57.570 回答
0

尝试一下

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();
}
于 2012-10-03T05:54:52.253 回答
0
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;
    }

要执行这个类:

new RequestTask().execute(insert your Http link );
于 2012-10-07T13:50:32.547 回答