Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. private String[] resultArray;
  2.  
  3. private EditText searchBox;
  4.  
  5. private Button searchBtn;
  6.  
  7. private String search_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
  8.  
  9. private String search_query;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState)
  13. {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_my);
  16.  
  17. listViewGoogle = (ListView)findViewById(R.id.listviewgoogle);
  18.  
  19. searchBox = (EditText)findViewById(R.id.searchBox);
  20. searchBtn = (Button)findViewById(R.id.searchBtn);
  21.  
  22. searchBtn.setOnClickListener(new View.OnClickListener()
  23. {
  24. @Override
  25. public void onClick(View v)
  26. {
  27. new JsonSearchTask().execute();
  28.  
  29. try
  30. {
  31. search_query = search_url + searchBox.getText().toString();
  32.  
  33. new JsonSearchTask().execute();
  34.  
  35. ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listviewgoogle, resultArray);
  36. listViewGoogle.setAdapter(adapter);
  37.  
  38. }
  39. catch (Exception e)
  40. {
  41. // TODO: handle exception
  42. }
  43. }
  44. });
  45. }
  46.  
  47. private class JsonSearchTask extends AsyncTask<Void, Void, Void>
  48. {
  49. @Override
  50. protected Void doInBackground(Void... arg0)
  51. {
  52. try
  53. {
  54. parseResult(sendQuery(search_query));
  55. }
  56. catch (JSONException e)
  57. {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. }
  61. catch (IOException e)
  62. {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. }
  66.  
  67. return null;
  68. }
  69. }
  70.  
  71. private String sendQuery(String query) throws IOException
  72. {
  73. String queryResult = "";
  74.  
  75. URL searchURL = new URL(query);
  76.  
  77. HttpURLConnection httpURLConnection = (HttpURLConnection) searchURL.openConnection();
  78.  
  79. if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
  80. {
  81. InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
  82. BufferedReader bufferedReader = new BufferedReader(inputStreamReader, 8192);
  83.  
  84. String line = null;
  85.  
  86. while ((line = bufferedReader.readLine()) != null)
  87. {
  88. queryResult += line;
  89. }
  90.  
  91. bufferedReader.close();
  92. }
  93.  
  94. return queryResult;
  95. }
  96.  
  97. private void parseResult(String json) throws JSONException
  98. {
  99. String parsedResult = "";
  100.  
  101. JSONObject jsonObject = new JSONObject(json);
  102.  
  103. JSONObject jsonObject_responseData = jsonObject.getJSONObject("responseData");
  104.  
  105. JSONArray jsonArray_results = jsonObject_responseData.getJSONArray("results");
  106.  
  107. resultArray = new String[jsonArray_results.length()];
  108.  
  109. for(int i = 0; i < jsonArray_results.length(); i++)
  110. {
  111. JSONObject jsonObject_i = jsonArray_results.getJSONObject(i);
  112. parsedResult = "title: " + jsonObject_i.getString("title") + "n";
  113. parsedResult += "url: " + jsonObject_i.getString("url") + "n";
  114. //parsedResult += "n";
  115. resultArray[i] = parsedResult;
  116. }
  117. }
  118.  
  119. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  120. android:id="@+id/label"
  121. android:layout_width="fill_parent"
  122. android:layout_height="fill_parent"
  123. android:padding="10dip"
  124. android:textSize="16dip"
  125. android:textStyle="bold"
  126. android:gravity="center" >
  127. </TextView>
  128.  
  129. <LinearLayout
  130. xmlns:android="http://schemas.android.com/apk/res/android"
  131. xmlns:tools="http://schemas.android.com/tools"
  132. android:layout_width="fill_parent"
  133. android:layout_height="fill_parent"
  134. android:orientation="horizontal"
  135. android:id="@+id/parentOuter" >
  136.  
  137. <LinearLayout
  138. android:layout_width="match_parent"
  139. android:layout_height="match_parent"
  140. android:orientation="vertical" >
  141.  
  142. <EditText
  143. android:layout_width="fill_parent"
  144. android:layout_height="match_parent"
  145. android:hint="type to search..."
  146. android:layout_weight="2"
  147. android:id="@+id/searchBox" />
  148.  
  149. <Button
  150. android:layout_width="match_parent"
  151. android:layout_height="match_parent"
  152. android:text="Search Me!"
  153. android:layout_weight="2"
  154. android:id="@+id/searchBtn" />
  155.  
  156. <ListView
  157. android:layout_width="match_parent"
  158. android:layout_height="match_parent"
  159. android:id="@+id/listviewgoogle"
  160. android:layout_weight="1" />
  161.  
  162. </LinearLayout>
  163.  
  164. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement