gpt4 book ai didi

android - 尝试使用 okhttpClient 连接到 URL 以获取 Json

转载 作者:行者123 更新时间:2023-11-28 23:23:43 26 4
gpt4 key购买 nike

我正在尝试为我的 Android 应用获取一个 Json 文件。我正在使用 Tomcat,这是我的代码的一部分:

final String servidor = "10.0.2.2:8080/ProjetoTCC";
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

private final String mEmail;
private final String mPasswordU;

UserLoginTask(String email, String password) {
mEmail = email;
mPasswordU = password;
}

@Override
protected Boolean doInBackground(Void... params) {

requester = new MoradorRequester();

if(requester.isConnected(LoginActivity.this)) {
intent = new Intent(LoginActivity.this, UserAreaActivity.class);

try {
morador = requester.get("http://" + servidor + "/ValidacaoLoginAndroid.json", mEmail, mPasswordU);


intent.putExtra("MORADOR", morador);
if(morador.getValidacao() == true){
startActivity(intent);}

} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast toast = Toast.makeText(LoginActivity.this, "Rede indisponível!", Toast.LENGTH_LONG);
toast.show();
}

return false;
}

在这里,我调用了尝试连接并获取 Json 对象的 MoradorRequester() 方法。这是 Moredor Requester():

    try {
JSONArray root = new JSONArray(jsonStr);
JSONObject item = null;
for (int i = 0; i < root.length(); i++ ) {
item = (JSONObject) root.get(i);

morador.setNome(item.getString("nome_completo"));
morador.setDataNascimento(item.getString("data_nascimento"));
morador.setnApartamento(item.getInt("n_apartamento"));
morador.setEmail(item.getString("email"));
morador.setValidacao((Boolean) item.get("validacao"));

}

} catch(JSONException e){
e.printStackTrace();
}
finally {
if(morador == null)
morador.setDataNascimento("10/10/2010");
morador.setEmail("email");
morador.setnApartamento(0);
morador.setNome("Sem Nome");
morador.setDataNascimento("10/10/2010");
morador.setValidacao(true);
}

请求者无法获取 Json,我总是收到我在 finally 上创建的默认用户。

我尝试调试,但当 Android 运行“String jsonStr = response.body().string();”时收到错误消息错误如下:

Response{protocol=http/1.1, code=404, message=Not Found, url=http://10.0.2.2:8080/ProjetoTCC/ValidacaoLoginAndroid.json}

此外,我在 Android 显示器上收到以下错误:

09-25 19:24:01.080 2414-2977/com.example.arthurf.tcc.app W/System.err: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
09-25 19:24:01.115 2414-2977/com.example.arthurf.tcc.app W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
09-25 19:24:01.115 2414-2977/com.example.arthurf.tcc.app W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
09-25 19:24:01.115 2414-2977/com.example.arthurf.tcc.app W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
09-25 19:24:01.115 2414-2977/com.example.arthurf.tcc.app W/System.err: at network.MoradorRequester.get(MoradorRequester.java:52)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at com.example.arthurf.tcc.app.Controller.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:220)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at com.example.arthurf.tcc.app.Controller.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:201)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
09-25 19:24:01.117 2414-2977/com.example.arthurf.tcc.app W/System.err: at java.lang.Thread.run(Thread.java:818)

我已经测试了 Json,它似乎工作正常,返回了我需要的东西,我相信这是 Android 和服务器之间连接的一些问题。

如果你愿意,你可以在 GitHub 上查看我的代码:

https://github.com/ArthurFranchetto/TCCTest1.git

我错过了什么吗?

谢谢。

最佳答案

为什么不试试 RETROFIT 1.9?

http://square.github.io/retrofit/

RETROFIT 是一个适用于 Android 的 REST 客户端 API,它比仅使用 OKHTTP 和 JsonObjects 更易于使用...

1.9 版本使用 GSON 和 OKHTTP 进行自动 Json 解析,您只需要使用回调来获取数据作为您的 bean 对象。

您可以将此项目用作开发应用的指南:

https://github.com/elcioabrahao/PDV_2016

[]的

关于android - 尝试使用 okhttpClient 连接到 URL 以获取 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com