- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的 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/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!