从web端同时取值和图片
1.布局
base.xml
2.web Servlet代码
public class MyServlet3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "text/html; charset=utf-8");
ArrayList<User> list=new ArrayList<User>();
for(int i=0;i<10;i++){
User user=new User();
user.setUn("用户"+i);
user.setPw("密码"+i);
user.setPhoto("out.jpg");;
list.add(user);
}
//只认json格式的串
Gson g=new Gson();
String info=g.toJson(list);
PrintWriter out= response.getWriter();
out.print(info);
out.flush();
out.close();
}
}
3.MainActivity 代码
public class Main5Activity extends AppCompatActivity {
private ListView listView;
private String path="http://10.151.2.14:8080/lesson13/MyServlet3";
ArrayList<User> list=new ArrayList<User>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
listView= (ListView) findViewById(R.id.listView);
try {
String info = new MyTask3().execute(path).get();
JSONArray jsonArray=new JSONArray(info);//把json串变成一个数组
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject= jsonArray.getJSONObject(i);
User user=new User();
user.setUn(jsonObject.get("un").toString());
user.setPw(jsonObject.get("pw").toString());
user.setPhoto(jsonObject.get("photo").toString());
list.add(user);
}
Log.i("message",info);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
listView.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView==null){
view= LayoutInflater.from(Main5Activity.this).inflate(R.layout.base,null);
}else {
view=convertView;
}
TextView un= (TextView) view.findViewById(R.id.textView);
// TextView pw= (TextView) view.findViewById(R.id.pw);
un.setText(list.get(position).getUn());
//pw.setText(list.get(position).getPw());
ImageView imageView= (ImageView) view.findViewById(R.id.imageView3);
String pathImg="http://10.151.2.14:8080/lesson13/image/"+list.get(position).getPhoto();
try {
Bitmap bitmap= new MyTask4().execute(pathImg).get();
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
}
}
class MyTask3 extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
//post方式传值
try {
HttpURLConnection httpURLConnection= (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setReadTimeout(8000);
//接到响应的数据
InputStream inputStream=httpURLConnection.getInputStream();
//实际的数据是字符串 把inputStream变成reader
Reader reader=new InputStreamReader(inputStream);
//一行一行读
BufferedReader bufferedReader=new BufferedReader(reader);
StringBuffer stringBuffer=new StringBuffer();
String str=null;
while((str=bufferedReader.readLine())!=null){
stringBuffer.append(str);
}
return stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
class MyTask4 extends AsyncTask<String,Void,Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
try {
HttpURLConnection httpURLConnection= (HttpURLConnection) new URL(params[0]).openConnection();
//post方式传值
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setReadTimeout(8000);
Bitmap bitmap= BitmapFactory.decodeStream(httpURLConnection.getInputStream());
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
}
}
4,安卓和web端都有实体类
public class User {
//web 上传file 把图片名写到数据库
//把对应数据库图片名字的图片写到服务器
//从数据库查询数据 得到图片名 根据图片名去服务器找图片
private String un;
private String pw;
private String photo;//只存文件名
public String getUn() {
return un;
}
public void setUn(String un) {
this.un = un;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}