布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.constraint.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context="net.bwie.network.activity.PostActivity"> 9 10 <Button 11 android:id="@+id/post_btn" 12 android:text="post请求" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content"/> 15 16 </android.support.constraint.ConstraintLayout>
Activity:
1 /** 2 * 当前案例: 3 * 上传姓名和年龄,返回JSON字符串 4 */ 5 public class PostActivity extends AppCompatActivity implements View.OnClickListener { 6 7 protected Button mPostBtn; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 super.setContentView(R.layout.activity_post); 13 initView(); 14 } 15 16 @Override 17 public void onClick(View view) { 18 if (view.getId() == R.id.post_btn) { 19 doPost(); 20 } 21 } 22 23 // 使用异步任务执行POST请求 24 private void doPost() { 25 String url = "http://localhost:8080/postdemo"; 26 27 PostTask task = new PostTask(); 28 task.execute(url); 29 } 30 31 private void initView() { 32 mPostBtn = (Button) findViewById(R.id.post_btn); 33 mPostBtn.setOnClickListener(PostActivity.this); 34 } 35 36 private class PostTask extends AsyncTask<String, Void, Person> { 37 38 @Override 39 protected Person doInBackground(String... params) { 40 String requestUrl = params[0]; 41 42 URL url = null; 43 try { 44 url = new URL(requestUrl); 45 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 46 // 设置POST请求 47 connection.setRequestMethod("POST"); 48 // 设置可向服务器输出 49 connection.setDoOutput(true); 50 // 打开连接 51 connection.connect(); 52 53 // 打开连接后,向服务端写要提交的参数 54 // 参数格式:“name=asdasdas&age=123123” 55 StringBuilder stringBuilder = new StringBuilder(); 56 stringBuilder.append("name=") 57 .append("wuyanzu")// 拼接自己传入的姓名 58 .append("&") 59 .append("age=") 60 .append("123");// 拼接自己传入的年龄 61 // 获取向服务器写数据的输出流 62 connection.getOutputStream() 63 .write(stringBuilder.toString().getBytes()); 64 65 // 提交数据后,获取来自服务器的json数据 66 if (connection.getResponseCode() == 200) { 67 BufferedReader br = null; 68 br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 69 70 String json = ""; 71 String line = ""; 72 73 while ((line = br.readLine()) != null) { 74 json += line.trim(); 75 } 76 77 // 解析 78 Gson gson = new Gson(); 79 Person person = gson.fromJson(json, Person.class); 80 return person; 81 82 } 83 84 } catch (Exception e) { 85 e.printStackTrace(); 86 } 87 88 89 return null; 90 } 91 92 @Override 93 protected void onPostExecute(Person person) { 94 super.onPostExecute(person); 95 96 Log.d("1507", "name:" + person.getName() + ", age: " + person.getAge()); 97 } 98 } 99 100 }
Bean:
1 package net.bwie.network.bean; 2 3 public class Person { 4 5 private String name = ""; 6 private String age = ""; 7 8 public String getName() { 9 return name; 10 } 11 12 public void setName(String name) { 13 this.name = name; 14 } 15 16 public String getAge() { 17 return age; 18 } 19 20 public void setAge(String age) { 21 this.age = age; 22 } 23 }
权限: 1 <uses-permission android:name="android.permission.INTERNET"/>