我正在使用LoopJ AndroidAsyncHttp库来与我的PhP服务器通信.我遇到了问题.
我需要像这样发送一个JsonObject:
{ "data": 2376845,
"data2": 12545,
"array": [{"data3": "2013-01-10",
"data4": 23532 },
{"data3": "2013-01-11",
"data4": 523526 }]
}
Run Code Online (Sandbox Code Playgroud)
但是在javadoc中; 唯一的参数是RequestParams,并且没有任何类型的数组.谁能帮我?或者告诉我一些我可以使用的东西.谢谢.
我想使用LoopJ的AsndroidAsyncHttpt将JSON作为POST发送到我的localhost服务器.我正在使用这种方法:
public void post(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
在我的代码中,但它不起作用.这是我的代码:
private void loginUser() throws JSONException, UnsupportedEncodingException {
String login = textLogin.getText().toString();
String password = textPassword.getText().toString();
JSONObject jsonObject = new JSONObject();
if(Utility.isNotNull(login) && Utility.isNotNull(password)) {
jsonObject.put("username", login);
jsonObject.put("password", password);
invokeWS(jsonObject);
}
else{
Toast.makeText(getApplicationContext(), "Prosz? wype?ni? wszystkie pola!", Toast.LENGTH_LONG).show();
}
}
private void invokeWS(JSONObject jsonObject) throws UnsupportedEncodingException {
StringEntity entity = new StringEntity(jsonObject.toString());
AsyncHttpClient client = new AsyncHttpClient();
Log.i("SER", "http://" + Constants.address + ":" + Constants.port + "/silownia_java/rest/login/auth" …Run Code Online (Sandbox Code Playgroud) 我有一个方法,它调用另一个方法,结果并不重要。确切地说,它是对 Web 服务的 POST 请求,结果在该方法中而不是在调用方法中处理。现在我希望 main 方法在该任务完成之前返回。
本质上,我需要某种异步性。我可以在 Java 中使用哪些工具?下面是步骤:
A调用方法BB开始执行(我们对方法 b 的结果不感兴趣:它调用 Web 服务并自行处理结果)A在方法B完成之前返回关于LoopJ AndroidAsyncHttp 示例,我发出这样的get请求:
final TextView text = (TextView) findViewById(R.id.textView);
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://example.com/mypage/", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
text.append(response);
}
});
Run Code Online (Sandbox Code Playgroud)
我还添加了cookies:
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
BasicClientCookie newCookie = new BasicClientCookie("id", 17882);
myCookieStore.addCookie(newCookie);
Run Code Online (Sandbox Code Playgroud)
但在提出GET请求时,如何在请求对象中发送我的cookie?
关于文档客户端有这些方法签名:
void get(Context context, String url, AsyncHttpResponseHandler responseHandler)
void get(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler)
void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
void get(String url, AsyncHttpResponseHandler responseHandler)
void get(String …Run Code Online (Sandbox Code Playgroud) 我正在尝试向 API 发送HttpClient请求,但我的响应内容始终如一NULL。当我尝试通过 Postman 发送完全相同的 GET 请求时,我发现数据确实被返回。此外,当我使用 Fiddler 检查应用程序的请求时,我还看到返回了 XML 数据。关于为什么我NULL在 VS 中设置断点时看到的任何想法?
尽管 GET 请求返回了实际内容,但以下代码中的变量response和xml变量都被设置为。NULL
static void Main(string[] args)
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(URL);
var response = await client.GetAsync(urlParameters);
if (response.IsSuccessStatusCode)
{
var xml = await response.Content.ReadAsStringAsync();
var ds = new XmlSerializer(typeof(trainData), new XmlRootAttribute("ctatt"));
using (StringReader sr = new StringReader(xml))
{
using (XmlReader xr …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用 AsyncHttpClient 库进行网络操作。我的问题是我需要指定带有端口的代理才能连接到远程服务器。有什么想法来指定它吗?
我正在尝试使用:
AsyncHttpClient client = new AsyncHttpClient();
Run Code Online (Sandbox Code Playgroud)
它说:
无法解析符号“AsyncHttpClient”。
我正在使用最新的 Android Studio 和 build.gradle min:15 target:22
我正在尝试使用,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)
我试图将AsyncHttpClient和Scalaz Task结合在一起.通常,如果我使用AsyncHttpClient,我可以调用client.close来停止客户端.
val asyncHttpClient = new AsyncHttpClient()
println(asyncHttpClient.prepareGet("http://www.google.com"))
asyncHttpClient.close()
Run Code Online (Sandbox Code Playgroud)
所以当前将停止.但是,如果我将api调用包装到Task中.我不知道如何阻止它.
def get(s: String) = Task.async[Int](k => {
asyncHttpClient.prepareGet(s).execute(toHandler)
Thread.sleep(5000)
asyncHttpClient.closeAsynchronously()
} )
def toHandler[A] = new AsyncCompletionHandler[Response] {
def onCompleted(r: Response) = {
println("get response ", r.getResponseBody)
r
}
def onError(e: Throwable) = {
println("some error")
e
}
}
println(get("http://www.google.com").run)
Run Code Online (Sandbox Code Playgroud)
当前进程仍在运行.我在想,原因是Task和AsynClient都是异步的.我不知道应该怎么做才能关闭它
提前谢谢了