Android后台提交数据到服务器,一种是post,一种是get。两种方式的区别有两点,1.get方法的路径需带参 2.post方法多了请求头部长度与类型
post与get属于原生态方法,还有使用 网络请求框架:AsyncHttpClient方法。今天用一个登录案例来比较一下,代码如下:
public class MainActivity extends AppCompatActivity { private EditText et_main_uname; private EditText et_main_upass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_uname = (EditText) findViewById(R.id.et_main_uname); et_main_upass = (EditText) findViewById(R.id.et_main_upass); } public void loginGET(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); new MyTask().execute(uname,upass,"GET"); } public void loginPOST(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); new MyTask().execute(uname,upass,"POST"); } public void loginAsyncHttpClient(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); RequestParams params=new RequestParams(); params.put("uname",uname); params.put("upass",upass); // Ctrl+H // ResponseHandlerInterface asyncHttpClient.post("http://193.168.3.134:8080/G150725_S2SH/loginActionlogin.action",params,new TextHttpResponseHandler(){ @Override public void onSuccess(int statusCode, org.apache.http.Header[] headers, String responseBody) { super.onSuccess(statusCode, headers, responseBody); Toast.makeText(MainActivity.this, ""+responseBody, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int statusCode, org.apache.http.Header[] headers, String responseBody, Throwable error) { super.onFailure(statusCode, headers, responseBody, error); } }); } class MyTask extends AsyncTask{ private URL url; private HttpURLConnection connection; @Override protected Object doInBackground(Object[] objects) { String uname=objects[0].toString(); String upass=objects[1].toString(); String type=objects[2].toString(); try { if("GET".equals(type)){ //用GET方式请求 url = new URL("http://193.168.3.134:8080/G150725_S2SH/loginActionlogin.action?uname="+uname+"&upass="+upass); Log.i("test","get方式"); }else if("POST".equals(type)){ Log.i("test","post方式"); //用POST方式请求 url = new URL("http://193.168.3.134:8080/G150725_S2SH/loginActionlogin.action"); } connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); connection.setConnectTimeout(5000); if("POST".equals(type)){ //设置可以允许对外输出数据 connection.setDoOutput(true); String str="uname="+uname+"&upass="+upass; //添加请求头 //Content-Length:24 connection.setRequestProperty("Content-Length",""+str.length()); //Content-Type:application/x-www-form-urlencoded connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); //将内容提交到服务器 connection.getOutputStream().write(str.getBytes()); } if(connection.getResponseCode()==200){ InputStream is= connection.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String str=br.readLine(); return str; // } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPreExecute() { super.onPreExecute(); } //更新UI @Override protected void onPostExecute(Object o) { super.onPostExecute(o); String s= (String) o; if("success".equals(s.trim())){ Toast.makeText(MainActivity.this, "跳转到主界面", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } //创建时间 修改时间 } } } |
相比较而言,网络请求框架:AsyncHttpClient比原生态的方式简单了些,只肖几行代码就搞定了,方法都已封装好,只要使用就可以了