请问android怎样通过json数据从服务器获取图片

请问android怎样通过json数据从服务器获取图片,第1张

android里面,通过json数据是不会直接返回的,只会返回的url地址。
步骤: 1,通过解析json数据,获取到的地址。
2,通过的地址,再一次的请求网络(用异步任务或者hangdler里面请求网络:比如:
URL url = new URL(urlStr);
>当手机需要获取一组数据,用>1 从Service继承一个类。
2 创建startService()方法
3 创建endService()方法 重载onCreate方法和onDestroy方法,并在这两个方法里面来调用startService以及endService。
4 在startService中,通过getSystemService方法获取ContextLOCATION_SERVICE。
5 基于LocationListener实现一个新类。默认将重载四个方法onLocationChanged、onProviderDisabled、onProviderEnabled、onStatusChanged。对于onLocationChanged方法是我们更新最新的GPS数据的方法。一般我们的 *** 作都只需要在这里进行处理。
6 调用LocationManager的requestLocationUpdates方法,来定期触发获取GPS数据即可。在onLocationChanged函数里面可以实现我们对得到的经纬度的最终 *** 作。
7 最后在我们的Activity里面通过按钮来启动Service,停止Service。
示意代码如下:
package comoffbyegpsservice;
import androidappService;
import androidcontentContext;
import androidcontentIntent;
import androidlocationLocationListener;
import androidlocationLocationManager;
import androidosBinder;
import androidosIBinder;
import androidutilLog;
public class GPSService extends Service {
// 2000ms
private static final long minTime = 2000;
// 最小变更距离10m
private static final float minDistance = 10;
String tag = thistoString();
private LocationManager locationManager;
private LocationListener locationListener;
private final IBinder mBinder = new GPSServiceBinder();
public void startService() {
locationManager = (LocationManager) getSystemService(ContextLOCATION_SERVICE);
locationListener = new GPSServiceListener();
locationManagerrequestLocationUpdates(LocationManagerGPS_PROVIDER, minTime, minDistance,
locationListener);
}
public void endService() {
if (locationManager != null && locationListener != null) {
locationManagerremoveUpdates(locationListener);
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}

数据库中的文件的话,要adb shell进去查看数据库有没有该字段。存储卡上的文件的话,java中没有获取文件创建事件的接口。
一、 从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
String res = "";
try{
InputStream in = getResources()openRawResource(Rrawbbi);
//在\Test\res\raw\bbitxt,
int length = inavailable();
byte [] buffer = new byte[length];
inread(buffer);
//res = EncodingUtilsgetString(buffer, "UTF-8");
//res = EncodingUtilsgetString(buffer, "UNICODE");
res = EncodingUtilsgetString(buffer, "BIG5");
//依bbitxt的编码类型选择合适的编码,如果不调整会乱码
inclose();
}catch(Exception e){
eprintStackTrace();
}
myTextViewsetText(res);//把得到的内容显示在TextView上
二、 从asset中获取文件并读取数据(资源文件只能读不能写)
String fileName = "yantxt"; //文件名字
String res="";
try{
InputStream in = getResources()getAssets()open(fileName);
// \Test\assets\yantxt这里有这样的文件存在
int length = inavailable();
byte [] buffer = new byte[length];
inread(buffer);
res = EncodingUtilsgetString(buffer, "UTF-8");
}catch(Exception e){
eprintStackTrace();
}
三、 从sdcard中去读文件,首先要把文件通过\android-sdk-windows\tools\adbexe把本地计算机上的文件copy到sdcard上去,adbexe push e:/Ytxt /sdcard/, 不可以用adbexe push e:\Ytxt \sdcard\ 同样: 把仿真器上的文件copy到本地计算机上用: adb pull /data/data/comtt/files/Testtxt e:/
String fileName = "/sdcard/Ytxt";
//也可以用String fileName = "mnt/sdcard/Ytxt";
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
//FileInputStream fin = openFileInput(fileName);
//用这个就不行了,必须用FileInputStream
int length = finavailable();
byte [] buffer = new byte[length];
finread(buffer);
res = EncodingUtilsgetString(buffer, "UTF-8");
finclose();
}catch(Exception e){
eprintStackTrace();
}
myTextViewsetText(res);

首先,你的电脑必须在某个端口提供了服务

然后,客户端通过服务建立连接

最后,发送>

下面给一段最简单的代码:

// Get方式请求
public static void requestByGet() throws Exception {
String path = ">}

具体看代码注释,一般都是>

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/12773914.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-27
下一篇 2023-05-27

发表评论

登录后才能评论

评论列表(0条)

保存