Last active
August 29, 2015 14:09
-
-
Save tatai/c1fb27680d82d4b698e0 to your computer and use it in GitHub Desktop.
Anexo 1 - Peticiones REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String json = "{\"message\":\"Hola a todos\"}"; | |
WelcomeMessage message = (WelcomeMessage)new ObjectMapper().readValues(new JsonFactory().createParser(json), WelcomeMessage.class).next(); | |
Log.d("main", message.getMessage()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WelcomeMessage message = new WelcomeMessage(); | |
message.setMessage(“Nuevo mensaje”); | |
String output = new ObjectMapper().writeValueAsString(message); | |
Log.d(“main”, output); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AsyncHttpClient client = new AsyncHttpClient(); | |
client.post(this, "http://trivial.tatai.es/welcome.json", new StringEntity("{\"name\":\”Fran\"}"), "application/json", new WelcomeResponseHandler(this)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class WelcomeMessage { | |
private String message = null; | |
public String getMessage() { | |
return this.message; | |
} | |
@JsonProperty("message") | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.Log; | |
import android.widget.Toast; | |
import com.fasterxml.jackson.core.JsonFactory; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.loopj.android.http.AsyncHttpResponseHandler; | |
import org.apache.http.Header; | |
import java.io.IOException; | |
public class WelcomeResponseHandler extends AsyncHttpResponseHandler { | |
private Context context = null; | |
public WelcomeResponseHandler(Context context) { | |
this.context = context; | |
} | |
@Override | |
public void onSuccess(int i, Header[] headers, byte[] bytes) { | |
WelcomeMessage message = null; | |
try { | |
message = new ObjectMapper().readValues(new JsonFactory().createParser(bytes), WelcomeMessage.class).next(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
Toast.makeText(this.context, message.getMessage(), Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { | |
Log.d("WelcomeResponseHandler", "Request returned error " + i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment