0

I am working on android app and I want to know how to get data from Json object by using http GET the (the http request url is APIary)

It's my first time to use Json and httpRequests so I don't know the syntax needed for this

That's my HttpRequest class I'm using :

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;
}
}
vzamanillo
  • 9,905
  • 1
  • 36
  • 56
user3100088
  • 335
  • 3
  • 8
  • 20
  • possible duplicate of [Displaying data through JSON httpGet Android](http://stackoverflow.com/questions/22698431/displaying-data-through-json-httpget-android) – Selvin Mar 28 '14 at 09:53
  • you can use a library or go about it with writing the code to parse tags, for use of Gson, check:http://stackoverflow.com/questions/21480634/unable-to-loop-through-dynamic-json-string-recursively-in-android/21480997#21480997 – Pararth Mar 28 '14 at 10:10

1 Answers1

1

I think this post can help you :

How to parse JSON in Android

Tell me if don't understand !

Community
  • 1
  • 1
JossVAMOS
  • 300
  • 1
  • 5
  • 20
  • It's useful but some I couldn't understand : What is the header ? and why do we use http response and entity ? Thanks – user3100088 Mar 28 '14 at 10:10
  • After receiving and interpreting a request message, a server responds with an HTTP response message. Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ] – JossVAMOS Mar 28 '14 at 10:15
  • An entity that can be sent or received with an HTTP message. Entities can be found in some requests and in responses, where they are optional. – JossVAMOS Mar 28 '14 at 10:16
  • You can find more details here : http://hc.apache.org/httpcomponents-core-4.3.x/httpcore/apidocs/org/apache/http/HttpResponse.html – JossVAMOS Mar 28 '14 at 10:16