I need to send some JSONs to another application of mine, for it to store them in a database.
My problem is that the other app keeps getting NULL parameters.
Here is my "sender" app:
public class Main {
public static void main(String[] args) {
ConcurrentLinkedQueue<Employee> employeeQueue = new ConcurrentLinkedQueue<>();
for (int i = 0; i <= 10; i++) {
byte[] array = new byte[7]; // length is bounded by 7
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));
long id = new Random().nextLong() & 0xffffffffL;
employeeQueue.add(new Employee(id, generatedString));
}
try {
while (!employeeQueue.isEmpty()) {
JSONObject json = new JSONObject(employeeQueue.remove());
URL url = new URL("http://localhost:8080/postgressApp/createEmp");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
try {
json.write(osw);
} catch (JSONException e) {
e.printStackTrace();
}
osw.flush();
osw.close();
os.close();
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response from server: " + response.toString());
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The "receiver app is a simple spring-boot app waiting for "POST" Requests in a Controller and sending the content of the JSON to a database.
It is working for me.
Use Gson for parsing Java Dto to JSON type. download com.google.code.gson.jar-2.2.4 version.
In your code, I got the issue with parsing.
public class Main {
public static void main(String[] args) {
ConcurrentLinkedQueue<Employee> employeeQueue = new ConcurrentLinkedQueue<>();
for (int i = 0; i <= 10; i++) {
byte[] array = new byte[7]; // length is bounded by 7
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));
long id = new Random().nextLong() & 0xffffffffL;
employeeQueue.add(new Employee(id, generatedString));
}
try {
while (!employeeQueue.isEmpty()) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonEmp = gson.toJson(employeeQueue.remove());
// JSONObject json = new JSONObject(employeeQueue.remove());
URL url = new URL("http://localhost:8080/postgressApp/createEmp");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
try {
osw.write(jsonEmp);
// json.write(osw);
} catch (JSONException e) {
e.printStackTrace();
}
osw.flush();
osw.close();
os.close();
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response from server: " + response.toString());
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Convert JsonObject into byte array then write
byte [] outputBytes=json.toString().getBytes("UTF-8")
OutputStream os = con.getOutputStream();
os.write(outputBytes);
Related
I am using the GitHub API to fetch files from a repository. I have the functionality implemented and it does work, I download the needed files but I found something strange, out of the 4 files I get, one is empty (no content inside) even though when I go the to repository and open it there it is clearly with content. The rest of the files have their content in when downloaded. Any idea why that happens?
Here is my code:
public int downloadFromGithub(String repo, String fileName) throws IOException {
URL url = new URL(repo);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", ****);
connection.setRequestProperty("Accept", ****);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK && fileName!=null)
{
return saveFile(connection, fileName);
}
else { return connection.getResponseCode();}
}
public void downloadMultipleFilesFromGithub(String repo,String directoryPath) throws IOException {
URL url = new URL(repo);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", *****);
connection.setRequestProperty("Accept", "***");
String response = getResponseBody(connection);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray array = jsonObj.getJSONArray("tree");
for (int i=0; i < array.length(); i++) {
System.out.println(array.getJSONObject(i).get("path"));
String path = array.getJSONObject(i).get("path").toString();
if(path.contains("Scripts")){
String fileName = path.replace(scriptsDirectoryReplace, "");
downloadFromGithub(scriptsRepositoryDirectory+fileName,fileName);
}
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public String getResponseBody(HttpURLConnection conn) {
BufferedReader br = null;
StringBuilder body = null;
String line = "";
try {
br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
body = new StringBuilder();
while ((line = br.readLine()) != null)
body.append(line);
return body.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int saveFile(HttpURLConnection connection, String fileName) throws IOException {
// opens input stream from the HTTP connection
String saveFilePath = fileSaveDirectory + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream writer = new FileOutputStream(saveFilePath);
InputStream reader = connection.getInputStream();
int bytesRead;
byte[] buffer = new byte[4096];
while ((bytesRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, bytesRead);
}
reader.close();
writer.close();
return connection.getResponseCode();
}
i want to send data from android application to tomcat java server.
Data is just one is client_id which is 1 and second is staff_id which is 2.
after authenticate the client id and staff id from tomcat show me a toast of success....please help...
Code is here
public class MyAsyncTasks extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// display a progress dialog for good user experiance
}
#Override
protected String doInBackground(String... params) {
// implement API in background and store the response in current variable
String current = "";
try {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://192.168.1.13:8080/digitaldisplay/s/m/data");
urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
current += (char) data;
data = isw.read();
System.out.print(current);
}
// return the data to onPostExecute method
return current;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
return "Exception: " + e.getMessage();
}
return current;
}
#Override
protected void onPostExecute(String s) {
Toast.makeText(Register.this, "success", Toast.LENGTH_SHORT).show();
Log.d("data", s.toString());
// dismiss the progress dialog after receiving data from API
try {
// JSON Parsing of data
JSONArray jsonArray = new JSONArray(s);
JSONObject oneObject = jsonArray.getJSONObject(0);
// Pulling items from the array
client = Integer.parseInt(oneObject.getString("client"));
staff = Integer.parseInt(oneObject.getString("staff"));
} catch (JSONException e) {
e.printStackTrace();
}
} }}
The logic in your code looks off to me. This is the pattern I usually follow when making a REST call from an activity using HttpURLConnection:
try {
String endpoint = "http://192.168.1.13:8080/digitaldisplay/s/m/data";
URL obj = new URL(endpoint);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST"); // but maybe you want GET here...
con.setConnectTimeout(10000);
con.setDoInput(true);
con.setDoOutput(true);
JSONObject inputJSON = new JSONObject();
inputJSON.put("Client_id", 1);
inputJSON.put("Staff_id", 2);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(inputJSON.toString());
writer.flush();
writer.close();
os.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
} catch (SocketTimeoutException se) {
// handle timeout exception
responseCode = -1;
} catch (Exception e) {
// handle general exception
responseCode = 0;
}
The only major change in adapting the above code for GET would be that you wouldn't write your input data to the connection. Instead, you would just append query parameters to the URL. I am possibly guessing that you need POST here, since your URL doesn't have any query parameters in it.
I want to get data from an web service and after that to display it in a listView. So I made a function that get the data from the service, but when I tested it I discovered something unexpectedly. When I tested it as a call in the main function of the java class, it works, it returns me the data, but when I use it in the listView class, it doesn't. After some debugging, I still don't get why it doesn't work, but I observed that the only difference is that when the function is called in the main function, the URLConnection begins with sun.net.www.protocol.http.Http.URLConnection:http://... and when it's called in the listView class it begins with com.android.okhttp.internal.huc.HttpURLConnectionImpl:http//.. .
public static String getDataFromServer(String url) {
BufferedReader inputStream = null;
URL dataUrl = null;
String data = null;
//handle url exception
try {
dataUrl = new URL(url);
try {
URLConnection dc = dataUrl.openConnection();
dc.setConnectTimeout(5000);
dc.setReadTimeout(5000);
try {
inputStream = new BufferedReader(new InputStreamReader(dc.getInputStream(), "UTF-8"));
} catch (UnsupportedEncodingException e) { System.out.println(e.getMessage());}
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = inputStream.readLine())!=null)
sb.append(line + "\r\n");
data = sb.toString();
} catch (IOException e) { System.out.println(e.getMessage());
}
} catch (MalformedURLException e) { System.out.println(e.getMessage());}
return data;
}
do somthing like that :
String url = "http://youaddres.com/path";
URL object = new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
//if it is post
con.setRequestMethod("POST");
String me = "{\"json\":\"" + json+ "\",\"json\":\"" + json+"\"}";
OutputStream os = con.getOutputStream();
os.write(me.getBytes());
os.flush();
InputStream inputStr = con.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStr));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String = response = sb.toString();
I'm trying to integrate payu poland payment system to my website but cant get success response. Website done by jsp. Here is link that payu gave for help click im using "create a new order" for configuration. But its not retrieving any answer. Could anywane help me? Here is my jsp code:
`public static String sendPostRequest2(String requestUrl, String payload, String x, String y) {
StringBuilder jsonString = new StringBuilder();
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) {
writer.write(payload);
}
connection.setRequestProperty(x, y);
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
jsonString.append("<br>\n");
}
}
connection.disconnect();
} catch (IOException e) {
try {
URL url = new URL(requestUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
InputStream is;
if (httpConn.getResponseCode() >= 400) {
is = httpConn.getErrorStream();
} else {
is = httpConn.getInputStream();
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
jsonString.append("<br>\n");
}
}
jsonString.append(new RuntimeException(e.getMessage()));
} catch (MalformedURLException ex) {
jsonString.append(new RuntimeException(ex.getMessage()));
Logger.getLogger(PayU.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
jsonString.append(new RuntimeException(ex.getMessage()));
Logger.getLogger(PayU.class.getName()).log(Level.SEVERE, null, ex);
}
}
return jsonString.toString();
}
public static String get() {
String x = "";
String payload2 = "{ 'notifyUrl': 'https://your.eshop.com/notify', 'customerIp': '127.0.0.1', 'merchantPosId': '145227', 'description': 'RTV market', 'currencyCode': 'PLN', 'totalAmount': '21000', 'products': [ { 'name': 'Wireless mouse', 'unitPrice': '15000', 'quantity': '1' }, { 'name': 'HDMI cable', 'unitPrice': '6000', 'quantity': '1' } ]}";
String requestUrl2 = "https://secure.payu.com/api/v2_1/orders/";
x += sendPostRequest2(requestUrl2, payload2, "Authorization", "Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd");
return x;
}`
set the redirect turn off after that you will get the json response. So in your java code set redirect false.
connection.setInstanceFollowRedirects(false);
I am making use of the HttURLconnection and URLConnection api's to connect to a PHP file on my web host (x10host). However the connection does not seem to be established and no string data is sent.
I am sure that my PHP code is correct, so am I using the classes incorrectly in the below code?
#Override
protected String doInBackground(String... params) {
StringBuilder respData = new StringBuilder();
InputStream stream = null;
OutputStream os = null;
HttpURLConnection httpUrlConnection;
URLConnection conn;
URL url;
try {
url = new URL("my_url/recieveString.php");
conn = url.openConnection();
httpUrlConnection = (HttpURLConnection) conn;
httpUrlConnection.setUseCaches(false);
//httpUrlConnection.setRequestProperty("User-Agent", "App");
httpUrlConnection.setConnectTimeout(30000);
httpUrlConnection.setReadTimeout(30000);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setDoOutput(true);
os = httpUrlConnection.getOutputStream();
toSubmit = "test";
stream = new ByteArrayInputStream(toSubmit.getBytes(StandardCharsets.UTF_8));
copy(stream, os);
httpUrlConnection.connect();
int responseCode = httpUrlConnection.getResponseCode();
if (200 == responseCode) {
InputStream is = httpUrlConnection.getInputStream();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(is);
char[] buffer = new char[1024];
int len;
while ((len = isr.read(buffer)) != -1) {
respData.append(buffer, 0, len);
}
} finally {
if (isr != null) {
isr.close();
success = true;
}
}
is.close();
} else {
// use below to get error stream
//inputStream = httpUrlConnection.getErrorStream();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return "done";
}
}