我一直在使用android-async-http(http://loopj.com/android-async-http/)lib与android但由于某种原因我无法从服务器捕获响应,我知道服务器接收和执行应该做的事情,但我无法得到答案。
以下是调用API的方法:
public User registUser(String mail, String pass) throws UnsupportedEncodingException {
final User user = new User();
user.setToken("enter");
String bodyAsJson = "{\"user\":{\"email\":\""+mail+"\",\"password\":\""+pass+"\"}}";
StringEntity entity = new StringEntity(bodyAsJson);
Header[] headers = {
new BasicHeader("Content-type", "application/json")
};
client.post(this.context, "http://104.131.189.224/api/user", headers , entity, "application/json", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject json) {
try {
json = json.getJSONObject("user");
user.setId(json.getInt("id"));
user.setEmail(json.getString("email"));
user.setPassword("123456");
user.setToken(json.getString("auth_token"));
} catch ( JSONException e) {
user.setToken("not json");
} catch (Exception e) {
user.setToken("error ");
}
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
user.setToken("comes json array");
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
user.setToken(responseString);
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
user.setToken("error");
}
@Override
public void onRetry(int retryNo) {
user.setToken("nothing");
}
});
return user;
}
当我调用该方法时,user.getToken仅显示"输入"我放入了beginin,这意味着,从未进入onSuccess,onFailure或onRetry方法。
但我知道服务器收到我的请求,因为服务器日志显示: (例如:电子邮件:carlos@prueba.com,传递:prueba)
"=>"carlos@prueba.com", "password"=>"[FILTERED]"}}
D, [2015-03-17T05:15:27.660562 #28450] DEBUG -- : (0.8ms) BEGIN
D, [2015-03-17T05:15:27.671126 #28450] DEBUG -- : User Exists (2.6ms) SELECT
1 AS one FROM `users` WHERE `users`.`email` = BINARY 'carlos@prueba.com' LIMIT
1
D, [2015-03-17T05:15:27.677448 #28450] DEBUG -- : SQL (1.0ms) INSERT INTO `us
ers` (`email`, `encrypted_password`, `created_at`, `updated_at`) VALUES ('carlos
@prueba.com', '$2a$10$Dg358IzoaG5KVJ8ZJTeViev2v5B9CAnAqIYI1Zd4EIFC.0Mh.nMU6', '2
015-03-17 05:15:27.672898', '2015-03-17 05:15:27.672898')
D, [2015-03-17T05:15:27.681514 #28450] DEBUG -- : (2.0ms) COMMIT
D, [2015-03-17T05:15:27.684634 #28450] DEBUG -- : User Exists (0.6ms) SELECT
1 AS one FROM `users` WHERE `users`.`auth_token` = '6aff3b4162cfcf3062a6db12a1c
ee2bc' LIMIT 1
D, [2015-03-17T05:15:27.685582 #28450] DEBUG -- : (0.2ms) BEGIN
D, [2015-03-17T05:15:27.690901 #28450] DEBUG -- : SQL (0.8ms) UPDATE `users`
SET `auth_token` = '6aff3b4162cfcf3062a6db12a1cee2bc', `updated_at` = '2015-03-1
7 05:15:27.687516' WHERE `users`.`id` = 11
D, [2015-03-17T05:15:27.693809 #28450] DEBUG -- : (1.8ms) COMMIT
I, [2015-03-17T05:15:27.698987 #28450] INFO -- : Rendered api/users/_user.jso
n.jbuilder (0.3ms)
I, [2015-03-17T05:15:27.700292 #28450] INFO -- : Rendered api/users/create.js
on.jbuilder (3.2ms)
I, [2015-03-17T05:15:27.701395 #28450] INFO -- : Completed 200 OK in 223ms (Vie
ws: 6.3ms | ActiveRecord: 10.0ms)
服务器应该以以下格式响应json:
{"user":{"id":3,"email":"carlos@prueba.com","auth_token":"dc45800fddee07cf9b300d2765283cb2"}}
答案 0 :(得分:0)
大多数教程已经过时,并尝试使用apache库,但我确实找到了一个有用的。
在尝试操作Team Treehouse Tutorial(https://teamtreehouse.com/library/build-a-weather-app/)来处理事件查找程序api而不是他们的天气api时,我遇到了同样的问题。
他们使用OkHttp库
build.gradle
文件中
醇>
compile 'com.squareup.okhttp:okhttp:2.4.0'
这是截至2015年7月9日的最新版本
此时一切都运行良好,现在我需要添加OkHttp代码来下载json数据
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thestephenmiller.myApp" >
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
String eventfulUrl = "https://api.eventful.com/json/events/search?l=arizona&within=15&units=miles&app_key="+apiKey;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(eventfulUrl)
.build();
并拨打电话
`
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
if (response.isSuccessful()) {
Log.v(TAG, response.body().string());
}
} catch (IOException e) {
Log.e(TAG, "Exception Caught: ", e);
}
}
});
所有这些都已添加到onCreate()
这一行
Log.v(TAG, response.body().string());
是什么输出响应日志,并在if语句中,您可以处理数据。
如果该网址未以&#34; http://&#34;开头,则代码将无法获得回复或&#34; https://&#34;但是该应用程序仍将无错运行