与使用相同服务器端服务的iPhone App相比,HttpURLConnection.getInputStream需要花费大量时间。
以下代码用于该服务:
date= new java.util.Date();
Log.d("time","Time Stamp before posting "+new Timestamp(date.getTime()));
URL ur= new URL(url);
HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
conn.setRequestProperty("Connection", "close");
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(nameValuePairs));
writer.close();
os.close();
conn.connect();
StringBuffer response=null;
try{
Log.d("time","Time Stamp bfr InputStream "+new Timestamp(date.getTime()));
InputStream is = conn.getInputStream();
date= new java.util.Date();
Log.d("time","Time Stamp aftr InputStream "+new Timestamp(date.getTime()));
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
response = new StringBuffer();
while((line = …Run Code Online (Sandbox Code Playgroud) 我尝试在我的单例类中设置我的asynchttpclient的超时,但似乎它没有改变.我得到10000,我认为是默认值.
client.setMaxRetriesAndTimeout(0,5000);
Log.i("loopj", ""+ client.getTimeout());
Run Code Online (Sandbox Code Playgroud)
编辑:
client.setTimeout(5000);
Run Code Online (Sandbox Code Playgroud)
这个方法工作但是对于信息,setMaxRetriesAndTimeout怎么没有?
我想重新打包apache的httpclient lib,用一个Android应用程序发送它(比如https://code.google.com/p/httpclientandroidlib/但是使用HttpClient 4.3.1)
因此,我手动下载了httpclient 4.3.1 jar(包括其所有依赖项)并使用jarjar重新打包它:
x@x$: cd libs && for f in *.jar; do java -jar ../jarjar-1.4.jar process ../rules.txt $f out/my-$f; done
Run Code Online (Sandbox Code Playgroud)
使用rules.txt:
rule org.apache.http.** my.repackaged.org.apache.http.@1
Run Code Online (Sandbox Code Playgroud)
然后我用ant将输出放在一起:
<project name="MyProject" default="merge" basedir=".">
<target name="merge">
<zip destfile="my-org-apache-httpclient-4.3.1.jar">
<zipgroupfileset dir="libs/out" includes="*.jar"/>
</zip>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
我可以使用该文件来开发和测试我的应用程序,但如果我在android上部署它,它会抛出一个异常,就像它找不到my.repackaged.org.apache.logging.log4j.something引用的那样my.package.org.apache.logging.whatEver.
所以,现在我想通过使用字节码操作来消除对commons-logging的任何依赖.这已经在以前完成:http://sixlegs.com/blog/java/dependency-killer.html
但我想知道我是怎么做到的?org.apache.commons.logging.Log只有依赖项:
x$x$: java -jar jarjar-1.4.jar find jar my-org-apache-httpclient-4.3.1.jar commons-logging-1.1.3.jar
my/http/impl/execchain/ServiceUnavailableRetryExec -> org/apache/commons/logging/Log
my/http/impl/execchain/RetryExec -> org/apache/commons/logging/Log
my/http/impl/execchain/RedirectExec -> org/apache/commons/logging/Log
my/http/impl/execchain/ProtocolExec -> org/apache/commons/logging/Log
...
Run Code Online (Sandbox Code Playgroud)
我认为要走的路是,删除这些依赖项并将其替换为自己的实现,就像他在此处所做的那样 …
android httpclient jarjar apache-commons-logging androidhttpclient
我正在使用 Google 的下载库进行扩展包下载,通过“Android SDK”管理器分发。使用 Eclipse/Ant 构建,因为还没有迁移到 AS/Gradle - 但任何解决方案都会有所帮助。
在 API 23 中,Apache HTTP 消失了。我已经实施了一种解决方法来编译 Google 的 LVL。(Lvl 库和 android 棉花糖)
但是下载库要解决的问题要大得多,而且不确定我是否真的想修复 Google 的代码 - 如果需要,可能只是编写自己的代码。
有谁知道方便的“插入”(-ish)替换库或合适的解决方法?
~~
'com.google.android.vending.expansion.downloader.impl.AndroidHttpClient' 中不可用导入的编译错误
导入 org.apache.http.Header; 导入 org.apache.http.HttpEntity; 导入 org.apache.http.HttpEntityEnclosureRequest; 导入 org.apache.http.HttpException; 导入 org.apache.http.HttpHost; 导入 org.apache.http.HttpRequest; 导入 org.apache.http.HttpRequestInterceptor; 导入 org.apache.http.HttpResponse; 导入 org.apache.http.client.ClientProtocolException; 导入 org.apache.http.client.HttpClient; 导入 org.apache.http.client.ResponseHandler; 导入 org.apache.http.client.methods.HttpUriRequest; 导入 org.apache.http.client.params.HttpClientParams; 导入 org.apache.http.client.protocol.ClientContext; 导入 org.apache.http.conn.ClientConnectionManager; 导入 org.apache.http.conn.scheme.PlainSocketFactory; 导入 org.apache.http.conn.scheme.Scheme; 导入 org.apache.http.conn.scheme.SchemeRegistry; 导入 org.apache.http.conn.scheme.SocketFactory; 导入 org.apache.http.conn.ssl.SSLSocketFactory;导入 org.apache.http.entity.AbstractHttpEntity; 导入 org.apache.http.entity.ByteArrayEntity; 导入 org.apache.http.impl.client.DefaultHttpClient; 导入 org.apache.http.impl.client.RequestWrapper; 导入 …
android android-download-manager androidhttpclient android-6.0-marshmallow
我是一名iOS开发人员,开始学习Android.在Swift中,创建一个完成处理程序非常简单,但我仍然找不到用Java编写的方法.
很抱歉,如果这个问题对于StackOverflow人来说太棒了.
问题
我正在创建一个类来处理我使用Retrofit完成的所有Http请求.
我让这个功能是我的 RequestHelper.java
public static void checkEmailAvailability(String email) {
MyWebServiceAPI serviceAPI = retrofit.create(MyWebServiceAPI.class);
Call<APIResults> call = serviceAPI.checkEmailAvailability(getAuthenticationHeader(), RequestBody.create(MediaType.parse("text/plain"), email));
call.enqueue(new Callback<APIResults>() {
@Override
public void onResponse(retrofit.Response<APIResults> response, Retrofit retrofit) {
//Parse Response Json
//Get some value and place it inside an object
//ANDI WOULD LIKE RETURN SOME BOOLEAN VALUE AND SOME OTHER STRING
}
@Override
public void onFailure(Throwable t) {
//I WOULD LIKE A BOOLEAN VALUE HERE
}
});
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼它 MainActivity
RequestHelper.checkEmailAvailability("user@user.com");
Run Code Online (Sandbox Code Playgroud)
现在该函数仍然无效但我希望它在on onResponse和onFailure …
java android event-handling android-handler androidhttpclient
我有一个存储在 Xampp 服务器中的 php 脚本,并希望我的应用程序执行它来执行任务。在 Eclipse 中,服务器端的 android.txt 没有发生任何事情。以下是我的android应用程序代码
String url = "http://my.site.php?data=hello";
HttpClient client = new DefaultHttpClient();
try {
client.execute(new HttpGet(url));
} catch(IOException e) {
//do something here
}
Run Code Online (Sandbox Code Playgroud)
以下是我在服务器端的 php 代码。
$name=$_GET['data'];
$file=fopen("./android.txt","w");
fwrite($file, $name);
fclose($file);
Run Code Online (Sandbox Code Playgroud)
虽然我从 mozila 浏览器运行 php,但它运行良好。但此代码不适用于 android。
我正在尝试使用OKHttp和https://github.com/hongyangAndroid/okhttp-utils实现登录功能,但是我不知道如何JsonObject在回调中创建新的使用响应。
OkHttpUtils.post()
.url(url)
.addParams("phone", "18681873411")
.addParams("password", "18681873411")
.build()
.execute(new Callback() {
@Override
public Object parseNetworkResponse(Response response) throws Exception {
Log.i("ws","---->>parseNetworkResponse" + response.body().string());
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(response.body().string());
} catch (JSONException e) {
Log.i("ws","---->>JSONException");
e.printStackTrace();
} catch (IOException e) {
Log.i("ws","---->>IOException");
e.printStackTrace();
}
Log.i("ws","---->>111 :" );
String opCode = jsonObj.getString("OpCode");
String message = jsonObj.getString("Message");
Log.i("ws","---->>111 opCode:" + opCode);
Log.i("ws","---->>111 Message:" + message);
JSONObject result = new JSONObject();
result.put("qrcode",opCode);
result.put("message", message); …Run Code Online (Sandbox Code Playgroud) 我有问题.改造是否支持使用SNI的SSL?这是一种允许服务器根据请求的主机名返回不同SSL证书的技术,允许在共享主机方案中使用SSL.
我正在使用自签名 CA 证书进行测试环境,但面临使用 HttpClient 访问 api 的问题...请帮助我解决此问题...
W/System.err:javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。/W/System.err: 在 com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:333) 在 com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:103) 在 com. android.okhttp.Connection.connect(Connection.java:167) 在 com.android.okhttp.Connection.connectAndSetOwner(Connection.java:209) 在 com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128) 在com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:352) 在 com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:341) 在 com.android.okhttp.internal。 http.HttpEngine.sendRequest(HttpEngine.java:259) 在 com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:454) 在 com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl. java:114) 在 com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245) 在 com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218) 在 com.android .okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java) 在 .Components.HttpClient.getInternetData(HttpClient.java:135) 在 .Components.HttpClient.doInBackground(HttpClient.java:53) 在 .Components.HttpClient.doInBackground (HttpClient.java:31) 03-11 20:18:19.305 8156-8238/W/System.err: 在 android.os.AsyncTask$2.call(AsyncTask.java:295) 在 java.util.concurrent.FutureTask。在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)在java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)在java.lang.Thread运行(FutureTask.java:237) .run(Thread.java:818) 原因:java.security.cert.CertificateException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。在 com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:324) 在 com.android.org.conscrypt.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:225) 在 com.android.org.conscrypt.Platform.checkServerTrusted (Platform.java:115)在com.android.org.conscrypt.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:571)在com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(本机方法)03-11 20:18:19.306 8156-8238/ W/System.err:位于 com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:329) ... 20 更多
这是我的调用方法:
HttpClient client = new HttpClient(context, new ReturnProcess(), params_hash_map);
client.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, URL);
Run Code Online (Sandbox Code Playgroud)
基类:
import android.content.Context; …Run Code Online (Sandbox Code Playgroud) 最近我将 android 9 更新到了 android 10,但不幸的是,该应用程序有时会崩溃并出现此错误。
com.fgapps.maker E/chromium: [ERROR:cookie_manager.cc(137)] Strict Secure Cookie policy does not
allow setting a secure cookie for http://googleads.g.doubleclick.net/ for apps targeting >= R.
Please either use the 'https:' scheme for this URL or omit the 'Secure' directive in the cookie value.
Run Code Online (Sandbox Code Playgroud)
我尝试在清单中使用它
<application
android:hardwareAccelerated="false"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
....>
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
Run Code Online (Sandbox Code Playgroud)
我还添加了 build-gradle (app)
android {
useLibrary 'org.apache.http.legacy'
...}
Run Code Online (Sandbox Code Playgroud)
Site Behavior: Navigation即使我检查了所有链接都正常工作,AdMob 也会因为违规而拒绝我的应用,所以如何解决这个问题。谢谢