android使用http怎么向服务器获得数据并用json解析
1个回答
展开全部
写个适配器
下面是代码:
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import bean.CoureBean;
import tools.StreamHelper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.VisibleForTesting;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private List<CoureBean> list = new ArrayList<CoureBean>();
private ListView lv;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
String jsonString = msg.obj.toString();
converData(jsonString);
init();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvImgList);
getJson();
}
// 绑定列表
private void converData(String jsonString) {
try {
JSONObject jOb = new JSONObject(jsonString);
JSONArray jArray = jOb.getJSONArray("Data");
for (int i = 0; i < jArray.length(); i++) {
JSONObject currentItem = jArray.getJSONObject(i);
CoureBean modelBean = new CoureBean();
modelBean.setName(currentItem.getString("name"));
modelBean.setImgUrl(currentItem.getString("picSmall"));
list.add(modelBean);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 获取json数据
private void getJson() {
new Thread() {
@Override
public void run() {
String strUrlString = "http://www.imooc.com/api/teacher?type=4&num=30";
try {
URL url = new URL(strUrlString);
HttpsURLConnection conn = (HttpsURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
String jsonString = StreamHelper.readInputStream(is);
Message msg = new Message();
msg.what = 1;
msg.obj = jsonString;
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
class ViewHolder {
ImageView imPic;
TextView txtName;
}
private void init(){
ArrayAdapter adapter = new MyAdapter(this, R.layout.list_item, list);
lv.setAdapter(adapter);
}
/**
* 自定义适配器(内部类)
*
* @author
*
*/
class MyAdapter extends ArrayAdapter<CoureBean> {
private int resourceId;
// 构造函数
public MyAdapter(Context context, int resource, List<CoureBean> objects) {
super(context, resource, objects);
resourceId = resource;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
view = LayoutInflater.from(MainActivity.this).inflate(
resourceId, null);// 拿到当前行对象
// 拿到当前视图对象里面的名字叫tvPwdTitle的TextView对象
viewHolder.txtName = (TextView) view.findViewById(R.id.txtName);
viewHolder.imPic = (ImageView) view
.findViewById(R.id.imageView1);
view.setTag(viewHolder);
} else {
view = convertView;
}
// 拿到应该放在当前行上的数据
final CoureBean dataItem = getItem(position);
viewHolder = (ViewHolder) view.getTag();
viewHolder.txtName.setText(dataItem.getName());
return view;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询