Java Android:从DefaultHttpClient到改型
我正在努力寻找一个替代品,以取代弃用的Java Android:从DefaultHttpClient到改型,java,android,retrofit,Java,Android,Retrofit,我正在努力寻找一个替代品,以取代弃用的DefaultHttpClient和连接类likeHttpPost等 在第一次尝试中,我尝试使用volley库,但似乎没有任何效果,因此经过一点研究后,我现在正在尝试使用改型1.9 在我的应用程序中,我连接到自己的restful客户端。下面是旧代码(示例文章),它工作得非常好: private static DefaultHttpClient httpClient = new DefaultHttpClient(); public stat
DefaultHttpClient
和连接类likeHttpPost
等
在第一次尝试中,我尝试使用volley
库,但似乎没有任何效果,因此经过一点研究后,我现在正在尝试使用改型1.9
在我的应用程序中,我连接到自己的restful客户端。下面是旧代码(示例文章),它工作得非常好:
private static DefaultHttpClient httpClient = new DefaultHttpClient();
public static String executePOST(Map<String, String> postParams, int connTO, int sockTO, String uri){
String res, message;
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connTO);
HttpConnectionParams.setSoTimeout(httpParams, sockTO);
HttpConnectionParams.setTcpNoDelay(httpParams, true);
httpClient.setParams(httpParams);
HttpPost httppost = new HttpPost(uri);
JSONObject json = new JSONObject();
try {
Iterator<Entry<String, String>> iterator = postParams.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<String, String> pair = (Map.Entry<String, String>)iterator.next();
json.put(pair.getKey(), pair.getValue());
}
message = json.toString();
httppost.setEntity(new StringEntity(message, "UTF8"));
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
res = EntityUtils.toString(entity).trim();
} catch (ClientProtocolException e) {
res = "Client Protocol Exception";
} catch (IOException e) {
res = e.getLocalizedMessage();
} catch (JSONException e){
res = e.getLocalizedMessage();
}
return res;
}
EmailCheck.java
public interface MyDosAPI {
@FormUrlEncoded
@POST("/kunde")
public void checkEmail(@Field("email") String email, Callback<EmailCheck> response);
}
public class EmailCheck {
@SerializedName("id")
private String id;
@SerializedName("person")
private String person;
public void setId(String id){
this.id = id;
}
public void setPerson(String person){
this.person = person;
}
public String getId(){
return id;
}
public String getPerson(){
return person;
}
}
在活动中,
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(URI_ROOT)
.build();
MyDosAPI api = adapter.create(MyDosAPI.class);
api.checkEmail(username, new Callback<EmailCheck>() {
@Override
public void success(EmailCheck emailChecks, Response response) {
Log.i("MyCount", "success");
}
@Override
public void failure(RetrofitError error) {
Log.i("MyCount", error.getMessage());
}
});
显然,我遗漏了一些重要的东西。除了改装或截击之外,是否还有其他解决方案可以像以前一样定义请求?更改为:
public interface MyDosAPI {
@POST("/kunde")
public void checkEmail(@Body String email, Callback<EmailCheck> response);
}
公共接口MyDosAPI{
@职位(“/kunde”)
public void checkEmail(@Body String email,回调响应);
}
您声明您的请求是@FormUrlEncoded
,但这不是原始代码所做的。它没有提交表格;它发布一个JSON负载。“除了改型或截击之外,还有没有其他解决方案可以让我像以前一样定义请求?”——使用Apache的HttpClient打包。或者,使用HttpUrlConnection
。或者,使用OkHttp。或者,使用AndroidSync。请参阅@commonware不是HttpClientdeprecated@meda:Android SDK中的HttpClient已弃用。如果需要,可以使用Apache的HttpClient独立打包。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
public interface MyDosAPI {
@POST("/kunde")
public void checkEmail(@Body String email, Callback<EmailCheck> response);
}