Value org.apache.http.message.BasicHttpResponse of type java.lang.String cannot be converted to JSONObject - http-get

I have the error message:
error in executing get org.json.JSONException: Value
org.apache.http.message.BasicHttpResponse#ed9ee8a of type
java.lang.String cannot be converted to JSONObject
And the following code:
response = httpclient.execute(httpget);
JSONObject locationObj = new JSONObject(String.valueOf(response));
JSONObject locationInfo = locationObj.getJSONObject("item");
lat = locationInfo.getInt("lat");
lon = locationInfo.getInt("lon");
I don't want to return the string, I want to get the items from the JSON string and use them for a google map.
String json = "{\"info\": {" +"\"lat\": " + locationGet.getLat() + ", " + "\"lon\": " + locationGet.getLon() + "} }";

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
See How to convert String to JSONObject in Java (The answer by Mappan, not the accepted answer)

Related

I have created container successfully but failed to upload an image/pdf to my blob container using Rest API

I am using PUT Blob Rest API for uploading img/pdf- PUT Blob reference. But it is showing- Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature error (403 response code).
Below are the string to sign:
"PUT\n\n\n41676\n\nimage/png\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:Wed, 17 Feb 2021 10:24:02 GMT\nx-ms-version:2020-02-10\n/acc_name/container_name/12362_50800_13_s_1.png"
Below are the header details:
{ "x-ms-blob-type" : "BlockBlob" , "x-ms-date" : "Wed, 17 Feb 2021 10:24:02 GMT" , "x-ms-version" : "2020-02-10" , "Authorization" : "SharedKey acc_name:XXXQsR728D7aqyJzFi/HXk+qT9rpjYbplUG9KXXXX" , "Content-Type" : "image/png" , "Content-Length" : "41676"}
Below are the URL details:
https://acc_name.blob.core.windows.net/container_name/12362_50800_13_s_1.png
private BasicDBObject createAzureBlob(String storageAPIVersion, String storageAccURL, String storageAccName, String storageAcckey, String storageAccConString,
String containerName, BasicDBObject clientReqObj, File outputfile) throws Exception
{
log.info("Entry createAzureBlob in the GenericServicesDeligator");
BasicDBObject clientResponse = null;
String date = clientReqObj.remove(Constants.F_CREATED_DATE).toString();
String filePath = clientReqObj.remove(Constants.F_PATH).toString();
String contentLength = clientReqObj.remove("Content-Length").toString();
String contentType = clientReqObj.remove("Content-Type").toString();
String hostMethod = "PUT";
StringBuilder authorization = new StringBuilder("SharedKey ").append(storageAccName).append(":").append("sharedKeySign");
StringBuilder canonicalizedResource = new StringBuilder("/").append(storageAccName).append("/").append(containerName).append("/").append(filePath);
BasicDBObject clientReqHeader = new BasicDBObject();
// Host URL
String hostName = clientReqHeader.size()>0l?new StringBuilder("/").append(filePath).append(prepareHostInfo(clientReqHeader)).toString():new StringBuilder("/").
append(filePath).toString();
String hostURL = new StringBuilder(storageAccURL).append("/").append(containerName).append(hostName).toString().trim();
// canonicalizedResource should always be in alphabetical order
canonicalizedResource.append(prepareCanonicalizedResource(clientReqHeader));
//canonicalizedHeader should always be in alphabetical order and include only that begin with x-ms-
clientReqHeader.clear();
clientReqHeader.put("x-ms-blob-type", "BlockBlob");
clientReqHeader.put("x-ms-date", date);
clientReqHeader.put("x-ms-version", storageAPIVersion);
String canonicalizedHeader = prepareCanonicalizedHeader(clientReqHeader);
// string to sign
String stringToSign = prepareStringToSign(hostMethod, contentLength, contentType, canonicalizedHeader, canonicalizedResource.toString());
// encode the string by using the HMAC-SHA256 algorithm over the UTF-8-encoded signature string
String signature = encodeSharedKeySignature(stringToSign, storageAcckey);
clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", signature));
clientReqHeader.put("Content-Length", contentLength);
clientReqHeader.put("Content-Type", contentType);
clientResponse = sendAzureClientRequest(hostMethod, hostURL, clientReqHeader, clientReqObj, outputfile);
log.info("Exit createAzureBlob in the GenericServicesDeligator");
return clientResponse;
}
private String prepareHostInfo(BasicDBObject hostNameObject) throws Exception
{
StringBuilder hostName = new StringBuilder();
for(String key: hostNameObject.keySet())
{
if(hostName.length() == 0l)
hostName.append("?").append(key).append("=").append(hostNameObject.get(key));
else
hostName.append("&").append(key).append("=").append(hostNameObject.get(key));
}
return hostName.toString().trim();
}
private String prepareCanonicalizedResource(BasicDBObject hostNameObject) throws Exception
{
StringBuilder canonicalizedResource = new StringBuilder();
Map<String, String> canonicalizedMap = new TreeMap<String, String>();
canonicalizedMap.putAll(hostNameObject.toMap());
for(String key: canonicalizedMap.keySet())
{
canonicalizedResource.append("\n").append(key).append(":").append(canonicalizedMap.get(key));
}
return canonicalizedResource.toString();
}
private String prepareCanonicalizedHeader(BasicDBObject hostNameObject) throws Exception
{
StringBuilder canonicalizedHeader = new StringBuilder();
Map<String, String> canonicalizedMap = new TreeMap<String, String>();
canonicalizedMap.putAll(hostNameObject.toMap());
for(String key: canonicalizedMap.keySet())
{
canonicalizedHeader.append(key).append(":").append(canonicalizedMap.get(key).toString().trim()).append("\n");
}
return canonicalizedHeader.toString();
}
private String prepareStringToSign(String hostMethod, String contentLength, String contentType, String canonicalizedHeader, String canonicalizedResource)
{
String input = "";
input = new StringBuilder(hostMethod).append("\n").
append("\n"). // Content-Encoding + "\n" +
append("\n"). // Content-Language + "\n" +
append(contentLength!=null?contentLength:"").append("\n").
append("\n"). // Content-MD5 + "\n" +
append(contentType!=null?contentType:"").append("\n").
append("\n"). // Date
append("\n"). // If-Modified-Since + "\n" +
append("\n"). // If-Match + "\n" +
append("\n"). // If-None-Match + "\n" +
append("\n"). // If-Unmodified-Since + "\n" +
append("\n"). // Range + "\n" +
append(canonicalizedHeader).
append(canonicalizedResource).toString().trim();
return input;
}
public String getUTCDate(Date date) throws Exception
{
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return (sdf.format(date) + " GMT").trim();
}
public static String getFileContentType(String fileType)
{
String contentType = "";
switch (fileType)
{
case "pdf":
contentType = "application/pdf";
break;
case "txt":
contentType = "text/plain";
break;
case "bmp":
contentType = "image/bmp";
break;
case "gif":
contentType = "image/gif";
break;
case "png":
contentType = "image/png";
break;
case "jpg":
contentType = "image/jpeg";
break;
case "jpeg":
contentType = "image/jpeg";
break;
case "xls":
contentType = "application/vnd.ms-excel";
break;
case "xlsx":
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "csv":
contentType = "text/csv";
break;
case "html":
contentType = "text/html";
break;
case "xml":
contentType = "text/xml";
break;
case "zip":
contentType = "application/zip";
break;
default:
contentType = "application/octet-stream";
break;
}
return contentType.trim();
}
private String encodeSharedKeySignature(String inputVal, String storageAcckey) throws Exception
{
SecretKeySpec secretkey = new SecretKeySpec(new BASE64Decoder().decodeBuffer(storageAcckey), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretkey);
String hash = Base64.encodeAsString(mac.doFinal(inputVal.getBytes("UTF-8")));
return hash.trim();
}
Kindly share the solution so i would upload a image/pdf.
Thanks in advance
In the private BasicDBObject createAzureBlob() method, in the following section:
//encode the string by using the HMAC-SHA256 algorithm over the UTF-8-encoded signature string
String signature = encodeSharedKeySignature(stringToSign, storageAcckey);
clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", storageAcckey));
clientReqHeader.put("Content-Length", contentLength);
clientReqHeader.put("Content-Type", contentType);
You should use the signature instead of storageAcckey for Authorization header, so please change this line of code
clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", storageAcckey));
to
clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", signature));

How to get H2O ModelMetricsBinominal using Rest API with Retrofit and h2o-bindings

I've got several (Binominal)-DRF-Models and I'd like to get the ModelMatricsBinominalV3 object to extract the thresholds_and_metric_scores variable. I've implemented a solution without retrofit and bindings, but I want to use h2o-bindings to be able to send and receive pojos since my current solution is not very elegant and very fault-prone. Has anybody done something like this and can share his code? Especially I'm interested in extracting Accuracy, F1-Score, Recall etc. for a given threshold.
My current approach calling:
h2oApi.predict(ModelMetricsListSchemaV3)
Works - but is not containing thresholds_and_metric_scores
Whereas calling POST /3/Predictions/models/{model}/frames/{frame} within PostMan works fine and is returning thresholds_and_metric_scores within the json String. How can that be since h2oApi calls POST /3/Predictions/models/{model}/frames/{frame} internally?!
This is my old implementation:
public String getModelMetrics(String modelId, String frameId, double threshold){
String url = buildHttpPath("/3/Predictions/models/" + modelId + "/frames/" + frameId);
int metricIndex = Integer.MIN_VALUE;
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
HttpResponse response;
String json = "";
try
{
response = client.execute(post);
json = EntityUtils.toString(response.getEntity());
}
catch (IOException exception)
{
LOG.error(exception.toString());
}
JsonObject var1 = new Gson().fromJson(json, JsonObject.class);
JsonArray var2 = var1.getAsJsonArray("model_metrics");
JsonElement var3 = var2.get(0);
JsonElement var4 = ((JsonObject) var3).get("thresholds_and_metric_scores");
JsonElement var5 = ((JsonObject) var4).get("data");
JsonArray var6 = (JsonArray) ((JsonArray) var5).get(0);
Double min = Double.MAX_VALUE;
for (int i = 0; i < var6.size(); i++)
{
Double currentElement = var6.get(i).getAsDouble();
Double diff = Math.abs(currentElement - threshold);
if (diff < min)
{
min = diff;
metricIndex = i;
}
}
LOG.info("Received threshold is: " + threshold);
LOG.info("Nearest Threshold is: " + var6.get(metricIndex).getAsDouble());
JsonArray accuracyColumn = (JsonArray) ((JsonArray) var5).get(4);
JsonArray f1Column = (JsonArray) ((JsonArray) var5).get(1);
JsonArray recallColumn = (JsonArray) ((JsonArray) var5).get(6);
JsonArray precisionColumn = (JsonArray) ((JsonArray) var5).get(5);
JsonArray tpColumn = (JsonArray) ((JsonArray) var5).get(14);
JsonArray tnColumn = (JsonArray) ((JsonArray) var5).get(11);
JsonArray fpColumn = (JsonArray) ((JsonArray) var5).get(13);
JsonArray fnColumn = (JsonArray) ((JsonArray) var5).get(12);
Double accuracy = accuracyColumn.get(metricIndex).getAsDouble();
Double f1 = f1Column.get(metricIndex).getAsDouble();
Double recall = recallColumn.get(metricIndex).getAsDouble();
Double precision = precisionColumn.get(metricIndex).getAsDouble();
int tp = tpColumn.get(metricIndex).getAsInt();
int tn = tnColumn.get(metricIndex).getAsInt();
int fp = fpColumn.get(metricIndex).getAsInt();
int fn = fnColumn.get(metricIndex).getAsInt();
return accuracy.toString() + ";" + f1.toString() + ";" + recall.toString() + ";" + precision.toString() + ";" + tp + ";" + tn + ";" + fp + ";" + fn;}
Thank you in advance!

OAuth 1 Authorization Header with RestTemplate

I'm trying to connect to DropBox API via OAuth 1. I have app key and app secret. I need access token and access secret.
I've tried to use DropBox SDK but couldn't find how to do it (current tutorial explains OAuth 2)
I've followed this tutorial and it works via cURL: https://blogs.dropbox.com/developers/2012/07/using-oauth-1-0-with-the-plaintext-signature-method/
I could not make that POST request with that header via RestTemplate:
Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT",
oauth_consumer_key="<app-key>", oauth_token="<request-token>",
oauth_signature="<app-secret>&<request-token-secret>"
I've tried that:
RestTemplate restTemplate = new RestTemplateBuilder().build();
HttpComponentsClientHttpRequestFactory rf =
(HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(1 * 1_000);
rf.setConnectTimeout(1 * 1_000);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization: ", "OAuth");
headers.set("oauth_version", "1.0");
headers.set("oauth_signature_method", "PLAINTEXT");
headers.set("oauth_consumer_key", APP_KEY);
headers.set("oauth_signature", APP_SECRET);
HttpEntity<String> entity = new HttpEntity<>(headers);
Object result = restTemplate.postForEntity(
"https://api.dropbox.com/1/oauth/request_token",
entity,
Object.class)
It results with 400 HTTP Bad Request Error. How can I do it with RestTemplate?
I have been working on this for a while until I could find a solution for myself.
Object result = restClient.postData("https://api.dropbox.com/1/oauth/request_token",
getHeaders(), entity, Object.class);
For Headers
private HttpHeaders getHeaders() throws UnsupportedEncodingException, NoSuchAlgorithmException,
InvalidKeyException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set(HttpHeaders.AUTHORIZATION, getAuthHeader());
return headers;
}
private String getAuthHeader() throws UnsupportedEncodingException, NoSuchAlgorithmException,
InvalidKeyException {
String randomNumber = generateRandomString();
String oauthNonce = getMd5(randomNumber);
Long oauthTimestamp = Instant.now().getEpochSecond();
String baseString = "POST&"+
URLEncoder.encode( URL , StandardCharsets.UTF_8.toString()) + "&" +
URLEncoder.encode(
("deploy=" + DEPLOY +
+ "&oauth_consumer_key=" + CONSUMER_KEY
+ "&oauth_nonce=" + oauthNonce
+ "&oauth_signature_method=" + "HMAC-SHA1"+
+ "&oauth_timestamp=" + oauthTimestamp
+ "&oauth_token=" + TOKEN
+ "&oauth_version= 1.0"
+ "&realm=" + REALM
+ "&script=" + SCRIPT),StandardCharsets.UTF_8.toString()
);
String sigString = URLEncoder.encode(CONSUMER_SECRET,StandardCharsets.UTF_8.toString())
+ "&" + URLEncoder.encode(TOKEN_SECRET,StandardCharsets.UTF_8.toString());
String signature= generateSignature(baseString,sigString,"HmacSHA1");
String headers= " OAuth realm=\""+ URLEncoder.encode(
REALM,StandardCharsets.UTF_8.toString()) + "\", "
+ "oauth_consumer_key=\"" + URLEncoder.encode(
CONSUMER_KEY,StandardCharsets.UTF_8.toString()) + "\", "
+ "oauth_token=\"" + URLEncoder.encode(TOKEN,StandardCharsets.UTF_8.toString()) + "\", "
+ "oauth_signature_method=\"" + URLEncoder.encode(
"HMAC-SHA1",StandardCharsets.UTF_8.toString()) + "\", "
+ "oauth_timestamp=\"" + URLEncoder.encode(
String.valueOf(oauthTimestamp).substring(0,10),StandardCharsets.UTF_8.toString()) + "\", "
+ "oauth_nonce=\"" + URLEncoder.encode(oauthNonce,StandardCharsets.UTF_8.toString()) + "\", "
+ "oauth_version=\"" + URLEncoder.encode(
"1.0",StandardCharsets.UTF_8.toString()) + "\", "
+ "oauth_signature=\"" + URLEncoder.encode(signature,StandardCharsets.UTF_8.toString()) + "\" ";
return headers;
}
The fields SCRIPT, DEPLOY and REALM if they are not necessary for you, remove them
Addicional methods
public static String getMd5(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
StringBuilder bld = new StringBuilder();
String hashText = no.toString(16);
bld.append(hashText);
while (hashText.length() < 32) {
bld.append("0");
}
return bld.toString();
}
public static String generateSignature(String msg, String keyString, String algoritmo) throws InvalidKeyException,
NoSuchAlgorithmException {
String digest;
SecretKeySpec key = new SecretKeySpec(keyString.getBytes(StandardCharsets.UTF_8), algoritmo);
Mac mac = Mac.getInstance(algoritmo);
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes(StandardCharsets.US_ASCII));
digest = Base64.getEncoder().encodeToString(bytes);
return digest;
}
private String generateRandomString() throws NoSuchAlgorithmException {
int largo=20;
String [] carateres = ("0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"
+ "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,S").split(",");
StringBuilder randomString = new StringBuilder();
Random random = SecureRandom.getInstanceStrong();
for(int i=0; i<largo; i++){
randomString.append(carateres[random.nextInt((carateres.length - 1))]);
}
return randomString.toString();
}
I hope it helps someone, greetings

Not able to construct httpmessage for push notification in windows phone 7

I am trying to construct the httpmessage for sending push notification(toast type), but it doesn't recognize methods in the below code. I have this code in Class Library.
Add method in sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
Content Length in sendNotificationRequest.ContentLength = notificationMessage.Length;
GetRequestStream in Stream requestStream = sendNotificationRequest.GetRequestStream()
GetResponse in HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(channel.ChannelUri.ToString());
sendNotificationRequest.Method = "POST";
//Indicate that you'll send toast notifications!
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers = new WebHeaderCollection();
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
if (string.IsNullOrEmpty(txtMessage.Text)) return;
//Create xml envelope
string data = "X-WindowsPhone-Target: toast\r\n\r\n" +
"<?xml version='1.0' encoding='utf-8'?>" +
"<wp:Notification xmlns:wp='WPNotification'>" +
"<wp:Toast>" +
"<wp:Text1>{0}</wp:Text1>" +
"</wp:Toast>" +
"</wp:Notification>";
//Wrap custom data into envelope
string message = string.Format(data, txtMessage.Text);
byte[] notificationMessage = Encoding.Default.GetBytes(message);
// Set Content Length
sendNotificationRequest.ContentLength = notificationMessage.Length;
//Push data to stream
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}
//Get reponse for message sending
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
Got it worked after moving the code to WCF project.

GSON just to read a JsonObject tree from Json data doesn't work

I'm using GSON for a project. In particular I use this code to generate JSON strings:
Gson gs = new Gson();
JsonObject cmdobj = new JsonObject();
cmdobj.addProperty("cmd", cmd);
cmdobj.add("arg", args);
String cmdstr = cmdobj.toString();
which produces something like:
{"cmd":"HANDSHAKE","arg":{"protocol":"syncmanager","serverName":"12345678910"}}
then on a client machine this reads the json data:
String cmdstr = readCommand(this.is);
Gson gs = new Gson();
JsonObject jsobj = gs.fromJson(cmdstr, JsonObject.class);
JsonElement cmd = jsobj.get("cmd");
JsonObject args = jsobj.get("arg").getAsJsonObject();
the problem is that jsobj that should contain the parsed object doesn't contain anything ( if I do toString() prints {} ). Why this? I just want the JSonObject tree that I had on the other side, not an object serialization. Any clues?
JsonObject jsobj = new Gson().fromJson(cmdstr, JsonObject.class)
will try and build a type of JsonObject from the string - which your string clearly isn't.
I think what you want to do is get the raw parse tree - which you can do like this:
JsonObject jsobj = new JsonParser().parseString(cmdstr);
See this for more details.

Resources