开心,总算搞好了
调用接口获取所有用户信息,然后打印出来了。
MainActivity.java
package ink.cik.logininfoapp; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.util.List; import java.util.Map; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private final Gson gson = new Gson(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.loginButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText userName = findViewById(R.id.userName); EditText passWord = findViewById(R.id.passWord); Toast.makeText(MainActivity.this, userName.toString(), Toast.LENGTH_SHORT).show(); verLogin(); } }); } private void verLogin() { new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("https://api.565.ink/login/getAll?passwd=lanol666").build(); Response response = client.newCall(request).execute(); String responsedata = response.body().string(); Log.d("返回数据:", responsedata); parseJson(responsedata); } catch (IOException e) { e.printStackTrace(); } } }).start(); } private void parseJson(String JsonData) { Gson gson = new Gson(); List<userInfo> userInfoList = gson.fromJson(JsonData, new TypeToken<List<userInfo>>() { }.getType()); for (userInfo userInfo : userInfoList) { Log.d("信息:", userInfo.getUserName()); } } }
userInfo.java
package ink.cik.logininfoapp; public class userInfo { private int id; private String userName; private String passWord; private String nation; private int age; private String tel; public int getAge() { return age; } public String getNation() { return nation; } public int getId() { return id; } public String getPassWord() { return passWord; } public String getTel() { return tel; } public String getUserName() { return userName; } public void setAge(int age) { this.age = age; } public void setId(int id) { this.id = id; } public void setNation(String nation) { this.nation = nation; } public void setPassWord(String passWord) { this.passWord = passWord; } public void setTel(String tel) { this.tel = tel; } public void setUserName(String userName) { this.userName = userName; } }
评论 (0)