Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. String JSON_STRING;
  3. private EditText editTextId;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. editTextId = (EditText) findViewById(R.id.editTextId);
  10. }
  11. public void getJSON(View view){
  12. new BackgroundTask().execute();
  13.  
  14. }
  15.  
  16. class BackgroundTask extends AsyncTask<Void,Void,String> {
  17.  
  18. String json_url;
  19.  
  20. @Override
  21. protected void onPreExecute() {
  22. super.onPreExecute();
  23. json_url = "http://192.168.43.126/select/select.php?id="+editTextId.getText().toString().trim();
  24. }
  25.  
  26. @Override
  27. protected String doInBackground(Void... voids) {
  28.  
  29.  
  30. try {
  31. URL url=new URL(json_url);
  32. HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
  33. InputStream inputStream=httpURLConnection.getInputStream();
  34. BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
  35. StringBuilder stringBuilder= new StringBuilder();
  36. while((JSON_STRING= bufferedReader.readLine())!=null){
  37. stringBuilder.append(JSON_STRING+"n");
  38. }
  39. bufferedReader.close();
  40. inputStream.close();
  41. httpURLConnection.disconnect();
  42. return stringBuilder.toString().trim();
  43. } catch (MalformedURLException e) {
  44. e.printStackTrace();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. return null;
  49. }
  50.  
  51. @Override
  52. protected void onProgressUpdate(Void... values) {
  53. super.onProgressUpdate(values);
  54. }
  55.  
  56. @Override
  57. protected void onPostExecute(String result) {
  58. TextView textview=(TextView)findViewById(R.id.textview);
  59. textview.setText(result);
  60. }
  61. }
  62.  
  63. }
  64.  
  65. <?php
  66.  
  67. if($_SERVER['REQUEST_METHOD']=='GET'){
  68.  
  69. $id = $_GET['id'];
  70.  
  71.  
  72. define('HOST','localhost');
  73. define('USER','root');
  74. define('PASS','');
  75. define('DB','test');
  76.  
  77. $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
  78.  
  79. $sql = "SELECT * FROM colleges WHERE name='".$id."'";
  80.  
  81. $r = mysqli_query($con,$sql);
  82.  
  83.  
  84. $result = array();
  85. while($res = mysqli_fetch_array($r)) {
  86.  
  87. $result[]=array(
  88. "name"=>$res['name'],
  89. "address"=>$res['address'],
  90. "vc"=>$res['vicechancellor']
  91. );
  92. }
  93. echo json_encode(array("result"=>$result));
  94.  
  95. mysqli_close($con);
  96.  
  97. }
  98.  
  99. <?xml version="1.0" encoding="utf-8"?>
  100. <RelativeLayout
  101. xmlns:android="http://schemas.android.com/apk/res/android"
  102. xmlns:tools="http://schemas.android.com/tools"
  103. android:layout_width="match_parent"
  104. android:layout_height="match_parent"
  105. android:paddingLeft="@dimen/activity_horizontal_margin"
  106. android:paddingRight="@dimen/activity_horizontal_margin"
  107. android:paddingTop="@dimen/activity_vertical_margin"
  108. android:paddingBottom="@dimen/activity_vertical_margin"
  109. tools:context="com.example.wvs.myapplication.MainActivity">
  110. <EditText
  111. android:id="@+id/editTextId"
  112. android:layout_width="wrap_content"
  113. android:layout_height="wrap_content"
  114. android:layout_weight="1"
  115. android:layout_toLeftOf="@+id/buttonGet"
  116. android:layout_alignParentLeft="true"
  117. android:layout_alignParentStart="true" />
  118.  
  119.  
  120. <Button
  121. android:layout_width="wrap_content"
  122. android:layout_height="wrap_content"
  123. android:text="json"
  124. android:id="@+id/b1"
  125. android:layout_centerHorizontal="true"
  126. android:layout_marginTop="39dp"
  127. android:onClick="getJSON"/>
  128.  
  129.  
  130.  
  131. <TextView
  132. android:layout_width="wrap_content"
  133. android:layout_height="wrap_content"
  134. android:text="New Text"
  135. android:id="@+id/textview"
  136. android:layout_below="@+id/b2"
  137. android:layout_centerHorizontal="true"
  138. android:layout_marginTop="48dp"
  139. />
  140. <ListView
  141. android:id="@+id/listView1"
  142. android:layout_width="match_parent"
  143. android:layout_height="wrap_content"
  144. android:layout_alignParentTop="true"
  145. android:layout_centerHorizontal="true"
  146. android:layout_marginTop="14dp" >
  147. </ListView>
  148. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement