先看看效果
利用下拉列表,显示获取的图片文件列表,然后选择图片进行显示
获取服务器端图片文件列表需要移动端和服务端的双向配合
1 服务器端代码
using Newtonsoft.Json; //C#中处理JSON
public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string userFile = "D:\phpstudy_pro\WWW\WebSite2\WxpPic";
DirectoryInfo root = new DirectoryInfo(userFile);
FileInfo[] files = root.GetFiles();
//输出图片文件列表
Dictionary dic = new Dictionary();
int iCount=files.Count();
if (iCount>0)
{
for(int i=0;i
2 移动端代码
//需要一个全局变量数组
public String[] WxpPicArray2;
//调用函数
//获取图片列表
private void WxpGetPiclist()
{
//获取图片列表
String url = "http://x.x.x.x/default5.aspx";
WxpGetNetworkPiclistTask mTask = new WxpGetNetworkPiclistTask();
mTask.execute(url);
}
实现功能的异步线程类 WxpGetNetworkPiclistTask
//获取网络图片列表异步线程类
private class WxpGetNetworkPiclistTask extends AsyncTask {
@Override
protected void onPostExecute(String result) {
//最终结果的显示
//mTvProgress.setText(result);
tx.setText(result);
//声明一个下拉列表的数组适配器
ArrayAdapter starAdapter = new ArrayAdapter(getActivity(),R.layout.item_select,WxpPicArray2);
//设置数组适配器的布局样式
starAdapter.setDropDownViewResource(R.layout.item_dropdown);
//设置下拉框的标题,不设置就没有难看的标题了
spList.setprompt("请选择图片");
//设置下拉框的数组适配器
spList.setAdapter(starAdapter);
//设置下拉框默认的显示第一项
spList.setSelection(0);
}
@Override
protected void onPreExecute() {
//开始前的准备工作
//mTvProgress.setText("loading...");
tx.setText("loading .....");
WxpPicArray2= new String[]{};
}
@Override
protected void onProgressUpdate(Integer... values) {
//显示进度
// mPgBar.setProgress(values[0]);
// mTvProgress.setText("loading..." + values[0] + "%");
tx.setText("loading:" + values[0] + "%");
}
@Override
protected String doInBackground(String... params) {
//这里params[0]和params[1]是execute传入的两个参数
String url = params[0];
BufferedReader bufferedReader = null;
try {
url = url + "";
tx.setText("位置信息");
URL httpUrl = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
//设置请求头header
httpURLConnection.setRequestMethod("GET");
//设置请求头header
httpURLConnection.setRequestProperty("test-header", "get-header-value");
httpURLConnection.setReadTimeout(5000);
//获取内容
InputStream inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
final StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
//解析JSON
JSonObject jsonObject = new JSonObject(stringBuffer.toString());
Iterator> it = jsonObject.keys();
String keys = "";
String vals = null;
String sourceStr = "";
List listPicname=new ArrayList();
//遍历keys
while (it.hasNext()) {//遍历JSonObject
keys = (String) it.next().toString();
// sourceStr = sourceStr + jsonObject.get(keys).toString() + ";";
listPicname.add(jsonObject.get(keys).toString());
}
WxpPicArray2=listPicname.toArray(new String[listPicname.size()]);
return "success";
} catch (Exception e) {
e.printStackTrace();
return (e.toString());
}
}
}



