有人知道,当使用HTTP URL作为源时,Android的MediaPlayer类的默认超时是多少?就我搜索而言,不可能设置此超时(与HttpURLConnection不同),并且只通过实现MediaPlayer.OnErrorListener接口获取有关它的信息,但是任何人都会在默认超时之后发送对此的调用方法(MediaPlayer.OnErrorListener.onError())(还有"什么"和"额外"的API前级别17)?
android android-networking android-mediaplayer androidhttpclient
我试图做出HttpPost与multiPart/form-data通过我的Android应用程序.我有一个邮递员测试,正在使用我的api和postman中的请求预览,看起来像这样:
POST /api/0.1/content/upload HTTP/1.1
Host: 54.221.194.167
X-AUTHORIZATION: 166e649911ff424eb14446cf398bd7d6
Cache-Control: no-cache
Postman-Token: 2412eba9-f72d-6f3b-b124-6070b5b26644
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"
{"mime_type":"image/jpeg","title":"IMG_20140131_111622"}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"; filename="addedaslib.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Run Code Online (Sandbox Code Playgroud)
我试图使用我的android HttpPost使用multipart/form-data复制它,但它似乎没有工作.有没有办法"预览"我的请求,看看它实际上是如何发布到api?我究竟做错了什么?我的代码:
public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) {
client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
HttpResponse response = null;
MultipartEntity mpe = new MultipartEntity();
try {
Log.v("API", "URL:"+url);
request.setHeader("Content-Type", "multipart/form-data");
request.addHeader("X-AUTHORIZATION",token);
request.addHeader("Cache-Control", "no-cache");
DRPContentForUpload content = new DRPContentForUpload(file);
String jsonObject = DRPJSONConverter.toJson(content);
FormBodyPart part1= new FormBodyPart("file01", …Run Code Online (Sandbox Code Playgroud) 我想知道,AndroidHttpClient线程是否安全,因为文档中没有提到这一点.意味着,单个实例AndroidHttpClient可以在多个线程之间共享.
我有这个代码:
HttpPut put = new HttpPut(url);
try {
put.setEntity(new StringEntity(body, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
// That would really not be good
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
在已知支持该编码的平台上.
永远不会提出异常.我永远不会做任何事情.
代码存在的事实仍然表明它可能会发生,并且其余代码可能以不可靠的状态执行.但它永远不会.或者如果是这样,优雅的网络连接回退是我的最后一个问题.
所以我有这个丑陋无用的尝试catch块.我该怎么办?
(在这个特定情况下,如果我想使用它,没有太多其他选择StringEntity.String.getBytes例如,有一堆方法可以接受一个Charset对象,例如,避免需要捕获异常,但不是StringEntity)
Apache已弃用DefaultHttpClient,但似乎Android并非如此,另请参阅此处不推荐使用Java HttpClient - 它有多难?
输入
org.apache.httpcomponents:httpclient:4.3.5
Run Code Online (Sandbox Code Playgroud)
代替
new DefaultHttpClient();
Run Code Online (Sandbox Code Playgroud)
我现在用
HttpClient httpClient = HttpClientBuilder.create().build();
Run Code Online (Sandbox Code Playgroud)
创建一个http客户端.这在Java项目中工作正常,但在Android项目中使用时,缺少以下导入
import org.apache.http.impl.client.HttpClientBuilder;
Run Code Online (Sandbox Code Playgroud)
而
HttpClient sendClient = new DefaultHttpClient();
Run Code Online (Sandbox Code Playgroud)
未在Android中标记为已弃用且编译良好.
我不想再次添加Apache httpclient(如果我这样做,Android Studio无论如何都会排除它).
Android文档在这里说http://developer.android.com/reference/android/net/http/AndroidHttpClient.html#newInstance(java.lang.String)来使用
AndroidHttpClient.new Instance(string)
Run Code Online (Sandbox Code Playgroud)
"获得具有合理默认值的http客户端".
有谁知道在Android上创建HttpClient的正确方法是什么,以及userAgent字符串是什么!?
我正在尝试使用本教程学习XML解析,但有些类没有导入.这是代码:
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(uri);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
Run Code Online (Sandbox Code Playgroud)
这些类没有导入:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.DefaultHttpClient;
Run Code Online (Sandbox Code Playgroud)
我的Gradle属性:
android {
compileSdkVersion 23
buildToolsVersion "21.1.2"
defaultConfig {
applicationId …Run Code Online (Sandbox Code Playgroud) 我的MainActivity中的以下代码在重写的doInBackground()方法中失败并出现致命异常:
public class MainActivity extends ActionBarActivity {
. . .
public void onFetchBtnClicked(View v){
if(v.getId() == R.id.FetchBtn){
Toast.makeText(getApplicationContext(), "You mashed the button, dude.", Toast.LENGTH_SHORT).show();
new NetworkTask().execute();
}
}
public static class NetworkTask extends AsyncTask<String, Void, HttpResponse> {
@Override
protected HttpResponse doInBackground(String... params) {
String link = params[0];
HttpGet request = new HttpGet("http://localhost:28642/api/deliveries/Count");
AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
try {
return client.execute(request);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
client.close();
}
}
@Override
protected void onPostExecute(HttpResponse result) { …Run Code Online (Sandbox Code Playgroud) java android android-asynctask androidhttpclient android-studio
我需要使用Android的HttpClient上传文件.不幸的是,Android不包含MultipartEntity,所以我必须使用Apache的httpmime库.
我不想在Android的HttpClient和官方的HttpClient之间产生可能的冲突.此外,我不想引入比我更多的外部库,因为我需要保持我的方法数量低.
什么版本的httpmime可以与Android的HttpClient一起使用?我见过推荐httpmime的所有问题也建议使用Apache官方的HttpClient或HttpCore.
(我知道我应该使用Retrofit,但我必须使用Android的HttpClient与其他库兼容.)
我想从我的android代码上传几个文件,以便在$ _FILES数组中可用.
服务器正在将文件作为数组进行查找.服务器中的php代码看起来像
for ($i=0;$i<$NumFiles;$i++){
...
$filename = $_FILES["fileset"]["name"][$i];
$filetype = $_FILES["fileset"]["type"][$i];
$file = curl_image_create($filePath,$filetype,$filename);
...
}
Run Code Online (Sandbox Code Playgroud)
我有文件[](fileSet)加载的文件为我的Android代码
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(this.apiURL);
MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();
StringBody numFilesBody = new StringBody("" + fileSet.length, ContentType.TEXT_PLAIN);
mpEntity.addPart("NumFiles", numFilesBody);
for (int i = 0; i < fileSet.length; i++) {
//What should I be doing here to get the $_FILES set up correctly
}
request.setEntity(mpEntity.build());
HttpResponse response = client.execute(request); …Run Code Online (Sandbox Code Playgroud) 今天我更新了android SDK API 23.
一旦我将项目更改为针对Android SDK API 23,我就开始在eclipse中收到关于Apache的客户端和AndroidHttpClient API 的错误.API 23中不再能够找到Apache库API,但仍然可以使用API 22及更低版本继续正常工作.
有人可以建议这些错误背后的问题是什么?
项目中还有一个错误
谢谢.
android android-compatibility androidhttpclient android-6.0-marshmallow