复习URLHttpConnection方式GET,POST方式链接网络解析uri

xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="com.xh.tx.postget.MainActivity" >
 6 
 7   <EditText 
 8       android:id="@+id/et_username"
 9       android:layout_width="match_parent"
10       android:layout_height="wrap_content"
11       android:hint="请输入用户名"
12     
13       />
14   <EditText 
15       android:id="@+id/et_password"
16       android:layout_width="match_parent"
17       android:layout_height="wrap_content"
18       android:layout_below="@id/et_username"
19       android:hint="请输入密码"
20      
21       />
22   
23   <Button 
24       android:id="@+id/bt_get"
25       android:layout_width="match_parent"
26       android:layout_height="wrap_content"
27       android:layout_below="@id/et_password"
28       android:text="GET提交"
29       />
30   
31     <Button 
32       android:id="@+id/bt_post"
33       android:layout_width="match_parent"
34       android:layout_height="wrap_content"
35       android:layout_below="@id/bt_get"
36       android:text="POST提交"
37       />
38 
39 </RelativeLayout>

NetUtils:

  1 package com.xh.tx.netUtils;
  2 
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.IOException;
  5 import java.io.InputStream;
  6 import java.io.OutputStream;
  7 import java.net.HttpURLConnection;
  8 import java.net.ProtocolException;
  9 import java.net.URL;
 10 import java.net.URLEncoder;
 11 
 12 public class NetUtils 
 13 {
 14     public static String getSubmit(String username,String password,String uri)
 15     {
 16         uri = uri +"?username=" + username + "&password=" + password;
 17         
 18         HttpURLConnection conn =  getHttpURLConnection(uri);
 19         // http://localhost:8080/TestServlet?username=zs&password=123
 20         
 21         try {
 22             conn.setConnectTimeout(30000);
 23             conn.setReadTimeout(30000);
 24             conn.setRequestMethod("GET");
 25             
 26             conn.connect(); //连接 连接的时候是否要传递参数过去
 27             
 28             //先判断一下状态是否为200,如果为200则将in流转换为字符串
 29             if(conn.getResponseCode() == 200)
 30             {
 31                 String content = getStringFromInputStream(conn.getInputStream());
 32                 
 33                 return content;
 34             }
 35             
 36         } catch (ProtocolException e) {
 37             e.printStackTrace();
 38         } catch (IOException e) {
 39             e.printStackTrace();
 40         }
 41         
 42         return null;
 43     }
 44     
 45     private static String getStringFromInputStream(InputStream inputStream) throws IOException
 46     {
 47         byte[] buffer = new byte[1024];
 48         ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
 49         int len = 0;
 50         
 51         while((len = inputStream.read(buffer, 0, 1024)) != -1)
 52         {
 53             bytearray.write(buffer);
 54         }
 55         
 56     //    String content = new String(bytearray.toByteArray(),"GBK");
 57         
 58         return bytearray.toString();
 59     }
 60 
 61     public static String postSubmit(String username,String password, String uri)
 62     {
 63         HttpURLConnection conn =  getHttpURLConnection(uri);
 64         
 65         try {
 66             conn.setConnectTimeout(30000);
 67             conn.setReadTimeout(30000);
 68             conn.setRequestMethod("POST");
 69             //如果你要兼容2.3版本,那么你必须添加一下这句话
 70             conn.setDoInput(true);
 71             
 72             //参数传递
 73             OutputStream out = conn.getOutputStream();
 74             
 75             conn.connect();
 76             out.write(("username="+username + "&password=" + password).getBytes());
 77             
 78             if(conn.getResponseCode() == 200)
 79             {
 80                 String content = getStringFromInputStream(conn.getInputStream());
 81                 return content;
 82             }
 83         } catch (ProtocolException e) {
 84             e.printStackTrace();
 85         } catch (IOException e) {
 86             e.printStackTrace();
 87         }
 88         
 89         return null;
 90     }
 91     
 92     public static HttpURLConnection getHttpURLConnection(String uri)
 93     {
 94         try {
 95             URL url = new URL(uri);
 96             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 97             
 98             return conn;
 99         } catch (IOException e) {
100             e.printStackTrace();
101         }
102         return null;
103         
104     }
105 }

MainActivity:

 1 package com.xh.tx.postget;
 2 
 3 import com.xh.tx.netUtils.NetUtils;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.view.Menu;
 8 import android.view.MenuItem;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.EditText;
12 import android.widget.Toast;
13 
14 public class MainActivity extends Activity implements OnClickListener {
15 
16     EditText et_username;
17     EditText et_password;
18     
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         
24         et_username = (EditText) findViewById(R.id.et_username);
25         et_password = (EditText) findViewById(R.id.et_password);
26         
27         findViewById(R.id.bt_get).setOnClickListener(this);
28         findViewById(R.id.bt_post).setOnClickListener(this);
29         
30     }
31 
32     @Override
33     public void onClick(View v) 
34     {
35         final String username = et_username.getText().toString();
36         final String password = et_password.getText().toString();
37         
38         switch (v.getId()) {
39         case R.id.bt_get:
40             new Thread(new Runnable()
41             {
42                 @Override
43                 public void run()
44                 {
45                     final String status = NetUtils.getSubmit(username, password,"http://10.0.2.2:8080/baidu/LoginServelt");
46                     
47                     runOnUiThread(new Runnable() {
48                         @Override
49                         public void run() 
50                         {
51                             Toast.makeText(MainActivity.this, "返回的状态为:" + status, 0).show();
52                         }
53                     });
54                 }
55             }).start();
56             break;
57         case R.id.bt_post:
58             new Thread(new Runnable()
59             {
60                 @Override
61                 public void run()
62                 {
63                     final String status = NetUtils.postSubmit(username, password,"http://10.0.2.2:8080/baidu/LoginServelt");
64                     
65                     runOnUiThread(new Runnable() {
66                         @Override
67                         public void run() 
68                         {
69                             Toast.makeText(MainActivity.this, "返回的状态为:" + status, 0).show();
70                         }
71                     });
72                 }
73             }).start();
74             break;
75 
76         default:
77             break;
78         }
79     }
80 }
原文地址:https://www.cnblogs.com/zzw1994/p/4918043.html