1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
public class ProgressBarAppli extends Activity implements OnClickListener, OnItemSelectedListener{
ProgressDialog myProgressDialog;
String sAccCurr ;
String chaine="Agadir";
TextView ville;
TextView population;
TextView longtitude;
TextView latitude;
String[] result;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
String myurl="";
URL url;
try {
url = new URL(myurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("ereur",e.getMessage());
}
Spinner s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
s.setOnItemSelectedListener(this);
}
private String getHttpResponse(String ville) {
String result = null;
URL url = null;
// Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " location = " + location)
try {
url = new URL("http://testandroid.hebergratuit.com/index.php?ville=\""+ville+"\"");
// Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url = " + url);
} catch (MalformedURLException e) {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine;
int lineCount = 1; // limit the lines for the example
while ((lineCount < 10) && ((inputLine = in.readLine()) != null)) {
lineCount++;
// Log.v(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " inputLine = " + inputLine);
result += "\n" + inputLine;
}
in.close();
urlConn.disconnect();
} catch (IOException e) {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
}
} else {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url NULL");
}
return result;
}
public String[] splitResult(String result){
String[] resultat =new String[4];
resultat=result.split(":");
return resultat;
}
@Override
public void onClick(View arg0) {
}
@Override
public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
long arg3) {
ville=(TextView)findViewById(R.id.ville);
population=(TextView)findViewById(R.id.population);
longtitude=(TextView)findViewById(R.id.longtitude);
latitude=(TextView)findViewById(R.id.latitude);
Spinner AccCurr = (Spinner) findViewById(R.id.spinner);
sAccCurr = AccCurr.getSelectedItem().toString();
LoadTask load=new LoadTask();
load.execute(result);
}
class LoadTask extends AsyncTask<String ,String , String> {
@Override
protected String doInBackground(String... urls) {
chaine=getHttpResponse(sAccCurr);
result=splitResult(chaine);
ville.setText(sAccCurr);
population.setText(result[1]);
longtitude.setText(result[2]);
latitude.setText(result[3]);
return chaine;
}
protected void onProgressUpdate(Void inutilise) {
myProgressDialog = ProgressDialog.show(ProgressBarAppli.this,
"Please wait...", "Doing Extreme Calculations...", true);
}
protected void onPostExecute(Void inutilise) {
myProgressDialog.dismiss();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
} |
Partager