0

ブラウザで開かずに、アプリケーションからリンクにリクエストを送信するにはどうすればよいですか?試しIntentましたが、ブラウザで開きます。"アンドロイド"

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

4 に答える 4

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 に答える
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

ブラウザで表示したい場合は、次のコードを使用します

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 に答える