郭霖大神第一行代码中关于POST请求的代码
调试过程中发现POST请求中无法触发View控件,也郭霖的第一行代码里面也没给全POST请求的代码,查了诸多资料都没用相关解释,求大神解答
下面附上布局代码,和java类代码
activity_main.xml
<?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"
tools:context="com.example.okhttp.MainActivity">
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/register_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
Register.java
package com.example.okhttp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Register extends AppCompatActivity implements View.OnClickListener{
TextView registerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendRegister=(Button)findViewById(R.id.register);
registerText=(TextView)findViewById(R.id.register_text);
sendRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.register){
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection(){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection=null;
BufferedReader reader=null;
try{
URL url=new URL("http://www.qq.com");
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
out.writeBytes ("userid=id&password=123&username=qxl");
connection.setReadTimeout(8000);
InputStream in=connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(in));
StringBuilder response=new StringBuilder();
String line;
while((line=reader.readLine())!=null){
response.append(line);
}
showResponse(response.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
private void showResponse(final String response){
runOnUiThread(new Runnable() {
@Override
public void run() {
registerText.setText(response);
}
});
}
}
下面附上布局代码,和java类代码
activity_main.xml
<?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"
tools:context="com.example.okhttp.MainActivity">
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/register_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
Register.java
package com.example.okhttp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Register extends AppCompatActivity implements View.OnClickListener{
TextView registerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendRegister=(Button)findViewById(R.id.register);
registerText=(TextView)findViewById(R.id.register_text);
sendRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.register){
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection(){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection=null;
BufferedReader reader=null;
try{
URL url=new URL("http://www.qq.com");
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
out.writeBytes ("userid=id&password=123&username=qxl");
connection.setReadTimeout(8000);
InputStream in=connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(in));
StringBuilder response=new StringBuilder();
String line;
while((line=reader.readLine())!=null){
response.append(line);
}
showResponse(response.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
private void showResponse(final String response){
runOnUiThread(new Runnable() {
@Override
public void run() {
registerText.setText(response);
}
});
}
}
...全文
相关推荐
郭霖第二行代码---源代码.rar
郭霖第二行代码---源代码.rar,,,,,,,,,,,,,,
第一行代码第3版随书源码详解:Kotlin与Jetpack工程实践
在Android开发学习路径中,掌握一门语言和一套现代架构必不可少。Kotlin作为官方首选语言,凭借空安全、扩展函数和协程等特性,极大提升了开发效率;而Jetpack与Compose则重新定义了UI构建和数据处理方式。理解这些概念背后的原理,能帮助开发者从“会写代码”进阶到“懂设计”。本文从工程实践角度出发,围绕代码组织、环境配置、核心章节精读与问题排查,系统拆解一套完整的Android学习源码。无论你是刚接触Android Studio的初学者,还是想补齐Kotlin和Jetpack栈的开发者,都能通过
《第一行代码》第3版随书源码跑通指南:从环境配置到避坑全攻略
在Android开发中,环境配置与工具链版本兼容是初学者最常见的拦路虎。Android Studio、JDK与Gradle的版本匹配直接影响项目能否顺利构建。面对老版本源码,如《第一行代码》第3版随书源码,在新工具链上运行时往往出现Gradle同步失败、SDK缺失等问题。本文从开发环境搭建、Gradle配置、依赖仓库加速与API Key申请等角度,系统梳理了如何让老项目在新系统上跑通的完整流程,并分析了Android 13+权限变更对老代码的影响。无论你是刚入门Android的初学者,还是想用源码查漏补缺的
第一行代码多章相关代码
请查看专栏:https://blog.csdn.net/lhk147852369/column/info/29177
CoolWeather:第一行代码第二版,酷欧天气开源项目研究
凉爽的天气 第一行代码第二版,酷欧天气开源项目研究


