上次我们研究了Android端怎样解析web端上传的网络数据,这次我们来一起研究研究Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient),代码如下:

如下图这是主界面:


activaty_login.xml

            


   

   
   
LoginActivaty.Java
   
public class LoginActivity 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_login);        et_main_uname = (EditText) findViewById(R.id.et_main_uname);        et_main_upass = (EditText) findViewById(R.id.et_main_upass);    }    public void loginByGet(View view) {        String uname = et_main_uname.getText().toString();        String upass = et_main_upass.getText().toString();        String path = "http://192.168.253.1:8090/front/getlogin.xhtml";        //可变数组        new MyTask().execute(uname, upass, path, "GET");    }    public void loginByPost(View view) {        String uname = et_main_uname.getText().toString();        String upass = et_main_upass.getText().toString();        String path = "http://192.168.253.1:8090/front/getlogin.xhtml";        //可变数组        new MyTask().execute(uname, upass, path, "POST");    }    public void loginByAsyncHttpClient(View view) {        String uname = et_main_uname.getText().toString();        String upass = et_main_upass.getText().toString();        String path = "http://192.168.253.1:8090/front/getlogin.xhtml";        AsyncHttpClient asyncHttpClient=new AsyncHttpClient();        RequestParams requestParams=new RequestParams();        requestParams.put("uname",uname);        requestParams.put("upwd",upass);        asyncHttpClient.post(path,requestParams,new TextHttpResponseHandler(){            @Override            public void onSuccess(int statusCode, Header[] headers, String responseBody) {                super.onSuccess(statusCode, headers, responseBody);                Toast.makeText(LoginActivity.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 + "&upwd=" + upass;            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", "text/plain;charset=UTF-8");                    //设置允许对外输出数据                    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(LoginActivity.this, s, Toast.LENGTH_SHORT).show();        }    }}


   

   


更多相关文章

  1. GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
  2. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  3. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  4. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  5. android解析json(以解析時时价为例),java也是一样的(二)
  6. Android(安卓)- 数据存储(一)之 SharedPreferences
  7. Android(安卓)四种阴影实现方式对比
  8. Android移动开发,传输数据到电脑本地服务器(flask)
  9. 实现Activity数据中间的协同

随机推荐

  1. android sms发送、接收及格式
  2. Android(安卓)核心分析 之八------Androi
  3. SeekBar自定义
  4. android软键盘设置
  5. Android(安卓)面试题总结之Android(安卓)
  6. Android(安卓)SDK/ADT 历史版本下载地址
  7. 【置顶】用Eclipse开发Android应用程序索
  8. Android工程的建立与解析
  9. 浅析Android手机卫士保存手机安全号码
  10. android:windowBackground 和 android:ba