package ru.steapу.client;
import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.ProgressBar;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.widget.Toast;
public class LogActivity extends Activity
{
String SERVER_URL = "сам сайт";
EditText inputLogin, inputPass;
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.log);
inputLogin = (EditText)findViewById(R.id.inputLogin);
inputPass = (EditText)findViewById(R.id.inputPass);
pb = (ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
}
public void buttonLog(View v){
if(inputLogin.getText().toString().length()<1){
Toast.makeText(LogActivity.this,"Введите ваш логин",Toast.LENGTH_LONG).show();
}
else if(inputPass.getText().toString().length()<1){
Toast.makeText(LogActivity.this,"Введите ваш пароль",Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(inputLogin.getText().toString(), inputPass.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SERVER_URL);
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("login", params[0]));
nameValuePairs.add(new BasicNameValuePair("pass", params[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
}catch(ClientProtocolException e){
Toast.makeText(getBaseContext(),"Client Protocol Exception",Toast.LENGTH_SHORT).show();
}catch(IOException e){
Toast.makeText(getBaseContext(),"IO Exception",Toast.LENGTH_SHORT).show();
}
return null;
}
protected void onPostExecute(Double result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Отправлено",Toast.LENGTH_LONG).show();
inputLogin.setText("");
inputPass.setText("");
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}
}
}