-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEbayApi.java
More file actions
81 lines (68 loc) · 2.85 KB
/
Copy pathEbayApi.java
File metadata and controls
81 lines (68 loc) · 2.85 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
package com.clinton.yehuda.buyaphonecase;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class EbayApi {// extends AppCompatActivity
private static String appID = "yehudacl-phoneCas-PRD-d246ab013-afbfbdc4";
private static String ebayURL = "http://svcs.ebay.com/";
private Resources resources;//dont remove
private HttpURLConnection urlConnection = null;
private String theChoice;
public EbayApi(Context context) {
this.resources = context.getResources();
}
public String search(String keyword) throws Exception {
String jsonResponse = null;
theChoice = MainActivity.catChoice;
jsonResponse = invokeEbayRest(keyword);
Log.d("json response", jsonResponse);
if(jsonResponse.contains("\"totalEntries\":[\"0\"]")){//if contains this, means that nothing is found
Log.d("json response", "Contains!!!");
theChoice = "http://svcs.ebay.com/services/search/FindingService/v1?" +
"OPERATION-NAME=findItemsAdvanced&" +
"SERVICE-VERSION=1.0.0&" +
"SECURITY-APPNAME=^2&" +
"RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD=true&" +
"categoryId=20349&" +
"keywords="+keyword+"&" +//^3
"paginationInput.entriesPerPage=6";
jsonResponse = invokeEbayRest(keyword);//
Log.d("json response special", jsonResponse);
}
if ((jsonResponse == null) || (jsonResponse.length() < 1)) {
throw (new Exception("No result received from invokeEbayRest"));
}
return (jsonResponse);
}
private String getRequestURL(String keyword) {//
Log.d("get choice", theChoice);
Log.d("keyword",MainActivity.keyword);
CharSequence requestURL = TextUtils.expandTemplate(theChoice, ebayURL, appID, MainActivity.keyword);//
return (requestURL.toString());
}
private String invokeEbayRest(String keyword) throws Exception {//
String result = null;
URL url = new URL(this.getRequestURL(URLEncoder.encode(keyword, "UTF-8")));/////
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (in != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer temp = new StringBuffer();
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
temp.append(currentLine);
}
result = temp.toString();
in.close();
}
return (result);
}
}