2011年8月27日

Android HttpClient / Java URL

取得網頁

String urL = "http://www.網址..";
HttpPost  httpP = new HttpPost (urL);
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("param1", "value1"));
try {
    httpP.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
    HttpResponse httpR = new DefaultHttpClient().execute(httpP);
    if (httpR.getStatusLine().getStatusCode() == 200) {
    String ret = EntityUtils.toString(httpR.getEntity());
    Toast.makeText(main.this, out1, Toast.LENGTH_LONG).show();
    }
} catch (Exception e) {
  e.printStackTrace();
}

取得圖片
try {
    String urlAddress = "http://a.rimg.com.tw/ahd/01_106.jpg" ;
    InputStream is = (InputStream) (new URL(urlAddress)).getContent();
    Drawable image = Drawable.createFromStream(is, "src");
    capImage.setImageDrawable(image);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

沒有留言: