Java 通过http GET从Json对象提取数据
我正在开发android应用程序,我想知道如何使用http get从Json对象获取数据(http请求url是apariy) 这是我第一次使用Json和httpRequests,所以我不知道这需要什么语法 这是我正在使用的HttpRequest类:Java 通过http GET从Json对象提取数据,java,android,json,apache,android-asynctask,Java,Android,Json,Apache,Android Asynctask,我正在开发android应用程序,我想知道如何使用http get从Json对象获取数据(http请求url是apariy) 这是我第一次使用Json和httpRequests,所以我不知道这需要什么语法 这是我正在使用的HttpRequest类: public abstract class HttpRequest extends AsyncTask<String, String, String> { private HttpClient httpClient; private
public abstract class HttpRequest extends AsyncTask<String, String, String> {
private HttpClient httpClient;
private HttpRequestBase request;
private boolean hasError = false;
private String errorMessage = null;
private boolean hasBody = false;
private int statusCode;
public HttpRequest(){
httpClient = new DefaultHttpClient();
}
/**
* This method is called from the subclasses to pass the request method used to this class
* @param request , The request class passed from the subclass
*/
void setMethod(HttpRequestBase request){
this.request = request;
}
/**
* Adds a header to the current request
* @param header , header key
* @param value , header value
*/
public void addHeader(String header,String value){
this.request.addHeader(header, value);
}
/**
* @return false if the status code was anything other than 2XX after executing the request , true otherwise
*/
public boolean hasError() {
return hasError;
}
/**
* A getter for the error message
* @return String the error message returned from the request if any
*/
public String getErrorMessage() {
return errorMessage;
}
/**
* This is the method responsible for executing the request and handling the response
* @return String , The response body , null in case of errors
*/
@Override
protected String doInBackground(String... args) {
if(hasBody){
this.request.addHeader("content-type", "application/json");
}
ResponseHandler<String> handler = new BasicResponseHandler();
HttpResponse x = null;
try{
x = httpClient.execute(this.request);
this.statusCode = x.getStatusLine().getStatusCode();
return handler.handleResponse(x);
}catch(ClientProtocolException e ){
hasError = true;
errorMessage = e.getMessage();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* A getter method for the status code
* @return int , the status code of executing the request
*/
public int getStatusCode(){
return this.statusCode;
}
/**
* A setter method to set whether the request has a body or not , used between this class and its subclasses
* @param hasBody boolean
*/
void setHasBody(boolean hasBody){
this.hasBody = hasBody;
}
}
公共抽象类HttpRequest扩展了异步任务{
私有HttpClient HttpClient;
私有HttpRequestBase请求;
私有布尔hasError=false;
私有字符串errorMessage=null;
私有布尔hasBody=false;
专用int状态码;
公共HttpRequest(){
httpClient=新的DefaultHttpClient();
}
/**
*从子类调用此方法,以将用于此类的请求方法传递给该类
*@param request,从子类传递的请求类
*/
void setMethod(HttpRequestBase请求){
this.request=请求;
}
/**
*将标头添加到当前请求
*@param头,头键
*@param值,头值
*/
public void addHeader(字符串头,字符串值){
this.request.addHeader(头,值);
}
/**
*@如果执行请求后状态代码不是2XX,则返回false,否则返回true
*/
公共布尔hasError(){
返回错误;
}
/**
*错误消息的获取程序
*@return String请求返回的错误消息(如果有)
*/
公共字符串getErrorMessage(){
返回错误消息;
}
/**
*这是负责执行请求和处理响应的方法
*@return String,响应主体,出现错误时为空
*/
@凌驾
受保护的字符串doInBackground(字符串…args){
if(hasBody){
this.request.addHeader(“内容类型”、“应用程序/json”);
}
ResponseHandler=new BasicResponseHandler();
HttpResponse x=null;
试一试{
x=httpClient.execute(this.request);
this.statusCode=x.getStatusLine().getStatusCode();
返回handler.handleResponse(x);
}捕获(客户端协议例外e){
hasrerror=true;
errorMessage=e.getMessage();
返回null;
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
}
/**
*状态代码的getter方法
*@return int,执行请求的状态码
*/
public int getStatusCode(){
返回此.statusCode;
}
/**
*用于设置请求是否有正文的setter方法,用于此类及其子类之间
*@param hasBody布尔值
*/
void setHasBody(布尔hasBody){
this.hasBody=hasBody;
}
}
我认为这篇文章可以帮助你:
如果你不明白,就告诉我 您可以使用库或编写代码来解析标记,对于Gson的使用,请检查:这很有用,但有些我不明白:标题是什么?为什么我们要使用http响应和实体?感谢在接收和解释请求消息后,服务器将使用HTTP响应消息进行响应。Response=状态行*((通用标题|响应标题|实体标题)CRLF)CRLF[消息正文]可通过HTTP消息发送或接收的实体。实体可以在某些请求和响应中找到,它们是可选的。您可以在此处找到更多详细信息: