public class MainActivity extends AppCompatActivity { private Button btnChercher; private TextView tvJson; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnChercher = (Button) findViewById(R.id.btnchercher); tvJson = (TextView) findViewById(R.id.tvJson); btnChercher.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new JsonTask().execute("http://localhost:8080/membres"); } } ); } public class JsonTask extends AsyncTask<string,string,string>{ @Override protected String doInBackground(String... params) { HttpURLConnection connection=null; BufferedReader reader=null; try { URL url = new URL(params[0]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); InputStream stream = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(stream)); StringBuffer buffer = new StringBuffer(); String line =""; while ((line=reader.readLine())!=null){ buffer.append(line); } String finalJson = buffer.toString(); JSONObject parentObject = new JSONObject(); JSONArray parentArray = parentObject.getJSONArray(""); JSONObject finalObject = parentArray.getJSONObject(0); String nomMembre = finalObject.getString("nomMembre"); return nomMembre+"\n"; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } finally { if(connection!=null){ connection.disconnect(); } if(reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); tvJson.setText(result); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)