我使用loopj Android异步Http客户端,我有注册问题。注册码:
private void register(){
RequestParams params = new RequestParams();
params.put("email", "uuu@ii.com");
params.put("name", "Lurk");
params.put("surname", "More");
params.put("password", "123456789");
params.put("person", "0");
Client.post("register", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
//Log.d("Json_reg", response.toString());
try {
JSONObject idJson = response.getJSONObject("_id");
Log.d("Json_reg", response.toString());
String strId = idJson.getString("$iod");
Log.d("my_id", strId);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.d("THROW", throwable.toString());
}
});
}
日志显示:
org.apache.http.client。HttpResponseException:内部服务器错误07-28 11:14:48.676 20728-20728/com.example.murager。日志含义httpapp W/JsonHttpRH: onFailure(int, Header[], Throwable, JSONObject)没有被覆盖,但是收到回调org.apache.http.client.HttpResponseException: Internal Server Error
但是当我登录时一切正常:登录代码:
private void login(){
RequestParams params = new RequestParams();
params.put("email", "zzz@aa.com");
params.put("password", "123456789");
params.put("person", "0");
Client.post("login", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
try {
Log.d("Json_con", response.toString());
String nameStr = response.getString("name");
String snameStr = response.getString("surname");
JSONObject id = response.getJSONObject("_id");
String strId = id.getString("$oid");
String str = nameStr + " " + snameStr + " " + strId;
tv.setText(str);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
My Client is here:
public class Client {
private static AsyncHttpClient client = new AsyncHttpClient();
private static String BASE_URL = "some_url";
public Client() {
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler handler){
client.post(getAbsoluteUrl(url), params, handler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
我的问题是为什么两个相似的函数显示不同的结果?
使用弃用库进行注册工作正常。代码:
private void makeRegistration(){
String nameString = nameEditText.getText().toString();
String surnameString = nameEditText.getText().toString();
String emailString = emailEditText.getText().toString();
String passwordString = passwordEditText.getText().toString();
ArrayList<NameValuePair> param = new ArrayList<>();
param.add(new BasicNameValuePair("name", nameString));
param.add(new BasicNameValuePair("surname", surnameString));
param.add(new BasicNameValuePair("email", emailString));
param.add(new BasicNameValuePair("password", passwordString));
param.add(new BasicNameValuePair("person", userTypeInt + ""));
Log.d("User_type", userTypeInt + "");
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = jsonParser.makeHttpRequest("http://server/api/v1/r", "POST", param);
Log.d("What_I_get_2", jsonObject.toString() + "n");
try {
String name = jsonObject.getString("name");
String surname = jsonObject.getString("surname");
String id = jsonObject.getString("_id");
Log.d("What_I_get", jsonObject.toString() + "n" + id);
} catch (JSONException e) {
e.printStackTrace();
}
}
org.apache.http.client。HttpResponseException: Internal Server Error意味着你的服务器http://server_url/api/v1/抛出这个错误,你的代码没有问题。
如果你有访问服务器日志,你应该检查更多的细节,否则问相应的服务器人员的问题。这可能是您在寄存器请求中缺少某些参数的情况。无论如何,服务器应该发送回一些有意义的http响应代码。500服务器内部错误不正确
我有相同的错误警告。下面是技巧:
尝试从您的overide方法中删除下面的行
super.onFailure(statusCode, headers, throwable, errorResponse);