Created
January 1, 2018 08:30
-
-
Save navinthenapster/f1845cefd767b9812937eeec3165fd6a to your computer and use it in GitHub Desktop.
Send ajax calls from java ( http protocol data to Node or server). It convert the Map object into JSON object and vice versa. It requires gson.jar and java-json.jar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.controller; | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
public class Http_to_node { | |
static String node_server = "http://localhost:8124"; | |
public static Map<String,String> sendrequest(String strPostData, String operation) { | |
URL url; | |
Map<String,String> m = new HashMap<String,String>(); | |
try { | |
// String strPostData = "password=123"; | |
url = new URL(node_server + "/"+operation); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("POST"); | |
conn.setRequestProperty("Content-Type", "application/json"); | |
conn.setRequestMethod("POST"); | |
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); | |
conn.setRequestProperty("Content-length",Integer.toString(strPostData.length())); | |
conn.setRequestProperty("Content-Language", "en-GB"); | |
conn.setRequestProperty("charset", "utf-8"); | |
conn.setUseCaches(false); | |
conn.setDoOutput(true); | |
DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); | |
dos.writeBytes(strPostData); | |
dos.flush(); | |
dos.close(); | |
int intResponse = conn.getResponseCode(); | |
System.out.println("\nSending 'POST' to " + url.toString() | |
+ ", data: " + strPostData + ", rc: " + intResponse); | |
; | |
BufferedReader in = new BufferedReader(new InputStreamReader( | |
conn.getInputStream())); | |
String inputLine; | |
StringBuffer content = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
content.append(inputLine); | |
} | |
in.close(); | |
System.out.println("\nResponses from " + url.toString()); | |
System.out.println(content); | |
String strcontent = new String(content); | |
m= Http_to_node.stringintojson(strcontent); | |
conn.disconnect(); | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
} | |
return m ; | |
} | |
public static Map<String,String> getDetail(String strGetData, String operation) { | |
URL url; | |
Map<String,String> m = new HashMap<String,String>(); | |
try { | |
// String strPostData = "password=123"; | |
url = new URL(node_server + "/"+operation); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setRequestProperty("Content-Type", "application/json"); | |
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); | |
conn.setRequestProperty("Content-length",Integer.toString(strGetData.length())); | |
conn.setRequestProperty("Content-Language", "en-GB"); | |
conn.setRequestProperty("charset", "utf-8"); | |
conn.setUseCaches(false); | |
conn.setDoOutput(false); | |
int intResponse = conn.getResponseCode(); | |
System.out.println("\nSending 'GET' to " + url.toString() | |
+ ", data: " + strGetData + ", rc: " + intResponse); | |
; | |
BufferedReader in = new BufferedReader(new InputStreamReader( | |
conn.getInputStream())); | |
String inputLine; | |
StringBuffer content = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
content.append(inputLine); | |
} | |
in.close(); | |
System.out.println("\nResponses from " + url.toString()); | |
System.out.println(content); | |
String strcontent = new String(content); | |
m= Http_to_node.stringintojson(strcontent); | |
conn.disconnect(); | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
} | |
return m ; | |
} | |
public static Map<String,String> stringintojson(String content){ | |
Map<String,String> m = new HashMap<String,String>(); | |
try{ | |
JsonParser jsonParser = new JsonParser(); | |
JsonElement element = jsonParser.parse(content.toString()); | |
JsonObject result = element.getAsJsonObject(); | |
Set<Map.Entry<String, JsonElement>> entrySet = result.entrySet(); | |
for(Map.Entry<String, JsonElement> entry : entrySet) { | |
m.put(entry.getKey(),entry.getValue().toString().replace("\"", "")); | |
//code... | |
} | |
}catch(IllegalStateException e){ | |
} | |
return m; | |
} | |
public static void main(String[] args){ | |
String str=""; | |
Map<String,String>map = Http_to_node.getDetail(str,"txhash/"+"0x3aae48b3e1ea16b29a04c548d73711d70f36e0ea56e597652b1714f32adee2e2"); | |
System.out.println(map); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment