編輯:關於Android編程
代碼如下。只實現了doGet方法
package com.wzw.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("password"); if(username.equals("admin")&&password.equals("123456")){ response.getWriter().println("success"); }else{ response.getWriter().println("failed"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
package com.wzw.submitdata; import com.wzw.submitdata.utils.NetUtil; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText etUsername; private EditText etPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUsername = (EditText) findViewById(R.id.et_username); etPassword = (EditText) findViewById(R.id.et_password); } public void doGet(View v){ final String username=etUsername.getText().toString(); final String password=etPassword.getText().toString(); new Thread(new Runnable() { @Override public void run() { //訪問網絡要在子線程中實現,使用get取數據 final String state=NetUtil.loginOfGet(username, password); //執行在主線程上 runOnUiThread(new Runnable() { public void run() { //就是在主線程上操作,彈出結果 Toast.makeText(MainActivity.this, state, 0).show(); } }); } }).start(); } }
package com.wzw.submitdata.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class NetUtil { /** * 使用GET訪問去訪問網絡 * @param username * @param password * @return 服務器返回的結果 */ public static String loginOfGet(String username,String password){ HttpURLConnection conn=null; try { String data="username="+username+"&password="+password; URL url=new URL("http://192.168.1.4:8080/AndroidServer/LoginServlet?"+data); conn=(HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(10000); conn.setReadTimeout(5000); conn.connect(); int code=conn.getResponseCode(); if(code==200){ InputStream is=conn.getInputStream(); String state=getStringFromInputStream(is); return state; } } catch (Exception e) { e.printStackTrace(); }finally{ if(conn!=null){ conn.disconnect(); } } return null; } /** * 根據輸入流返回一個字符串 * @param is * @return * @throws Exception */ private static String getStringFromInputStream(InputStream is) throws Exception{ ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte[] buff=new byte[1024]; int len=-1; while((len=is.read(buff))!=-1){ baos.write(buff, 0, len); } is.close(); String html=baos.toString(); baos.close(); return html; } }
這樣就可以向服務器請求數據了
(一)概述 (二)Activity1.Activity,Window與View的關系下面是自己查閱資料,看了下一點源碼的歸納所得,如果哪寫錯了歡迎
前段時間項目中用到了下拉刷新功能,之前在網上也找到過類似的demo,但這些demo的質量參差不齊,用戶體驗也不好,接口設計也不行。最張沒辦法,終於忍不了了,自己就寫了一個
RecyclerView已經出來很久了,許許多多的項目都開始從ListView轉戰RecyclerView,那麼,上拉加載和下拉刷新是一件很有必要的事情。在ListVie
廢話不多說,先看效果圖: package com.example.circlemenuofbottom.anim;import android.v