RestClient c = new RestClient("http://www.myserver.com/json.php"); c.AddParam("my_field_value", 1); try { c.Execute(RequestMethod.POST); // Here you can parse the JSON with an instance of Gson class. Gson jsonObject = new Gson(); String jsonOutput = jsonObject.toJson(c.getResponse()); // Do whatever you need with the data. } catch (Exception e) { e.printStackTrace(); }
public String communicateWithServer(String xmlInput) { String response = null; String webServiceUrl = "your-server-url"; RestClient client = new RestClient(webServiceUrl); client.setJSONString(xmlInput); client.addHeader("Content-Type", "text/xml"); try { client.execute(RequestMethod.POST); if (client.getResponseCode() != 200) { // handle error here } response = client.getResponse(); } catch (Exception e) { e.printStackTrace(); } return response; }
String webServiceUrl = "your-service-url"; RestClient client = new RestClient(webServiceUrl); String serverResponse = ""; String inputJsonString = "{" + " \"grant_type\" : \"password\"," + " \"username\" : \"perry.marange@zmod.co\"," + " \"password\" : \"password\"" + "}"; client.setJSONString(inputJsonString); client.addHeader("Content-Type", "appication/json"); // if required try { client.execute(RequestMethod.POST); if (client.getResponseCode() != 200) { // response error } else { // the response from server serverResponse = client.getResponse(); } } catch (Exception e) { // handle error }
public class getDatafromserver extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { } @Override protected String doInBackground(String... params) { final RestClient client = new RestClient("http://xxx.appspot.com/client"); client.AddParam("userName", "user"); client.Execute(RequestMethod.GET); String s = client.getResponse(); return s ; } @Override protected void onPostExecute(String result) { String[] items = result.split(";"); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items); _listView.setAdapter(adapter); } }
public class RemoteDBAdapter { public String register(String email, String password) throws Exception { RestClient c = new RestClient("http://myurl/Account/Register"); c.AddHeader("Accept", "application/json"); c.AddHeader("Content-type", "application/json"); c.AddParam("Email", email); c.AddParam("Password", password); c.Execute(RequestMethod.POST); JSONObject key = new JSONObject(c.getResponse()); return key.getString("status"); } }
@Override protected Void doInBackground(Void... voids) { try { RestClient client = new RestClient("your url goes here"); client.AddParam("param1", "value1")); client.AddParam("param2", "value2")); client.AddHeader("Headername","Value"); client.Execute(RequestMethod.POST); response = client.getResponse(); }catch (Exception e) { e.printStackTrace(); } return null; }
RestClient c = new RestClient("http://somewebsite.com/register"); c.AddHeader("Accept", "application/json"); c.AddHeader("Content-type", "application/json"); c.AddParam("UserName", user); c.AddParam("Password", password); c.Execute(RequestMethod.POST); JSONObject userKey = new JSONObject(c.getResponse());