Created
June 25, 2016 18:21
-
-
Save kostovtd/12e7ced2fe1593d0f179ffdd551b9fea 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
/** | |
* Created by todor.kostov on 6/25/2016. | |
*/ | |
public interface AsyncResponse { | |
void processFinish(String 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
import android.os.AsyncTask; | |
import com.squareup.okhttp.OkHttpClient; | |
import com.squareup.okhttp.Request; | |
import com.squareup.okhttp.Response; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
public class CommonData extends AsyncTask<String, Void, String> { | |
public AsyncResponse delegate = null; | |
private static final String URL = "https://httpbin.org/get"; | |
@Override | |
protected String doInBackground(String... params) { | |
OkHttpClient client = new OkHttpClient(); | |
client.setConnectTimeout(120, TimeUnit.SECONDS); | |
client.setWriteTimeout(120, TimeUnit.SECONDS); | |
client.setReadTimeout(120, TimeUnit.SECONDS); | |
Request request = new Request.Builder() | |
.url(URL) | |
.build(); | |
try { | |
Response response = client.newCall(request).execute(); | |
if(response.isSuccessful()) { | |
return response.body().string(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
delegate.processFinish(result); | |
} | |
} |
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.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
/** | |
* Created by todor.kostov on 6/25/2016. | |
*/ | |
public class MainActivity extends Activity implements AsyncResponse{ | |
Button b; | |
CommonData commonData; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.test_activity); | |
b = (Button) findViewById(R.id.btnProceed); | |
commonData = new CommonData(); | |
commonData.delegate = this; | |
b.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
commonData.execute(); | |
} | |
}); | |
} | |
@Override | |
public void processFinish(String output) { | |
Log.e("MainActivity", "Heelloooooo"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, what dependencies do I need in my build.gradle to use AsyncResponse?