Java Kodu:
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://www.domain.com/api");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button login = (Button) findViewById(R.id.buton_login);
final EditText user = (EditText) findViewById(R.id.input_user);
final EditText pass = (EditText) findViewById(R.id.input_pass);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login.setClickable(false);
if( user.getText().toString().length() < 1 ){
Toast.makeText(MainActivity.this, "Kullanıcı adınızı girmelisiniz", Toast.LENGTH_SHORT).show();
}else if( pass.getText().toString().length() < 1 ){
Toast.makeText(MainActivity.this, "Parolanızı girmelisiniz", Toast.LENGTH_SHORT).show();
}else{
new postAction().execute();
}
login.setClickable(true);
}
});
}
private class postAction extends AsyncTask {
final TextView resultView = (TextView) findViewById(R.id.result_view);
protected void OnPreExecute(){
}
@Override
protected String doInBackground(String... strings) {
List nameValuePair = new ArrayList(2);
nameValuePair.add(new BasicNameValuePair("action","balance"));
nameValuePair.add(new BasicNameValuePair("key","12"));
try{
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e){
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
String result = response.toString();
return result;
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(String result){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Title");
builder.setMessage(result);
builder.setNegativeButton("İptal", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
//İptal butonuna basılınca yapılacaklar.Sadece kapanması isteniyorsa boş bırakılacak
}
});
builder.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Tamam butonuna basılınca yapılacaklar
}
});
builder.show();
}
}
}