Build search request

This commit is contained in:
Tim Bentley 2015-11-21 16:50:05 +00:00
parent 663bf6f55e
commit 51a9a216d9
3 changed files with 57 additions and 19 deletions

View File

@ -45,6 +45,20 @@ public class JsonHelpers {
}
}
public static String createSearchJSON(String key, String value) throws JSONHandlerException {
try {
String responseJSON;
responseJSON = new JSONStringer().object().key(key).value(value)
.endObject().toString();
responseJSON = URLEncoder.encode(responseJSON, "UTF-8");
return responseJSON;
} catch (JSONException e) {
throw new JSONHandlerException(e);
} catch (UnsupportedEncodingException e) {
throw new JSONHandlerException(e);
}
}
public static class JSONHandlerException extends Exception {
private static final long serialVersionUID = -6772307308404816615L;

View File

@ -8,11 +8,15 @@ import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
@ -72,16 +76,30 @@ public class SearchFragment extends Fragment {
View view = inflater.inflate(R.layout.fragment_search, container, false);
spinner = (Spinner)view.findViewById(R.id.search_spinner);
triggerTextRequest(Api.SEARCHABLE_PLUGINS);
//spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
return view;
}
@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "Resuming...");
//triggerTextRequest(Api.SEARCHABLE_PLUGINS);
Log.d(LOG_TAG, "Resumed...");
// Add search listener to text field
EditText editText = (EditText)view.findViewById(R.id.search_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Now close the keyboard as finished with
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager imm =
(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
String text = spinner.getSelectedItem().toString();
String text1 = v.getText().toString();
Toast.makeText(context, "Search Called " + text + " " + text1, Toast.LENGTH_SHORT).show();
requestSearch(spinner.getSelectedItem().toString(), v.getText().toString());
return true;
}
return false;
}
});
return view;
}
@Override
@ -118,7 +136,6 @@ public class SearchFragment extends Fragment {
Log.i(LOG_TAG, "populatePluginList - exit");
}
public void itemClicked(int position) {
try {
String request = JsonHelpers.createRequestJSON("id", Integer.toString(position));
@ -153,4 +170,17 @@ public class SearchFragment extends Fragment {
});
}
public void requestSearch(String plugin, String text) {
updateUrl = Api.SEARCH_PLUGIN_FORMATTED;
try {
String request = JsonHelpers.createSearchJSON("data", text);
String url = String.format(Api.SEARCH_PLUGIN_FORMATTED, plugin);
triggerTextRequest(String.format("%s%s", url, request));
Log.d(LOG_TAG, String.format("Search request. apiBase(%s), text(%s)", plugin, text));
} catch (JsonHelpers.JSONHandlerException e) {
e.printStackTrace();
Toast.makeText(context, "Search Request Failed", Toast.LENGTH_SHORT).show();
}
}
}

View File

@ -33,14 +33,6 @@
android:layout_weight="0.4"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Search"
android:id="@+id/searchView"
android:layout_gravity="center_horizontal"
android:layout_weight="0.07" />
</LinearLayout>
<LinearLayout
@ -60,11 +52,13 @@
android:paddingRight="30dp"/>
<EditText
android:imeOptions="actionSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search_text"
style="@android:style/Animation.InputMethod"
android:layout_weight="0.87" />
android:layout_weight="0.87"
android:inputType="text" />
</LinearLayout>