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/my_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.myapplication.MainActivity">
<EditText
android:id="@+id/txtURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入网址"
tools:maxLength="13" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="查询"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ScrollView>
</LinearLayout>
MainActivity.java
package com.example.administrator.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText txt;
Button mbt;
TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.txtURL);
mbt = findViewById(R.id.button1);
tv_result = findViewById(R.id.tv_content);
}
public void click(View v) {
new Thread(new Runnable() {//新线程
@Override
public void run() {
String URL = txt.getText().toString();
final String result = HttpUtil.sendGet(URL);
runOnUiThread(new Runnable() {
@Override
public void run() {
tv_result.setText(result);
}
});
}
}).start();
}
}
HttpUtil.java
package com.example.administrator.myapplication;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.util.Log;
public class HttpUtil {
public interface HttpCallbackListener{
void onFinish(String response);
void onError(Exception e);
}
/**
* Post服务请求
*
* @param requestUrl 请求地址
* @param requestbody 请求参数
* @return
*/
public static String sendPost(String requestUrl, String requestbody){
try {
//建立连接
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置连接属性
connection.setDoOutput(true); //使用URL连接进行输出
connection.setDoInput(true); //使用URL连接进行输入
connection.setUseCaches(false); //忽略缓存
connection.setRequestMethod("POST"); //设置URL请求方法
String requestString = requestbody;
//设置请求属性
byte[] requestStringBytes = requestString.getBytes(); //获取数据字节数据
connection.setRequestProperty("Content-length", "" + requestStringBytes.length);
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
connection.setRequestProperty("Charset", "UTF-8");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//建立输出流,并写入数据
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestStringBytes);
outputStream.close();
//获取响应状态
int responseCode = connection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) { //连接成功
//当正确响应时处理数据
StringBuffer buffer = new StringBuffer();
String readLine;
BufferedReader responseReader;
//处理响应流
responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((readLine = responseReader.readLine()) != null) {
buffer.append(readLine).append("\n");
}
responseReader.close();
Log.d("HttpPOST", buffer.toString());
return buffer.toString();//成功
}
}catch (Exception e){
e.printStackTrace();
}
return "2";//失败
}
/**
* Get服务请求
*
* @param requestUrl
* @return
*/
public static String sendGet(String requestUrl){
try{
//建立连接
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(false);
connection.setDoInput(true);
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.connect();
//获取响应状态
int responseCode = connection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) { //连接成功
//当正确响应时处理数据
StringBuffer buffer = new StringBuffer();
String readLine;
BufferedReader responseReader;
//处理响应流
responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((readLine = responseReader.readLine()) != null) {
buffer.append(readLine).append("\n");
}
responseReader.close();
Log.d("HttpGET", buffer.toString());
//JSONObject result = new JSONObject(buffer.toString());
return buffer.toString();
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
调试了很久,测试一直没反应,最后发现是xml布局的问题。。。