HttpClient网络访问
public class HttpClientHelper {
public static HttpClient checkNetwork(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return httpClient;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,将获取到数据储存在文件流中
*
* @param url
* :访问网络的url地址
* @return inputstream
*/
public static InputStream loadFileFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
return entity.getContent();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,将获取到的数据存在字节数组中
*
* @param url
* :访问网络的url地址
* @return byte[]
*/
public static byte[] loadByteFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("====>" + e.toString());
}
return null;
}
/**
* 作用:实现网络访问文件,返回字符串
*
* @param url
* :访问网络的url地址
* @return String
*/
public static String loadTextFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“GET”方式提交数据,再返回相应的数据
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。
* 第二个参数格式为:username=wangxiangjun&password=123456
* @return byte[]
*/
public static byte[] doGetSubmit(String url, String params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url + "?" + params);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“POST”方式提交数据,再返回相应的数据
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。 第二个参数为:List
* @return byte[]
*/
public static byte[] doPostSubmit(String url, List
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
try {
requestPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“POST”方式提交数据,再返回相应的数据
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。 Map
* @return byte[]
*/
public static byte[] doPostSubmit(String url, Map
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
List
try {
if (params != null) {
for (Map.Entry
String key = entry.getKey();
String value = entry.getValue().toString();
BasicNameValuePair nameValuePair = new BasicNameValuePair(
key, value);
parameters.add(nameValuePair);
}
}
requestPost
.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public class MainActivity extends Activity {
private SoundPool pool = null;
private HashMappoolMap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 调用初始化声音池
initSoundPool();
}
/*
* 初始化声音池 pool = new SoundPool(5, // maxStreams参数,该参数为设置同时能够播放多少音效
* AudioManager.STREAM_MUSIC, //
* streamType参数,该参数设置音频类型,在游戏中通常设置为:STREAM_MUSIC //
* srcQuality参数,该参数设置音频文件的质量,0为默认值。
*/
public void initSoundPool() {
pool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
poolMap = new HashMap() ;
poolMap.put(1, pool.load(this, R.raw.ding, 1));
poolMap.put(2, pool.load(this, R.raw.ringin, 1));
}
/*
* 播放声音,参数sound是播放音效的id,参数number是播放音效的次数 pool.play(poolMap.get(sound), //
* 播放的音乐id volumnRatio, // 左声道音量 volumnRatio, // 右声道音量 1, // 优先级,0为最低
* number, // 循环次数,0无不循环,-1无永远循环 1 // 回放速度 ,该值在0.5-2.0之间,1为正常速度
*/
public void playSound(int sound, int number) {
// 实例化AudioManager对象
AudioManager manager = (AudioManager) this
.getSystemService(this.AUDIO_SERVICE);
// 返回当前AudioManager对象的最大音量值
float audioMaxVolumn = manager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// 返回当前AudioManager对象的音量值
float audioCurrentVolumn = manager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumnRatio = audioCurrentVolumn / audioMaxVolumn;
pool.play(poolMap.get(sound), // 播放的音乐id
volumnRatio, // 左声道音量
volumnRatio, // 右声道音量
1, // 优先级,0为最低
number, // 循环次数,0:不循环,-1:永远循环
1 // 回放速度 ,该值在0.5-2.0之间,1为正常速度
);
}
public void clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_play1:
playSound(1, 0);
break;
case R.id.button_main_play2:
playSound(2, 0);
default:
break;
}
}
}
public class MainActivity extends Activity {
private WebView webView_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView_main = (WebView) findViewById(R.id.webView_main);
// 设置浏览器支持。这样webview可以处理各种通知和请求事件。
// 如果不使用该句代码,在点击超链地址后,会跳出程序,而弹出浏览器访问网页。
webView_main.setWebChromeClient(new WebChromeClient());
webView_main.setWebViewClient(new WebViewClient());
WebSettings webSetting = webView_main.getSettings();
webSetting.setJavaScriptEnabled(true); // 设置支持javascript脚本
webSetting.setAllowFileAccess(true); // 允许访问文件
webSetting.setBuiltInZoomControls(true); // 设置显示缩放按钮
webSetting.setSupportZoom(true); // 支持缩放
String data = "";
StringBuilder sb = new StringBuilder();
sb.append("
sb.append("用户名:
");
sb.append("密码:
");
sb.append("");
sb.append("");
sb.append("");
// webview组件能加载包含html代码的字符串,并且能执行html。为了防止乱码,使用loadDataWithBaseURL()方法。而不用loadData()方法。
// loadDataWithBaseURL()方法中的第二个参数时MIME,详见当天的课堂笔记。
webView_main.loadDataWithBaseURL(null, sb.toString(), "text/html",
"utf-8", null);
}
}
private ImageView imageView_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
try {
InputStream is = this.getAssets().open("abc.jpg");
imageView_main.setImageBitmap(BitmapFactory.decodeStream(is));
} catch (IOException e) {
e.printStackTrace();
}
}
良心的公众号,更多精品文章,不要忘记关注哈
《Android和Java技术栈》