我正在使用Retrofit android最新版本1.6与OKhttp2.0.0-RC2与Okio
当我调用Post方法时,这个Json generator URL调用代码可以正常运行状态代码200(我称之为成功(..)方法,解析json一切正常!
JSON:
{
"responseCode": "200",
"responseMessage": "OK",
"responseDetail": {
"surname": "Trevor",
"forename": "Ross",
"dob": "10101960",
"appRefNo": "1258866",
"result": "Success"
}
}
代码: String hostUrl =
void doTrackRef(Map<String, String> paramsref2) {
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(hosturl)
.setLogLevel(RestAdapter.LogLevel.FULL) .setErrorHandler(new MyErrorHandler())。build();
TrackerRefRequest userref = restAdapter.create(TrackerRefRequest.class);
userref.login(paramsref2,
new Callback<TrackerRefResponse>() {
@Override
public void success(
TrackerRefResponse trackdetailresponse,
Response response) {
Toast.makeText(TrackerActivity.this, "Success",
Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError retrofitError) {
Toast.makeText(TrackerActivity.this, "No internet",
Toast.LENGTH_SHORT).show();
}
});
}
MyErrorHandler类:
class MyErrorHandler implements ErrorHandler {
@Override public Throwable handleError(RetrofitError cause) {
Response r = cause.getResponse();
if (r != null && r.getStatus() == 401) {
return new Throwable(cause);
}
return cause;
}
}
}
POJO的:
public class TrackerRefResponse {
private String responseCode;
private String responseMessage;
private ResponseDetail responseDetail;
//Setters and Getters
}
以上代码100%正常工作。
问题: 当我将Json url api更改为IP编号的数字网址ex:
时我收到此错误日志:
D/Retrofit(9114): ---> HTTP POST http://[IP-Address]/api/index.php/data/TrackerInfo
D/Retrofit(9114): Content-Type: application/x-www-form-urlencoded; charset=UTF-8
D/Retrofit(9114): Content-Length: 22
D/Retrofit(9114): app_ref_no=E0000000001
D/Retrofit(9114): ---> END HTTP (22-byte body)
D/dalvikvm(9114): GC_FOR_ALLOC freed 224K, 7% free 4667K/4996K, paused 2ms, total 4ms
D/Retrofit(9114): ---- ERROR http://[IP-Address]/api/index.php/data/TrackerInfo
D/Retrofit(9114): java.net.UnknownHostException: host == null
D/Retrofit(9114): at com.squareup.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
D/Retrofit(9114): at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:231)
D/Retrofit(9114): at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
D/Retrofit(9114): at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:317)
D/Retrofit(9114): at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
D/Retrofit(9114): at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:420)
D/Retrofit(9114): at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:105)
D/Retrofit(9114): at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:239)
D/Retrofit(9114): at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:68)
D/Retrofit(9114): at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
D/Retrofit(9114): at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
D/Retrofit(9114): at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
D/Retrofit(9114): at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
D/Retrofit(9114): at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
D/Retrofit(9114): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
D/Retrofit(9114): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
D/Retrofit(9114): at retrofit.Platform$Android$2$1.run(Platform.java:142)
D/Retrofit(9114): at java.lang.Thread.run(Thread.java:841)
D/Retrofit(9114): ---- END ERROR
更新
这个问题已经被接受的答案解决了!
答案 0 :(得分:4)
Android的URI类与0
的第四个八位字节中的额外192.168.100.06
不同。将其替换为规范的IP地址192.168.100.6
,您就可以了。
我向AOSP问题跟踪器报告了this bug。我并非100%确定他们会修复它;使用非规范主机名存在主要缺点。