android获取网络数据有字符串、图片、文件对于不同的数据的获取方法如下
字符串:

public String getDataFromServer(String Urlpath) {
String result="";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(Urlpath);
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bufferReader = new BufferedReader(isr);
String inputLine = "";
int i=1;
while ((inputLine = bufferReader.readLine()) != null) {
result += inputLine + "\n";
Log.i("getDataFromServer","inputLine"+i+":"+inputLine);
i=i+1;
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

图片:

public Bitmap getHttpBitmap(final String url) {
try {

myFileURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) myFileURL
.openConnection();
conn.setConnectTimeout(6000);
conn.setDoInput(true);
conn.setUseCaches(false);
InputStream is = conn.getInputStream();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds=true;
byte[] data = getBytes(is);
BitmapFactory.decodeByteArray(data, 0,data.length,opts);
Log.i("ImageLoader","width="+opts.outWidth+",height="+opts.outHeight);
opts.inJustDecodeBounds=false;
opts.inSampleSize=calculateInSampleSize(opts,150,300);
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeByteArray(data, 0,data.length,opts);
Log.i("ImageLoader","houwidth="+bitmap.getWidth()+",houheight="+bitmap.getHeight());
} catch (Exception e) {
e.printStackTrace();
Log.i("HttpUtils","getHttpBitmap异常");
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;

}

文件:

public String DownloadFile(String fileURL, File directory) {

String code = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(fileURL);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
code = String.valueOf(response.getStatusLine().getStatusCode());
FileOutputStream f = new FileOutputStream(directory);
int l;
byte[] tmp = new byte[2048];

while ((l = instream.read(tmp)) != -1) {
f.write(tmp, 0, l);
}

f.close();
}

} catch (IOException e) {
e.printStackTrace();
Log.i("HttpUtils","DownloadFile异常");
} finally {

}

return code;

}

更多相关文章

  1. Android:无法在同一部手机上执行通过Linux部署编译的文件
  2. Android 字符串资源
  3. 多个dex文件定义了Landroid/支持/v13/app/FragmentCompatICS。
  4. 解决:AndroidStudio 下使用AIDL不能生成对应java文件
  5. 尽管在清单文件中指定了权限,但是ACCESS_FINE_LOCATION SecurityE
  6. Android Web-View:将本地Javascript文件注入远程网页
  7. Ubuntu Android/Sdk/build-tools/23.0.3/aapt": error=2, 没有那
  8. 小米1出现的资源文件找不到问题!
  9. android文件读写,ndk文件读写

随机推荐

  1. SVN服务器迁移
  2. 【Android】Handler机制源码解析
  3. Android开发手记一 NDK编程实例
  4. android启动时间
  5. android耗时任务_handler
  6. Android(安卓)面试题
  7. Android(安卓)判断应用程序获取通知栏权
  8. android XMl 解析神奇xstream 五: 把复杂
  9. 欢迎访问Android中国
  10. Android监听手机软键盘的弹起和关闭