Android:网络存储(3)

从web端同时取值和图片

1.布局

Android:网络存储(3)_第1张图片
base.xml
Android:网络存储(3)_第2张图片

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 list=new ArrayList();
         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 list=new ArrayList();
    @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{

    @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{

    @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;
	}
}

你可能感兴趣的:(Andiord)