郭霖大神第一行代码中关于POST请求的代码

栗几 2018-03-25 04:25:00
调试过程中发现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);
}
});
}
}
...全文
954 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
开发者_android 2018-03-27
  • 打赏
  • 举报
回复
URL url=new URL("http://www.qq.com"); 腾讯怎么可能让你这样POST一下,就可以注册。这又不是提供的接口。
栗几 2018-03-26
  • 打赏
  • 举报
回复
那我需要POST请求去实现注册等等功能,没有办法在这个简单模板上进行更改吗?必须要重新去一个POST请求咯
jklwan 2018-03-26
  • 打赏
  • 举报
回复
这接口不支持post,改成get

URL url=new URL("http://www.qq.com");
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");

connection.setReadTimeout(8000);
cattpon 2018-03-26
  • 打赏
  • 举报
回复
正解 https://segmentfault.com/q/1010000013440721
jklwan 2018-03-26
  • 打赏
  • 举报
回复
引用 4 楼 Chestnutyah 的回复:
有没有POST网站呢?
参考https://blog.csdn.net/mycms5/article/details/73291060 很多都能post
栗几 2018-03-26
  • 打赏
  • 举报
回复
引用 3 楼 jklwan的回复:
[quote=引用 2 楼 Chestnutyah 的回复:] 那我需要POST请求去实现注册等等功能,没有办法在这个简单模板上进行更改吗?必须要重新去一个POST请求咯
找个可以注册的网站进行测试[/quote] 有没有POST网站呢?
jklwan 2018-03-26
  • 打赏
  • 举报
回复
引用 2 楼 Chestnutyah 的回复:
那我需要POST请求去实现注册等等功能,没有办法在这个简单模板上进行更改吗?必须要重新去一个POST请求咯
找个可以注册的网站进行测试

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧