Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2988 varun.gupt 1
package in.shop2020.utils;
2
 
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.io.OutputStreamWriter;
7
import java.io.UnsupportedEncodingException;
8
import java.net.HttpURLConnection;
9
import java.net.MalformedURLException;
10
import java.net.ProtocolException;
11
import java.net.URL;
12
import java.net.URLEncoder;
13
import java.util.Date;
14
import java.util.ResourceBundle;
15
 
16
public class MyNotesGAEClient {
17
	public MyNotesGAEClient() {
18
	}
19
 
20
    private static ResourceBundle properties = ResourceBundle.getBundle(DataLogger.class.getName());
21
    private static String googleAppUrl = "http://" + properties.getString("googleappurl") + "/my-notes";
22
 
23
    public static void putNote(long userId, long entityId, String slide, String note)	{
24
        final Long time = (new Date()).getTime();
25
        //URLEncoder.encode("time", "UTF-8")
26
        try {
27
        	String data = "time=" + URLEncoder.encode(time.toString(), "UTF-8") + "&user=" + URLEncoder.encode(Long.toString(userId), "UTF-8");
28
			data += "&entity=" + URLEncoder.encode(Long.toString(entityId), "UTF-8") + "&slide=" + URLEncoder.encode(slide, "UTF-8");
29
			data += "&note=" + URLEncoder.encode(note, "UTF-8");
30
 
31
            // POST data
32
            URL url = new URL(googleAppUrl);
33
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
34
            conn.setRequestMethod("POST");
35
            conn.setDoOutput(true);
36
 
37
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
38
            wr.write(data);
39
            wr.close();
40
 
41
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
42
            while (in.readLine() != null);
43
            in.close();
44
 
45
		} catch (UnsupportedEncodingException e) {
46
			e.printStackTrace();
47
 
48
		} catch (MalformedURLException e) {
49
			e.printStackTrace();
50
 
51
		} catch (IOException e) {
52
			e.printStackTrace();
53
		}
54
    }
55
 
56
    public static String getNotes(long userId, long entityId)	{
57
		try {
58
			//GET data
59
 
60
            URL url = new URL(googleAppUrl + "?user=" + URLEncoder.encode(Long.toString(userId), "UTF-8")
61
            		+ "&entity=" + URLEncoder.encode(Long.toString(entityId), "UTF-8"));
62
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
63
 
64
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
65
 
66
            StringBuffer res = new StringBuffer();
67
            String line;
68
            while ((line = rd.readLine()) != null)	{
69
            	res.append(line);
70
            	res.append('\r');
71
            }
72
            rd.close();
73
            return res.toString();
74
 
75
		} catch (UnsupportedEncodingException e) {
76
			e.printStackTrace();
77
 
78
		} catch (MalformedURLException e) {
79
			e.printStackTrace();
80
 
81
		} catch (ProtocolException e) {
82
			e.printStackTrace();
83
 
84
		} catch (IOException e) {
85
			e.printStackTrace();
86
		}
87
    	return "";
88
    }
89
}