Repository navigation
Expand file tree
/
Copy pathMainActivity.java
More file actions
200 lines (154 loc) · 6.44 KB
/
Copy pathMainActivity.java
File metadata and controls
200 lines (154 loc) · 6.44 KB
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
package com.example.majisha.ebayshopping;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.facebook.FacebookSdk;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class MainActivity extends ActionBarActivity {
private EditText input_keyword;
private EditText input_pricefrom;
private EditText input_priceto;
private TextView t_result;
private Spinner s_sortby;
/**
* Background Async Task to get all results by making HTTP Request
* */
class getSearchResult extends AsyncTask<String, String, JSONObject> {
/**
* getting All products from url
* */
@Override
protected JSONObject doInBackground(String... args) {
JSonParser jParser = new JSonParser();
// Getting JSON from URL
String url = null;
String sort_order = "";
try {
String keyword = URLEncoder.encode(input_keyword.getText().toString(), "UTF-8");
String pagination = "&results_per_page=5";
String selectedSortby = s_sortby.getSelectedItem().toString();
if(selectedSortby.equals("Best Match")){
sort_order = "&sort_by=BestMatch";
}
else if(selectedSortby.equals("Price:highest first")){
sort_order = "&sort_by=CurrentPriceHighest";
}
else if(selectedSortby.equals("Price+Shipping:highest first")){
sort_order = "&sort_by=PricePlusShippingHighest";
}
else if(selectedSortby.equals("Price+Shipping:lowest first")){
sort_order = "&sort_by=PricePlusShippingLowest";
}
String pricefrom = input_pricefrom.getText().toString();
String priceto = input_priceto.getText().toString();
String item_filter = "";
if(!pricefrom.isEmpty()){
item_filter = "&low_price_range="+pricefrom;
}
if(!priceto.isEmpty()){
item_filter +="&high_price_range="+priceto;
}
url = "http://searchebay-env.elasticbeanstalk.com/my_index.php?keywords=" + keyword + pagination + sort_order + item_filter;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return jParser.getJSONResults(url);
}
protected void onPostExecute(JSONObject json) {
try {
String result = json.getString("ack");
if(result.equals("Success")){
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("JSON_OBJECT", json.toString());
intent.putExtra("SEARCH_KEYWORD",input_keyword.getText().toString());
startActivity(intent);
}
else {
t_result = (TextView) findViewById(R.id.t_result);
t_result.setText(result);
}
}
catch (JSONException e){
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
input_keyword = (EditText) findViewById(R.id.i_keyword);
input_pricefrom = (EditText) findViewById(R.id.i_fromprice);
input_priceto = (EditText) findViewById(R.id.i_toprice);
t_result = (TextView) findViewById(R.id.t_result);
s_sortby = (Spinner) findViewById(R.id.dropdown_sortby);
//add onclick for clear button
findViewById(R.id.button_clear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
input_keyword.setText("");
input_pricefrom.setText("");
input_priceto.setText("");
t_result.setText("");
input_keyword.setError(null);
input_pricefrom.setError(null);
input_priceto.setError(null);
s_sortby.setSelection(0);
}
});
//add onclick for search button
findViewById(R.id.button_search).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean v_result = validate_input(v);
//validation was successful, no errors -> send request to php server with parameters
if (v_result){
new getSearchResult().execute();
}
}
});
}
public boolean validate_input(View v){
if (input_keyword.getText().toString().isEmpty()){
input_keyword.setError("Please enter a keyword");
return false;
}
if(!input_pricefrom.getText().toString().isEmpty() && !input_priceto.getText().toString().isEmpty() ) {
if (Float.parseFloat(input_pricefrom.getText().toString()) > Float.parseFloat(input_priceto.getText().toString())) {
input_pricefrom.setError("Price From should be less than or equal to Price To");
return false;
}
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}