Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void getLogin() {
- final String mURL = "https://somesite.com/api/login";
- EditText username = (EditText) findViewById(R.id.username);
- EditText password = (EditText) findViewById(R.id.password);
- // Post params to be sent to the server
- HashMap<String, String> params = new HashMap<String, String>();
- params.put("username", username.getText().toString());
- params.put("password", password.getText().toString());
- JsonObjectRequest req = new JsonObjectRequest(mURL, new JSONObject(
- params), new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- JSONObject obj = response
- .getJSONObject("some_json_obj");
- Log.w("myApp",
- "status code..." + obj.getString("name"));
- // VolleyLog.v("Response:%n %s", response.toString(4));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.w("error in response", "Error: " + error.getMessage());
- }
- });
- // add the request object to the queue to be executed
- AppController.getInstance().addToRequestQueue(req);
- }
- /* import com.android.volley.toolbox.HttpHeaderParser; */
- public void onErrorResponse(VolleyError error) {
- // As of f605da3 the following should work
- NetworkResponse response = error.networkResponse;
- if (error instanceof ServerError && response != null) {
- try {
- String res = new String(response.data,
- HttpHeaderParser.parseCharset(response.headers, "utf-8"));
- // Now you can use any deserializer to make sense of data
- JSONObject obj = new JSONObject(res);
- } catch (UnsupportedEncodingException e1) {
- // Couldn't properly decode data to string
- e1.printStackTrace();
- } catch (JSONException e2) {
- // returned data is not JSONObject?
- e2.printStackTrace();
- }
- }
- }
- @Override
- public Map<String, String> getHeaders() throws AuthFailureError {
- HashMap<String, String> headers = new HashMap<String, String>();
- headers.put("Content-Type", "application/json; charset=utf-8");
- return headers;
- }
- StringRequest sr = new StringRequest(type,url, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- // valid response
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- // error
- }
- }){
- @Override
- protected Map<String,String> getParams(){
- Map<String,String> params = new HashMap<String, String>();
- params.put("username", username);
- params.put("password", password);
- params.put("grant_type", "password");
- return params;
- }
- @Override
- public Map<String, String> getHeaders() throws AuthFailureError {
- Map<String,String> params = new HashMap<String, String>();
- // Removed this line if you dont need it or Use application/json
- // params.put("Content-Type", "application/x-www-form-urlencoded");
- return params;
- }
- protected void getLogin() {
- EditText username = (EditText) findViewById(R.id.username);
- EditText password = (EditText) findViewById(R.id.password);
- RequestParams params = new RequestParams();
- params.put("username", username.getText().toString());
- params.put("password", password.getText().toString());
- RestClient.post(getHost() + "api/v1/auth/login", params,
- new JsonHttpResponseHandler() {
- @Override
- public void onSuccess(int statusCode, Header[] headers,
- JSONObject response) {
- try {
- //process JSONObject obj
- Log.w("myapp","success status code..." + statusCode);
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void onFailure(int statusCode, Header[] headers,
- Throwable throwable, JSONObject errorResponse) {
- Log.w("myapp", "failure status code..." + statusCode);
- try {
- //process JSONObject obj
- Log.w("myapp", "error ..." + errorResponse.getString("message").toString());
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement