-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.txt
268 lines (234 loc) · 9.71 KB
/
help.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.cbitss.careforu;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.apache.commons.lang3.text.WordUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link Knowmed#newInstance} factory method to
* create an instance of this fragment.
*/
public class Knowmed extends Fragment implements Callback,AdapterView.OnItemClickListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
Button search; EditText search_box; ListView listView;
String token; ArrayAdapter<String> adapter; ArrayList<String> medList;
JSONArray med_array; int type; // type 0 for autocomplete, type 1 for autocomplete
public Knowmed() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Knowmed.
*/
// TODO: Rename and change types and number of parameters
public static Knowmed newInstance(String param1, String param2) {
Knowmed fragment = new Knowmed();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTitle("Know Your Med");
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_knowmed, container, false);
JSONObject requestObject = new JSONObject();
try {
requestObject.put("grant_type", "client_credentials");
requestObject.put("client_id", "1b72cf4474d2c203ed16cf0ee86665b0792c97f0830fda3267d86d74a4f21905");
requestObject.put("client_secret", "f07e56e0e5ad7274d41ac985a8d034ae609accde515aa3bb8a9af1a731f60e64");
requestObject.put("scope", "public read write");
} catch (JSONException e) {
e.printStackTrace();
}
final OkHttpClient client = new OkHttpClient();
type = 0;
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, requestObject.toString());
final Request request = new Request.Builder()
.addHeader("Content-Type", "application/json")
.url("http://www.healthos.co/api/v1/oauth/token.json")
.post(body)
.build();
client.newCall(request).enqueue(this);
search = (Button) v.findViewById(R.id.search_button);
search_box = (EditText) v.findViewById(R.id.search_text);
listView= (ListView) v.findViewById(R.id.med_list);
listView.setOnItemClickListener(this);
search_box.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView tv, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
search();
}
return handled;
}
});
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
void search()
{
String find = search_box.getText().toString();
final OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
final Request request = new Request.Builder()
.addHeader("authorization", "Bearer " + token)
.url("http://www.healthos.co/api/v1/search/medicines/brands/" + find)
.build();
type=1;
client.newCall(request).enqueue(this);
}
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (!response.isSuccessful()) {
Log.d("No token", "onResponse: "+response.toString());
throw new IOException("Unexpected code " + response);
} else {
switch(type)
{
case 0 :
{
try {
JSONObject response_JSON = new JSONObject(response.body().toString());
token = response_JSON.getString("access_token");
Log.d("Token HElp", token);
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
case 1 :
{
try {
String json = response.body().toString();
med_array = new JSONArray(json);
// Getting all names in array list
medList = new ArrayList<String>();
for(int i=0;i<med_array.length();i++)
{
medList.add(WordUtils.capitalizeFully(med_array.getJSONObject(i).getString("name")));
}
//Log.d("arr",medList.toString());
//Updating list view on UI Thread
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,medList);
listView.setAdapter(adapter);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
default:
break;
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
JSONObject med = med_array.getJSONObject(position);
// Toast.makeText(getContext(), med.getString("medicine_id"), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(),MedInfo.class);
intent.putExtra("ID",med.getString("medicine_id"));
intent.putExtra("token",token);
intent.putExtra("name",med.getString("name"));
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}