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
| public class MainActivity extends AppCompatActivity {
String JSON_STRING;
String json_string;
String json_Parsing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new BackgroundTask().execute();
TextView textView = (TextView) findViewById(R.id.textView2);
textView.setText(json_Parsing2);
}
public void getJSON(View view) {
new BackgroundTask().execute();
}
public class BackgroundTask extends AsyncTask<Void, Void, String> {
String json_url;
String json_Parsing2=null;
@Override
protected void onPreExecute() {
json_url = "http://192.168.0.16/projet/php/fichier.php";
}
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_STRING = bufferedReader.readLine()) != null) {
stringBuilder.append(JSON_STRING + "\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
json_Parsing2=stringBuilder.toString();
return stringBuilder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json_Parsing2;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
//TextView textView = (TextView) findViewById(R.id.textView2);
//textView.setText(result);
json_Parsing=result;
}
}
/* public void parseJSON(View view) {
if(json_string==null)
{
Toast.makeText(getApplicationContext(),"First get json",Toast.LENGTH_LONG).show();
}
else {
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("json_data",json_string);
startActivity(intent);
}
}*/
/*public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.warning:
Intent intent = new Intent(MainActivity.this, popUp.class);
startActivity(intent);
return true;
case R.id.localiser:
return true;
case R.id.station:
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//ajoute les entrées de menu_test à l'ActionBar
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}*/
} |
Partager