正确的方法,使在Android的一个HTTP请求 [英] Proper way to make a HTTP request in Android
问题描述
这是使一个HTTP连接的最佳方式。我的意思是使用代理等。现在,我使用这个:
Which is the best way to make a HTTP connection. I mean using proxies and so on. Now I'm using this one:
StringBuilder entity = new StringBuilder();
entity.append("request body");
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(null);
String proxyHost = android.net.Proxy.getDefaultHost();
int proxyPort = android.net.Proxy.getDefaultPort();
if (proxyHost != null && proxyPort > 0) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
ConnRouteParams.setDefaultProxy(httpClient.getParams(), proxy);
}
HttpPost httpPost = new HttpPost("https://w.qiwi.ru/term2/xmlutf.jsp");
httpPost.setEntity(new StringEntity(entity.toString(), "UTF-8"));
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 15000);
HttpConnectionParams.setSoTimeout(params, 30000);
httpPost.setParams(params);
HttpResponse httpResponse = httpClient.execute(httpPost);
int responseCode = httpResponse.getStatusLine().getStatusCode();
if (responseCode == HttpStatus.SC_OK) {
// parsing response
}
我真的不知道这是正常的,因为我的一个客户告诉我,他有一个IllegalArgumentException在他的APN设置设置代理服务器之后。
I'm not really sure if that's ok, because one of my clients tells me he has an IllegalArgumentException right after setting proxy in his APN settings.
推荐答案
使用一种方法称为executeRequest这使得在主机API_REST_HOST实际调用,这种方式(API_REST_HOST能像api.flickr.com一个值Flickr的REST API的HTTP和端口被添加)
Use one method called executeRequest which makes the actual call to the host API_REST_HOST, this way (API_REST_HOST can be a value like "api.flickr.com" for flickr's rest api. The HTTP and the port get added)
private void executeRequest(HttpGet get, ResponseHandler handler) throws IOException {
HttpEntity entity = null;
HttpHost host = new HttpHost(API_REST_HOST, 80, "http");
try {
final HttpResponse response = mClient.execute(host, get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
final InputStream in = entity.getContent();
handler.handleResponse(in);
}
} catch (ConnectTimeoutException e) {
throw new ConnectTimeoutException();
} catch (ClientProtocolException e) {
throw new ClientProtocolException();
} catch (IOException e) {
e.printStackTrace();
throw new IOException();
}
finally {
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
调用此API从这里是这样的:
Call this API from here this way:
final HttpGet get = new HttpGet(uri.build().toString());
executeRequest(get, new ResponseHandler() {
public void handleResponse(InputStream in) throws IOException {
parseResponse(in, new ResponseParser() {
public void parseResponse(XmlPullParser parser)
throws XmlPullParserException, IOException {
parseToken(parser, token, userId);
}
});
}
});
如果你的URI是这样的构造:
Where your uri is constructed like this:
final Uri.Builder builder = new Uri.Builder();
builder.path(ANY_PATH_AHEAD_OF_THE_BASE_URL_IF_REQD);
builder.appendQueryParameter(PARAM_KEY, PARAM_VALUE);
您mClient被声明为一个类级别的变量这样
Your mClient is declared as a class level variable this way
private HttpClient mClient;
和最后的parseResponse可以这样做(说你要解析的XML数据)
and finally your parseResponse can be done in this way(say you want to parse XML data)
private void parseResponse(InputStream in, ResponseParser responseParser) throws IOException {
final XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(new InputStreamReader(in));
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
String name = parser.getName();
if (RESPONSE_TAG_RSP.equals(name)) {
final String value = parser.getAttributeValue(null, RESPONSE_ATTR_STAT);
if (!RESPONSE_STATUS_OK.equals(value)) {
throw new IOException("Wrong status: " + value);
}
}
responseParser.parseResponse(parser);
} catch (XmlPullParserException e) {
final IOException ioe = new IOException("Could not parse the response");
ioe.initCause(e);
throw ioe;
}
}
这code负责所有可能的异常,并展示了如何正确地解析响应即将从输入流出来的HTTP连接。
This code takes care of all of the possible exceptions and shows how to properly parse response coming from an input stream out of a HTTP connection.
正如你已经知道的请确保您使用一个单独的线程,而不是在UI线程。 就是这样:)
As you already know please make sure you use this in a separate thread and not in the UI thread. That's it :)
这篇关于正确的方法,使在Android的一个HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!