适用于我的 PC 的 Android HttpClient
我需要在 org.apache.httpclient 之上为我的 android 应用程序开发一个帮助程序类。以下代码适用于 android,但不适用于我的 PC 中的 JavaSE 1.6。
问题是:我可以在 Android 和 PC 上使用 org.apache.httpclient 吗?如果我不能:你的 http 客户端库有什么建议? 我想开发一个辅助类并使用它。
下面的代码在 android 中运行良好,但某些类无法在 Java SE 1.6 上解析。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class HTTPClient {
public static void connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
}
} catch (Exception e) {}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
I need to develop an helper class on top of org.apache.httpclient for my android application. Following code works fine for android, but not for JavaSE 1.6 in my PC.
Question is: Can I use org.apache.httpclient both for android and for PC? If I cant: what is your http client library advise?
I want to develop one helper class and use it at all.
Here is the code that works fine in android but some classes cannot be resolved for on Java SE 1.6.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class HTTPClient {
public static void connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
}
} catch (Exception e) {}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以,您应该避免将客户端帮助程序类命名为与已定义的类相同的名称。尽管 Java 区分大小写,但它会在您最意想不到的时候让您感到困惑。它甚至可能因造成这个问题而感到内疚。为什么不将其称为 HttpClientHelper,因为这才是它的真正含义。
下面是 HttpClient http://hc.apache.org/httpclient-3 的示例。 x/tutorial.html
Yes you can, you should avoid naming your client helper class as the same thing as an already defined class. Although Java is case sensitive, it is confusing will trip you up when you least expect it. It may even be guilty for causing this problem. Why not call it HttpClientHelper as that is what it truly is.
Here's an example of HttpClient http://hc.apache.org/httpclient-3.x/tutorial.html