Android之向服务器提交数据(POST,GET,AsyncHttpClient)

1.布局
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:id="@+id/et_main_uanem"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/et_main_upwd"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="loginGET"
        android:text="登录(GET)"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="loginPOST"
        android:text="登录(POST)"/>
</LinearLayout>


<?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:id="@+id/activity_main_post"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:id="@+id/et_Async_uanem"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/et_Async_upwd"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="loginAsyncHttpClient"
        android:text="登录(AsyncHttpClient)"/>

</LinearLayout>

2.java代码
使用Android原生态提交数据(POST,GET)

public class MainActivity extends AppCompatActivity {

    private EditText et_main_uanem;
    private EditText et_main_upwd;
    private HttpURLConnection connection;
    private URL url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_main_uanem = (EditText) findViewById(R.id.et_main_uanem);
        et_main_upwd = (EditText) findViewById(R.id.et_main_upwd);
    }

    public void loginGET(View view) {
        String uname = et_main_uanem.getText().toString();
        String upwd = et_main_upwd.getText().toString();
        String path = "http://192.168.43.149:8080/Mysqldenglu.xhtml";
        new MyTask().execute(uname, upwd, path, "GET");
    }
    public void loginPOST(View view) {
        String uname = et_main_uanem.getText().toString();
        String upwd = et_main_upwd.getText().toString();
        String path = "http://192.168.43.149:8080/Mysqldenglu.xhtml";
        new MyTask().execute(uname, upwd, path, "POST");
    }

    class MyTask extends AsyncTask {
        @Override
        protected Object doInBackground(Object[] params) {
            //获取参数的值
            String uname = params[0].toString();
            String upwd = params[1].toString();
            String path = params[2].toString();
            String type = params[3].toString();
            String str="sname=" + uname + "&spwd=" + upwd;
            //判断提交方式是GET还是POST
            try {
                if ("GET".equals(type)) {
                    path = path + "?"+str;
                    Log.i("哈哈", path);
                    url = new URL(path);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod(type);
                } else if ("POST".equals(type)) {
                    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 (Exception 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_LONG).show();
        }

    }
}


使用第三方框架提交数据(AsyncHttpClient)
public class MainActivity extends AppCompatActivity {

    private EditText et_main_uanem;
    private EditText et_main_upwd;
    private HttpURLConnection connection;
    private URL url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_main_uanem = (EditText) findViewById(R.id.et_main_uanem);
        et_main_upwd = (EditText) findViewById(R.id.et_main_upwd);
    }

    public void loginGET(View view) {
        String uname = et_main_uanem.getText().toString();
        String upwd = et_main_upwd.getText().toString();
        String path = "http://192.168.43.149:8080/Mysqldenglu.xhtml";
        new MyTask().execute(uname, upwd, path, "GET");
    }
    public void loginPOST(View view) {
        String uname = et_main_uanem.getText().toString();
        String upwd = et_main_upwd.getText().toString();
        String path = "http://192.168.43.149:8080/Mysqldenglu.xhtml";
        new MyTask().execute(uname, upwd, path, "POST");
    }

    class MyTask extends AsyncTask {
        @Override
        protected Object doInBackground(Object[] params) {
            //获取参数的值
            String uname = params[0].toString();
            String upwd = params[1].toString();
            String path = params[2].toString();
            String type = params[3].toString();
            String str="sname=" + uname + "&spwd=" + upwd;
            //判断提交方式是GET还是POST
            try {
                if ("GET".equals(type)) {
                    path = path + "?"+str;
                    Log.i("哈哈", path);
                    url = new URL(path);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod(type);
                } else if ("POST".equals(type)) {
                    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 (Exception 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_LONG).show();
        }

    }
}

3.清单文件添加权限
<!--添加联网的权限-->
    <uses-permission android:name="android.permission.INTERNET" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值