Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- String JSON_STRING;
- private EditText editTextId;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editTextId = (EditText) findViewById(R.id.editTextId);
- }
- public void getJSON(View view){
- new BackgroundTask().execute();
- }
- class BackgroundTask extends AsyncTask<Void,Void,String> {
- String json_url;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- json_url = "http://192.168.43.126/select/select.php?id="+editTextId.getText().toString().trim();
- }
- @Override
- protected String doInBackground(Void... voids) {
- 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();
- return stringBuilder.toString().trim();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String result) {
- TextView textview=(TextView)findViewById(R.id.textview);
- textview.setText(result);
- }
- }
- }
- <?php
- if($_SERVER['REQUEST_METHOD']=='GET'){
- $id = $_GET['id'];
- define('HOST','localhost');
- define('USER','root');
- define('PASS','');
- define('DB','test');
- $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
- $sql = "SELECT * FROM colleges WHERE name='".$id."'";
- $r = mysqli_query($con,$sql);
- $result = array();
- while($res = mysqli_fetch_array($r)) {
- $result[]=array(
- "name"=>$res['name'],
- "address"=>$res['address'],
- "vc"=>$res['vicechancellor']
- );
- }
- echo json_encode(array("result"=>$result));
- mysqli_close($con);
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context="com.example.wvs.myapplication.MainActivity">
- <EditText
- android:id="@+id/editTextId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:layout_toLeftOf="@+id/buttonGet"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="json"
- android:id="@+id/b1"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="39dp"
- android:onClick="getJSON"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Text"
- android:id="@+id/textview"
- android:layout_below="@+id/b2"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="48dp"
- />
- <ListView
- android:id="@+id/listView1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="14dp" >
- </ListView>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement