首先,先建立好Layout :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送请求" android:textColor="#000000" android:textSize="15dp"/> <Button android:id="@+id/upload" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交请求" android:textSize="15dp" android:textColor="#000000"/> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/response_txt" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/txt_2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/txt_3" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </ScrollView> </LinearLayout>
第二步是,建立一个类,用于写字段:
package com.example.webviewapplication;
public class App {
private String id;
private String name;
private String version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}第三步,是MainActivity的设置:
package com.example.webviewapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private Button send,upload;
private TextView txt,txt_2,txt_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = findViewById(R.id.send_request);
txt = findViewById(R.id.response_txt);
upload = findViewById(R.id.upload);
txt_2 = findViewById(R.id.txt_2);
txt_3 = findViewById(R.id.txt_3);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发出一条http请求
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://10.0.2.2/get_data.json")
.build();
try {
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseJsonByGson(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
private void parseJsonByGson(final String responseData) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
List<App> appList = gson.fromJson(responseData,new TypeToken<List<App>>(){}.getType());
txt.setText("id is "+appList.get(0).getId()+";version is "+appList.get(0).getVersion()+";name is"+appList.get(0).getVersion());
txt_2.setText("id is "+appList.get(1).getId()+";version is "+appList.get(1).getVersion()+";name is"+appList.get(1).getVersion());
txt_3.setText("id is "+appList.get(2).getId()+";version is "+appList.get(2).getVersion()+";name is"+appList.get(2).getVersion());
}
});
}
}温馨提示:
要先准备好假数据,写Json格式后,放到Apache的htdocs的文档。
随时随地看视频