Created
December 12, 2022 06:06
-
-
Save log2c/ebe8a4fa320df3fa900e2f6222501db3 to your computer and use it in GitHub Desktop.
Android中使用HttpClient, GET模式下提交 multipart/form-data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.entity.mime.MultipartEntityBuilder; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.util.EntityUtils; | |
import java.io.IOException; | |
import java.net.URI; | |
public class AndroidHttpClient { | |
private static final String TAG = AndroidHttpClient.class.getSimpleName(); | |
/** | |
* 清单文件中: | |
* | |
* <application> | |
* <uses-library android:name="org.apache.http.legacy" android:required="false"/> | |
* </application> | |
* <p> | |
* Gradle 申明依赖(自行去maven下载jar包): | |
* <p> | |
* api files('libs/httpclient-4.3.6.jar') | |
* api files('libs/httpcore-4.3.3.jar') | |
* api files('libs/httpmime-4.3.6.jar') | |
* <p> | |
* defaultConfig { | |
* useLibrary 'org.apache.http.legacy' | |
* } | |
* | |
* @return 构造HttpClient | |
*/ | |
public static HttpClient getHttpClient() { | |
final DefaultHttpClient httpClient = new DefaultHttpClient(); // Android 只能使用此方法构造 HttpClient | |
// httpClient.addRequestInterceptor(new HttpClientRequestInterceptor()); | |
// httpClient.addResponseInterceptor(new HttpClientResponseInterceptor()); | |
return httpClient; | |
} | |
private static final String URL = "http://xxxxxx"; | |
/** | |
* 测试 | |
* multipart/form-data | |
* | |
* @throws IOException | |
*/ | |
private void testHttp() throws IOException { | |
HttpClient client = getHttpClient(); | |
String value = ""; | |
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); | |
entityBuilder.addTextBody("key", value); | |
HttpGetWithEntity e = new HttpGetWithEntity(); | |
e.setURI(URI.create(URL)); | |
e.setEntity(entityBuilder.build()); | |
final HttpResponse response = client.execute(e); | |
String bodyAsString = EntityUtils.toString(response.getEntity(), "UTF-8"); | |
Log.i(TAG, bodyAsString); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; | |
import java.net.URI; | |
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase { | |
public final static String METHOD_NAME = "GET"; | |
public HttpGetWithEntity() { | |
super(); | |
} | |
public HttpGetWithEntity(final URI url) { | |
super(); | |
setURI(url); | |
} | |
public HttpGetWithEntity(final String url) { | |
super(); | |
setURI(URI.create(url)); | |
} | |
@Override | |
public String getMethod() { | |
return METHOD_NAME; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment