您的位置:首页 > 其它

使用Spinner和AsyncTask 下载数据绑定到Spinner

2016-01-16 00:00 387 查看
功能:一个Spinner 从网络上下载数据 然后绑定到Spinner
//记得配置联网权限

1、在布局界面布局 activity_main.xml

代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/bt_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载数据"
android:onClick="downLoad"
/>

<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
/>

</LinearLayout>

----------------------------------------

2、MainActivity 类

代码

public class MainActivity extends Activity {
private Spinner sp;
private ArrayAdapter<String> adapter;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.sp = (Spinner) this.findViewById(R.id.spinner);
this.pb = (ProgressBar) this.findViewById(R.id.pb);
}

//按钮 事件监听
public void downLoad(View view){
String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";
new MyAsyncTask().execute(url);
}
class MyAsyncTask extends AsyncTask<String, Integer, byte[]>{

// private ProgressDialog dialog;
// public MyAsyncTask(Context context) {
// dialog = new ProgressDialog(context);
// dialog.setTitle("温馨提示");
// dialog.setMessage("数据下载中,请稍等...");
// dialog.setIcon(R.drawable.ic_launcher);
// }
@Override
protected void onPreExecute() {
super.onPreExecute();
//dialog.show();
//当按下按钮后 就启动工具类 然后显示进度条
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//更新进度条
pb.setProgress(values[0]);
}
@Override
protected byte[] doInBackground(String... params) {
String url = params[0];
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
long totalLength = response.getEntity().getContentLength();
byte[] b = new byte[1024];
int count = 0;
int len = 0;
if(response.getStatusLine().getStatusCode() == 200){
BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
count+=len;
int l = (int)((count/(double)totalLength)*100);
publishProgress(l);

}
bis.close();

return bos.toByteArray();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] result) {
if(result != null){
//下载完数据 就使用json解析 下载的数据
List<String> list = jsonMethod(new String(result,0,result.length));
//把解析好的数据丢到ArrayAdapter适配器里
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list);
//把适配器绑定到Spinner里
sp.setAdapter(adapter);
}else{
Toast.makeText(MainActivity.this, "数据下载失败!", Toast.LENGTH_SHORT).show();
}
//dialog.dismiss();
pb.setVisibility(View.GONE);
}

}
public List<String> jsonMethod(String string) {
List<String> list = new ArrayList<String>();
try {
JSONObject object = new JSONObject(string);
JSONObject object_paramz = object.getJSONObject("paramz");
JSONArray object_feeds = object_paramz.getJSONArray("feeds");
for(int i = 0;i<object_feeds.length();i++){
JSONObject object_data = object_feeds.getJSONObject(i).getJSONObject("data");
String data = object_data.getString("subject");
list.add(data);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: