安卓 向服务器提交数据(方式:get、post、AsyncHttpClient )
2017-02-27 23:30
706 查看
之前写了篇如何接受解析服务器传过来的数据,今天就来讲一下如何向服务器提交数据。写了个小demo. 效果图如下:
服务端:
方法:
登录界面:
返回结果页面:
移动端:
xml:
activity:
注意:用AsyncHttpClient 框架时,需要导入相应的包,至于包可以到网上下载就不展示了,还需在清单文件加联网权限。当然,同时还需搭建一个局域网,或要有网的情况下。
服务端:
方法:
@RequestMapping("dologin.xhtml") public String login(HttpServletRequest req){ String uname=req.getParameter("uname"); String password=req.getParameter("pwd"); System.out.println("uname="+uname+" upass="+password); String result=null; //判断数据库 if("admin".equals(uname)&&"123456".equals(password)){ result="success"; }else{ result="fail"; } req.setAttribute("result", result); //System.out.println(uname+""+password); return "loginResult"; }
登录界面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <HEAD> <TITLE> ZTREE DEMO - Standard Data </TITLE> </HEAD> <body> <form action="dologin.xhtml" method="post"> name:<input type="text" name="uname"/><br/> password:<input type="text" name="pwd"/><br/> <input type="submit" value="登录"/><br/> </form> </div> </body> </html>
返回结果页面:
<%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%>${result}
移动端:
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.an_comit_data_to_server.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="admin" android:id="@+id/et_main_uname" /> <EditText android:id="@+id/et_main_upass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="123456" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByGet" android:text="登录(GET)" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByPost" android:text="登录(POST)" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByAsyncHttpClient" android:text="登录(AsyncHttpClient)" /> </LinearLayout>
activity:
import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.RequestParams; import com.loopj.android.http.ResponseHandlerInterface; import com.loopj.android.http.TextHttpResponseHandler; import org.apache.http.Header; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; 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); } //get方式登录 public void loginByGet(View view){ String uname=et_main_uname.getText().toString(); String password= et_main_upass.getText().toString(); String path="http://172.21.202.8:8090/front/dologin.xhtml"; new Mytask().execute(uname,password,path,"GET"); } //post方式登录 public void loginByPost(View view){ String uname=et_main_uname.getText().toString(); String password= et_main_upass.getText().toString(); String path="http://172.21.202.8:8090/front/dologin.xhtml"; //可变数组 new Mytask().execute(uname, password, path, "POST"); } AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); RequestParams requestParams=new RequestParams(); requestParams.put("uname",uname); requestParams.put("upass",upass); asyncHttpClient.post(path,requestParams,new TextHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, String responseBody) { super.onSuccess(statusCode, headers, responseBody); Toast.makeText(MainActivity.this, responseBody, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) { super.onFailure(statusCode, headers, responseBody, error); } }); } class MyTask extends AsyncTask { private HttpURLConnection connection; private URL url; @Override protected Object doInBackground(Object[] objects) { //获取参数的值 String uname = objects[0].toString(); String upass = objects[1].toString(); String path = objects[2].toString(); String type = objects[3].toString(); String str="uname="+uname+"&pwd="+password; try { if ("GET".equals(type)) { //用GET方式提交 path = path + "?"+str; url = new URL(path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); } else if ("POST".equals(type)) { //用POST方式提交 url = new URL(path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); //设置contentType contentLength connection.setRequestProperty("Content-Length",str.length()+""); connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); //设置允许对外输出数据 connection.setDoOutput(true); //将用户名和密码提交到服务器 connection.getOutputStream().write(str.getBytes()); } connection.setConnectTimeout(5000); if (connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String result = br.readLine(); return result; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); String s = (String) o; Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } } }
注意:用AsyncHttpClient 框架时,需要导入相应的包,至于包可以到网上下载就不展示了,还需在清单文件加联网权限。当然,同时还需搭建一个局域网,或要有网的情况下。
相关文章推荐
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- 使用get,post,httpclient三种方式向服务器提交文本数据
- Android-使用GET、POST、AsyncHttpClient向服务器提交数据
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- 使用get,post,httpclient三种方式向服务器提交文本数据
- 采用httpclient提交数据到服务器(get/post)(无服务器) → 采用get方式提交数据到服务器
- 使用get,post,httpclient三种方式向服务器提交文本数据
- c# POST和GET方式通过服务器地址提交数据
- android 向服务器提交数据(get-post-AsyncHttpClient)
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- 开源项目(asyncHttpClient) get post 方式提交
- Android通过Get,Post,HttpClient方式提交参数给服务器
- android中get方式和post方式提交数据到服务器的区别
- HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据
- android 通过get,post方式提交数据给服务器
- 使用AsyncHttpClient提交数据get-post
- android 通过get,post方式提交数据给服务器
- 程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成