android HttpUrlConnection进行GET和POST请求(案例)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ITzhongzi/article/details/88347429
示例代码

android中运用 HttpUrlRequest进行网络请求示例

  • get
 public void getClick(View v) {
        new Thread() {
            @Override
            public void run() {
                try {
                    String name = mEt_name.getText().toString();
                    String passWd = mEt_passWd.getText().toString();

                    String path = "http://192.168.80.85:8080/test/LoginServlet?name=" + name + "&passwd=" + passWd;
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    int responseCode = conn.getResponseCode();
                    if (responseCode == 200) {
                        InputStream is = conn.getInputStream();
                        String content = StreamTools.streamToString(is);
                        showToase(content);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    
  public void showToase(final String content) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), content, Toast.LENGTH_LONG).show();
            }
        });

    }
  • post请求
public void postClick(View v) {
        new Thread() {
            @Override
            public void run() {
                try {
                    String name = mEt_name.getText().toString();
                    String passWd = mEt_passWd.getText().toString();

                    String path = "http://192.168.80.85:8080/test/LoginServlet";
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    String data = "name=" + name + "&passwd=" + passWd;
                    conn.setRequestMethod("POST");
                    conn.setConnectTimeout(5000);

                    conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
                    conn.setRequestProperty("Content-Length", data.length() + "");

                    conn.setDoOutput(true);
                    OutputStream outputStream = conn.getOutputStream();
                    outputStream.write(data.getBytes());

                    int responseCode = conn.getResponseCode();
                    if (responseCode == 200) {
                        InputStream is = conn.getInputStream();
                        String content = StreamTools.streamToString(is);
                        showToase(content);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void showToase(final String content) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), content, Toast.LENGTH_LONG).show();
            }
        });

    }

猜你喜欢

转载自blog.csdn.net/ITzhongzi/article/details/88347429