Last active
November 14, 2018 11:14
-
-
Save SPurno/1717182b54a20b9a5552906d4345eb03 to your computer and use it in GitHub Desktop.
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
/* | |
* Copyright (c) 2018 Faruk Ahmed | |
* License Under MIT | |
* Not use for commercial perpose | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
private Button getApiBtn, postApiBtn; | |
private TextView resultTextView; | |
RequestQueue requestQueue; | |
private static final String TAG = MainActivity.class.getSimpleName(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
resultTextView = (TextView) findViewById(R.id.resultTextView); | |
getApiBtn = (Button) findViewById(R.id.getApiBtn); | |
postApiBtn = (Button)findViewById(R.id.postApiBtn); | |
// RequestQueue For Handle Network Request | |
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); | |
//Click Listner for GET JSONObject | |
getApiBtn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
getData(); | |
} | |
}); | |
//Click Listner for POST JSONObject | |
postApiBtn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
postData(); | |
} | |
}); | |
} | |
// Post Request For JSONObject | |
public void postData() { | |
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); | |
JSONObject object = new JSONObject(); | |
try { | |
//input your API parameters | |
object.put("parameter","value"); | |
object.put("parameter","value"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
// Enter the correct url for your api service site | |
String url = getResources().getString(R.string.url); | |
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, object, | |
new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
resultTextView.setText("String Response : "+ response.toString()); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
resultTextView.setText("Error getting response"); | |
} | |
}); | |
requestQueue.add(jsonObjectRequest); | |
} | |
// Get Request For JSONObject | |
public void getData(){ | |
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); | |
try { | |
String url = getResources().getString(R.string.url); | |
JSONObject object = new JSONObject(); | |
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
resultTextView.setText("Resposne : " + response.toString()); | |
Toast.makeText(getApplicationContext(), "I am OK !" + response.toString(), Toast.LENGTH_LONG).show(); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
requestQueue.add(jsonObjectRequest); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment