Skip to content

Instantly share code, notes, and snippets.

@AswinpAshok
Last active June 6, 2017 11:54
Show Gist options
  • Save AswinpAshok/8dbb7ae896a2e04cf6e4916e9b914d84 to your computer and use it in GitHub Desktop.
Save AswinpAshok/8dbb7ae896a2e04cf6e4916e9b914d84 to your computer and use it in GitHub Desktop.
Send rerquest to server (with optional json data to post), and Listen for ServerResponse using Interface. Uses okhttp3 (Android)
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONException;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by ASWIN on 5/3/2017.
*/
public class NetworkCall extends AsyncTask<String, String, String> {
String TAG="NETWORK_CALL";
ServerResponseListner mListener;
String type,URL,REQ_JSON;
public NetworkCall(String type) {
this.type = type;
}
public void setListener(ServerResponseListner listener){
mListener = listener;
}
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String json = params[0];
String url=params[1];
this.URL=url;
this.REQ_JSON=json;
String jsonResponse=null;
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
jsonResponse= response.body().string();
} catch (IOException e) {
e.printStackTrace();
mListener.onNetworkException(e);
}
return jsonResponse;
}
@Override
protected void onPostExecute(String s) {
try {
s = s.trim();
Log.d(TAG, "\n\t*****************************************" +
"\n\t REQUEST TYPE : "+ type
+"\n\t URL : "+URL
+"\n\t REQUEST DATA : "+REQ_JSON
+"\n\t SERVER RESPONSE : "+s+"\n" +
"\t*************************************************" +
"\n " );
String[] response=new String[]{type,s};
try {
mListener.onResponse(response);
} catch (JSONException e) {
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
import org.json.JSONException;
/**
* Created by ASWIN on 5/6/2017.
*/
public interface ServerResponseListner {
void onResponse(String[] response) throws JSONException;
void onNetworkException(Exception e);
}
/**
* Created by ASWIN on 5/6/2017.
*/
public class UsageExample extends AppCompatActivity implements ServerResponseListner{
private static final String TAG="RECIEVING_DATA";
private static final String MY_NETWORK_CALL_1="myFirstNetworkCall";
private static final String MY_NETWORK_CALL_2="mySecondNetworkCall";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_student);
//*************************FIRST NETWORK CALL********************************
NetworkCall networkCall=new NetworkCall(MY_NETWORK_CALL_1);
networkCall.setListener(UsageExample.this);
networkCall.execute(CreatePOST_JSON,"https://posttestserver.com/post.php");
//****************************************************************************
//*************************SECOND NETWORK CALL********************************
NetworkCall networkCall2=new NetworkCall(MY_NETWORK_CALL_2);
networkCall2.setListener(UsageExample.this);
//networkcall without any json data to post
networkCall2.execute("","https://posttestserver.com/post.php");
//****************************************************************************
}
@Override
public void onResponse(String[] response) throws JSONException {
if(response[0].equals(MY_NETWORK_CALL_1)){
//This is the response of the first NetworkCall
Log.d(TAG,response[1]);
}else
if(response[0].equals(MY_NETWORK_CALL_2)){
//This is the response of the second NetworkCall
Log.d(TAG,response[1]);
}
}
@Override
public void onNetworkException(Exception e) {
Snackbar snackbar=Snackbar.make(layout,"Please check your Internet connection",Snackbar.LENGTH_LONG);
snackbar.show();
}
private String CreatePOST_JSON() throws JSONException {
JSONObject object=new JSONObject();
object.put("POST_DATA","This is the data to be sent to the server");
return object.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment