我试图在loopj上使用setUseSynchronousMode来等待http调用的结果,然后继续一个案例.我试过了:
AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
Log.d("TEST", "Got results");
}
};
AsyncHttpClient client = new AsyncHttpClient();
responseHandler.setUseSynchronousMode(true);
client.get("http://www.google.com", responseHandler);
Log.d("TEST", "Don't want to get here until after getting results");
Run Code Online (Sandbox Code Playgroud)
但结果是:
07-11 19:48:05.631 D/TEST? Don't want to get here until after getting results
07-11 19:48:05.814 D/TEST? Got results
Run Code Online (Sandbox Code Playgroud)
我误解了setUseSynchronousMode应该做什么?
我需要在几个方面使用loopj的SyncHttpClient。当我使用AsyncHttpClient时,请求成功返回。当我按此处接受的答案所示使用SyncHttpClient时:如何使用loopJ SyncHttpClient进行同步调用?,我在中遇到断点onFailure。该的StatusCode为0,错误响应是空的,抛出的java.io.IOException异常:未处理的异常:空。
这是相关的代码。再次,当我使用异步时,它工作正常:
buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// AsyncHttpClient httpClient = new AsyncHttpClient();
SyncHttpClient httpClient = new SyncHttpClient();
httpClient.get("http://10.0.1.6:3000/home/test_endpoint", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
String stringResponse = response.toString();
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
String error = errorResponse.toString();
}
});
String temp = "got here";
} …Run Code Online (Sandbox Code Playgroud) 亲爱的,我正在使用Loopj并且非常喜欢它.它让我的生活更轻松.现在我想在帖子请求的正文中发布json.请检查它我做错了我的代码如下.
params.put("SaleOrderItems", mJsonArrayOfObject.toString());
params.put("CustomerReferenceNumber", "asdf");
// /*mSaleOrder.getmCustomerReferenceNo()*/);
params.put("RecordType", "HOS");
params.put("DeliveryDate", "2012-12-28T12:04:27.3553985+01:00"); // mSaleOrder.getmDeliveryDate());
params.put("SellToCustomerNumber", "user");
Run Code Online (Sandbox Code Playgroud)
然后我这样打电话.
mAsyncHttpClient.post(WEBCONSTANTS.ORDER_SERVICE_SAVE_ORDER, mParams,
new AsyncHttpResponseHandler(){};
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
{"Message":"No HTTP resource was found that matches the request URI}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何使用LoopJ在post请求的正文中发送json对象数组.最好的祝福,
我正在尝试在Android项目上创建单元测试,该项目正在处理URL请求.我使用loopj库,但有些东西不起作用.我的清单中启用了Internet:
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)
测试方法中的Java代码:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.yahoo.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response); // <------ I never get here!?!?!
}
});
Run Code Online (Sandbox Code Playgroud)
Folowing程序(没有loopj)在相同的单元测试方法中工作:
URL yahoo;
yahoo = new URL("http://www.yahoo.com/");
BufferedReader in;
in = new BufferedReader(new InputStreamReader(yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
Run Code Online (Sandbox Code Playgroud)
似乎loopj请求在单元测试类中不起作用,但它在基本Activity类中正常工作.有什么建议吗?
我已经被困在这两天了,最后决定在这里发帖.
我看到loopj库用于异步调用,并提供了大量示例和解释.
但由于我不能在Android中的IntentSerive中使用异步调用,我被迫使用SyncHttpClient,但它似乎不起作用,因为当我使用SyncHttpClient时只调用onFailure回调.
在文档中也没有使用SyncHttpClient的示例.
这里也讨论了这个问题.
那么有人能给出正确的方法吗?
我正在使用Loopj Android异步HTTP客户端异步发送多个HTTP请求.
我正在使用静态AsyncHttpClient建议并发送多个HTTP帖子并在匿名类上接收响应.问题是,当请求返回时,我不知道如何将其与原始请求联系起来.
例如,在缓存情况下,当我发送帖子并收到200 OK时,我需要能够知道响应的请求,因此我可以将其标记为已成功发送.
我正在尝试使用,LoopJ但它给了我一个错误的Header[]参数如下图所示.它说我需要导入Header包但是当我这样做时它会给我另一个错误FileAsyncHttpResponseHandler.
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://example.com/file.png", new FileAsyncHttpResponseHandler(/* Context */ this) {
@Override
public void onFailure(int i, Header[] headers, Throwable throwable, File file) {
}
@Override
public void onSuccess(int i, Header[] headers, File file) {
}
});
Run Code Online (Sandbox Code Playgroud)
我正在使用Loopj的AsyncHttpClient来使用JSON数据执行http请求.现在我需要获取从服务器返回的http错误代码.我在哪里可以找到它?我传递的处理程序如下所示:
new JsonHttpResponseHandler()
{
@Override
public void onFailure(Throwable throwable, JSONObject object)
{
// Get error code
}
}
Run Code Online (Sandbox Code Playgroud)
这根本不可能吗?
我试图使用android httpclient(loopj)发布一些数据.我在其正文中添加一些json数据并设置请求标头.但它显示AsyncHttpClient:将忽略Passed contentType,因为HttpEntity设置了内容类型.有谁知道如何解决这个问题?
public static void post(Activity context,String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
try {
JSONObject jsonParams = new JSONObject();
JSONObject innerObject = new JSONObject();
innerObject.put("Name", "@MODE");
innerObject.put("ParamType", "8");
innerObject.put("Value", "0");
JSONArray ar = new JSONArray();
ar.put(innerObject);
try {
jsonParams.put("ProcName", "Core.MENUS_SPR");
jsonParams.put("dbparams", ar);
Log.i("jsonParams.toString()",""+jsonParams.toString());
StringEntity se = null;
try {
se = new StringEntity(jsonParams.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}
client.post(context, (url), se, "application/json", responseHandler);
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) { …Run Code Online (Sandbox Code Playgroud) android web-services loopj androidhttpclient android-async-http
我正在寻找一种方法来返回我在loopJ AsyncHttpClient onFinish或onSuccess或onFailure中得到的响应.截至目前,我有这段代码:
**jsonParse.java file**
public class jsonParse {
static JSONObject jObj = null;
static String jsonString = "";
AsyncHttpClient client;
public JSONObject getJSONObj() {
RequestParams params;
params = new RequestParams();
params.add("username", "user");
params.add("password", "password");
client = new AsyncHttpClient();
client.post("http://example.com", params, new TextHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, String response) {
jsonString = response;
Log.d("onSuccess: ", jsonString);
}
@Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {
if (statusCode == 401) {
jsonString = …Run Code Online (Sandbox Code Playgroud) 谢谢你这么棒的图书馆.只是想知道有没有办法在下载大尺寸图像/文件时下载进度或字节大小BinaryHttpResponseHandler.等待回应.
希望得到使用它的示例代码.
我正在使用loopj上传图像.
params.put("file_"+key+"", new File(String.valueOf(value.getOriginalPath())));
Run Code Online (Sandbox Code Playgroud)
但我想在上传前调整图片大小.有没有办法"动态"调整图像大小 - 没有调整大小 - >保存到SD->上传?只调整内存和上传 - InputStream?
谢谢.