我想使用ljoop库来发出http请求。但是,当我执行get方法或post时,它只是跳过它,不会进入回调。
ip地址成功地在浏览器中提供了json结果。所以它可以使用10.0.2.2我知道这一点,但它也适用于192 ....在浏览器中。 我已经添加了互联网权限,但它似乎似乎没有发送获取请求。带有express的nodejs服务器只有在模拟器上使用chrome时才会收到任何内容。
import android.app.Activity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.List;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.conn.ConnectTimeoutException;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;
import schaschinger.eggermode.Restservice.Models.Item;
import schaschinger.eggermode.Restservice.Models.Stammkunde;
public class HttpRequestTask {
public static final String URL_STAMMKUNDEN = "http://192.168.1.190:9999/stammkunden/stammkunden";
public static final String URL_ITEMS = "http://192.168.1.190:9999/item/items";
Activity activity;
final AsyncHttpClient asyncHttpClient;
HttpResponseHandler httpResponseHandler;
public HttpRequestTask(Activity activity) {
this.activity = activity;
this.asyncHttpClient = new AsyncHttpClient();
httpResponseHandler = new HttpResponseHandler();
this.asyncHttpClient.allowRetryExceptionClass(IOException.class);
this.asyncHttpClient.allowRetryExceptionClass(SocketTimeoutException.class);
this.asyncHttpClient.allowRetryExceptionClass(ConnectTimeoutException.class);
}
public boolean insertStammkunde(Stammkunde stammkunde) {
String stammkundenJson = stammkunde.getJson();
StringEntity stringEntity = null;
try {
stringEntity = new StringEntity(stammkundenJson);
} catch (UnsupportedEncodingException e) {
return false;
}
stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
RequestHandle requestHandle = asyncHttpClient.post(activity.getApplicationContext()
, HttpRequestTask.URL_STAMMKUNDEN, stringEntity, "application/json", httpResponseHandler);
return requestHandle.isFinished();
}
public List<Item> getItems(){
asyncHttpClient.setConnectTimeout(1000);
asyncHttpClient.get("https://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
return null;
}
}
答案 0 :(得分:0)
当控制到达具有异步调用的行时,您的代码不应该调用回调。相反,当服务器向您的请求发送响应时,稍后会调用回调。这就是“异步”的含义。
您可以返回Future中包含的项目,例如,将getItems
的返回类型更改为CompletableFuture<List<Item>>
并返回CompletableFuture。在complete
回调中调用未来onSuccess
,在completeExceptionally
回调中调用onFailure
。
然后,您可以选择调用将来的get
来同步获取结果(这将阻止直到请求完成或超时),或者使用handle
(或{{分配回调)来自CompletionStage的{}}和thenAccept
} exceptionally
实现。