Java Android工作HttpRequest类
我正在寻找一个可用的Java Android工作HttpRequest类,java,android,Java,Android,我正在寻找一个可用的HtttpRequest类,这样我就可以这样做: String response = Request.get("http://google.com"); 我已经写了一个类,但它在安卓3+上不起作用,但在2.3上起作用 public class WebRequest { public String get(String url){ HttpClient httpclient = new DefaultHttpClient(); try { Ht
HtttpRequest
类,这样我就可以这样做:
String response = Request.get("http://google.com");
我已经写了一个类,但它在安卓3+上不起作用,但在2.3上起作用
public class WebRequest {
public String get(String url){
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
return responseBody;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
公共类WebRequest{
公共字符串获取(字符串url){
HttpClient HttpClient=新的DefaultHttpClient();
试一试{
HttpGet HttpGet=新的HttpGet(url);
//创建一个响应处理程序
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=httpclient.execute(httpget,responseHandler);
返回响应体;
}捕获(客户端协议例外e){
e、 printStackTrace();
返回null;
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}最后{
//当不再需要HttpClient实例时,
//关闭连接管理器以确保
//立即释放所有系统资源
httpclient.getConnectionManager().shutdown();
}
}
请帮忙!!!你可以看看这个答案: 在那里,他们使用以下两种方法:
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
Log.i("Praeda",response.getStatusLine().toString());
// 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();
}
更新
使用异步任务:
更新2-简单版本
它是否在2.3中工作?如果是,请检查错误是否为strictmode策略如果是,请将其转移到异步任务它可能与编码有关,请尝试向其传递utf-8编码的URL。请显示不工作的代码。URL是utf-8编码的。它在2.3中工作。您能将其转移到异步任务吗?我不知道如何才能重新执行此操作直接返回响应…我认为您必须在Android 3+上使用AsyncTask。也许这就是您要寻找的@user1849921?是的,但我如何直接返回响应,以便在开始执行的地方继续执行?就像回答中提到的:
YourClassExtendingJSONTask任务=新建YourClassExtendingJSONTask();task.execute(url);
这意味着您必须创建另一个类(例如:YourClassExtendingJSONTask.class)并扩展JSONTask类。此外,您必须在YourClassExtendingJSONTask.class中实现自己的customMethod(JSONObject json)
方法。是的,但我需要作为字符串的响应
public class XmlTask extends AsyncTask<String, Void, String>{
public String doInBackground(String... urls){
String url = urls[0];
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
Log.i("Praeda",response.getStatusLine().toString());
// 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();
}
return xml;
}
}
public void onPostExecute(String xml){
// Your XML parsing statement here
}
}
String result = new XmlTask().execute("http://google.com");