How to get only Json content from HttpEntity Java - java

Using HttpEntity I am getting a very long text which has special characters as well along with Json.
I tried regex but it's not working as it is almost 30000 of characters.
Is there a way that i can only get Json data from the HttpEntity. Even string split did not work since it has so many of special characters.
public JSONObject sendGet(String URL, String userName, String password) throws Exception {
getRequest = new HttpGet(URL);
getRequest.addHeader("User-Agent", USER_AGENT);
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
provider.setCredentials(AuthScope.ANY, credentials);
client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
response = client.execute(getRequest);
HttpEntity entity = response.getEntity();
outputFile = new File(directoryPath + "/target/response.txt");
fos = new FileOutputStream(outputFile);
headers = response.getAllHeaders();
bw = new BufferedWriter(new OutputStreamWriter(fos));
for (Header header: headers) {
bw.write(header.getName() + ": " + header.getValue() + "\n");
}
bw.write(response.getEntity());
bw.write("Response Code : " + response.getStatusLine());
String content = EntityUtils.toString(entity); //When i print content it has string other than json as well
JSONObject obj = new JSONObject(content); //Here i receive A JSONObject text must begin with '{' at 1 [character 2 line 1]
JSONArray keys = obj.names();
Object test = JSON.parse(content);
jsonFiles = new File(directoryPath + "/JsonFiles/test.json");
fos = new FileOutputStream(jsonFiles);
bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write(content);
bw.close();
return obj;
}

Try adding the following Headers:
getRequest.addHeader("Accept", "application/json");
getRequest.addHeader("Content-Type", "application/json");
getRequest.addHeader("Accept-Charset", "utf-8");

Related

Passing json array in a key value pair in Java

I'm trying to send data using rest webservice in Java. I'm able to post data using Java in POST but now I need to send data in rawModeData and I also need to send JSONArray in surveysData key.
Please find the attached screenshot
This is the Code that I'm using to send data
try {
URL url = new URL(webServiceData.getSyncSurveyDataUrl());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// ConnectionType
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setRequestProperty("charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Create the output form content
OutputStream out = httpURLConnection.getOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
JSONObject objects = new JSONObject();
objects.put("constituency", constituency);
objects.put("longitude", longitude);
objects.put("latitude", latitude);
objects.put("createdOn", createdOn);
for (Map.Entry<String, String> map : linkedHashMap.entrySet()) {
objects.put(map.getKey(), map.getValue());
}
writer.write("createdByDeviceId");
writer.write("=");
writer.write(URLEncoder.encode(loginModel.getDeviceId(), "UTF-8"));
writer.write("&");
writer.write("createdByMobileNumber");
writer.write("=");
writer.write(URLEncoder.encode(loginModel.getMobileNumber(), "UTF-8"));
writer.write("&");
writer.write("state");
writer.write("=");
writer.write(URLEncoder.encode(loginModel.getLoggedInState(), "UTF-8"));
writer.write("&");
writer.write("eventId");
writer.write("=");
writer.write(URLEncoder.encode(loginModel.getEventId(), "UTF-8"));
writer.write("&");
writer.write("surveysData");
writer.write("=");
writer.write(URLEncoder.encode(objects.toString(), "UTF-8"));
if (httpURLConnection.getResponseCode() != 200) {
System.out.println("Exception in 200: " + httpURLConnection.getResponseCode());
System.out.println("Exception Message: " + httpURLConnection.getResponseMessage());
/*errorLabel.setText(httpURLConnection.getResponseMessage());
errorLabel.setVisible(true);*/
}
} catch (Exception e) {
return false;
}
I'm getting 401 error message
I think you missed some authentication token in headers. Something like : httpURLConnection.setRequestProperty("tokenKey", "yourAuthToken");
You are not connecting to connection. Also I think that you need to setFixedLengthStreamingMode on connection and write output as byte array. Example:
//...
String queryParameters = formatQueryParameters(requestData);
byte[] output = queryParameters.getBytes(Charset.forName("UTF-8"));
connection.setFixedLengthStreamingMode(output.length);
connection.connect();
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(output);
//...
public static String formatQueryParameters(Map<String, String> requestData) throws UnsupportedEncodingException {
String equals = URLEncoder.encode("=", "UTF-8");
String ampersand = URLEncoder.encode("&", "UTF-8");
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> entry : requestData.entrySet()) {
String encodedKey = URLEncoder.encode(entry.getKey(), "UTF-8");
String encodedValue = URLEncoder.encode(entry.getValue(), "UTF-8");
builder.append(encodedKey).append(equals).append(encodedValue).append(ampersand);
}
builder.deleteCharAt(builder.lastIndexOf(ampersand));
return builder.toString();
}

java.lang.IllegalArgumentException: Illegal character in query at index 59

What i am doing:
I am trying to make a reverse geocoding in android
I am getting error as::
java.lang.IllegalArgumentException: Illegal character in query at index 59: http://maps.google.com/maps/api/geocode/json?address=Agram, Bengaluru, Karnataka, India&sensor=false
NOte: that request gets a json response in browser but not from my class below
This line is giving this error::
HttpGet httpget = new HttpGet(url);
JSONfunctions.java
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
Use URLEncoder.encode() to encode the value of your address parameter "Agram, Bengaluru, Karnataka, India" before putting it in the URL string so that it becomes something like
http://maps.google.com/maps/api/geocode/json?address=Agram,+Bengaluru,+Karnataka,+India&sensor=false
i.e. spaces changed to + and other special octets represented as %xx.
Browsers do smart URL encoding for strings entered in the address bar automatically so that's why it works there.
Build your url like,
final StringBuilder request = new StringBuilder(
"http://maps.googleapis.com/maps/api/geocode/json?sensor=false");
request.append("&language=").append(Locale.getDefault().getLanguage());
request.append("&address=").append(
URLEncoder.encode(locationName, "UTF-8"));
I am using httpclient 4.3.3
String messagestr = "Welcome to Moqui World";
String url="http://my.example.com/api/sendhttp.phpauthkey="+URLEncoder.encode("17djssnvndkfjb110d3","UTF-8")+"&mobiles=91"+URLEncoder.encode(contactNumber,"UTF-8")+"&message="+URLEncoder.encode(messagestr,"UTF8")+"&sender="+URLEncoder.encode("WMOQUI","UTF-8")+"&route=4";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
It's working fine for me. I hope this may help you.

How to Make an HTTP POST with JSON object as data?

Can anyone refer me to a single, simple resource explaining how to in Java make an HTTP POST with JSON object as data? I want to be able to do this without using Apache HTTP Client.
The following is what I've done so far. I am trying to figure out how to modify it with JSON.
public class HTTPPostRequestWithSocket {
public void sendRequest(){
try {
String params = URLEncoder.encode("param1", "UTF-8")
+ "=" + URLEncoder.encode("value1", "UTF-8");
params += "&" + URLEncoder.encode("param2", "UTF-8")
+ "=" + URLEncoder.encode("value2", "UTF-8");
String hostname = "nameofthewebsite.com";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
String path = "/nameofapp";
// Send headers
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("POST "+path+" HTTP/1.0rn");
wr.write("Content-Length: "+params.length()+"rn");
wr.write("Content-Type: application/x-www-form-urlencodedrn");
wr.write("rn");
// Send parameters
wr.write(params);
wr.flush();
// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
socket.close();//Should this be closed at this point?
}catch (Exception e) {e.printStackTrace();}
}
}
JSON is just a string.
Just add the json objet as a post value.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("jsonData", new JSONObject(json)));//json
params.add(new BasicNameValuePair("param1", "somevalue"));//regular post value

Multipart, set Content-Type of one part

I have this code to post data to my server:
// HTTP Settings
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://myserver.com/Login");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
// Http Headers
postRequest.addHeader("Accept", "application/xml");
postRequest.addHeader("Connection", "keep-alive");
// Credentials
reqEntity.addPart("username", new StringBody(ServerData.username));
reqEntity.addPart("password", new StringBody(ServerData.password));
if (m_sigFile.exists()) {
Bitmap m_sig = BitmapFactory.decodeFile(sigFilePath
+ "m_sig.jpg");
ByteArrayOutputStream m_bao = new ByteArrayOutputStream();
m_sig.compress(Bitmap.CompressFormat.JPEG, 90, m_bao);
byte[] m_ba = m_bao.toByteArray();
String m_ba1 = Base64.encodeToString(m_ba, 0);
reqEntity.addPart("m_sig.jpg", new StringBody(m_ba1));
}
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
The code works perfectly, all data is send to the server except for the jpeg file. The server only accepts the file if I set the content type to 'image/jpeg', but only for the image. The username and password has to be in plain text. Is this possible?
This will work:
ContentBody cbFile = new FileBody(new File(myPath
+ "image_1.jpg"),
"image/jpeg");
reqEntity.addPart("photo1"), cbFile);
Don't forget to check if you file exists!
There is a constructor for StringBody that accepts content type:
new StringBody(titleString, "application/atom+xml", Charset.forName("UTF-8"));

Get the CONTENT , Keywords, Title of a page using Apache TIKA

Anything wrong with this code.. If I add this line (String c= t.parseToString(content);) below the Ti t = new Ti(); then I get the actual content of the url back but after that I get null values for Keywords, Title and Authors. And If I remove this line (String c= t.parseToString(content);) then I get the actual values for Title, Author and Keywords.. Why is it so??
HttpGet request = new HttpGet("http://xyz.com/d/index.html");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
System.out.println(content)
Ti t = new Ti();
String ct= t.parseToString(content);
System.out.println(ct);
Metadata md = new Metadata();
Reader r = t.parse(content, md);
System.out.println(md);
System.out.println("Keywords: " +md.get("keywords"));
System.out.println("Title: " +md.get("title"));
System.out.println("Authors: " +md.get("authors"));
You are reading the same stream multiple times. After you read a stream fully, you cannot read it again. Do something like,
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
//http://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-in-java
byte[] content = streamToByteArray(entity.getContent());
String ct = t.parseToString(new ByteArrayInputStream(content));
System.out.println(ct);
Metadata md = new Metadata();
Reader r = t.parse(new ByteArrayInputStream(content), md);
System.out.println(md);

Categories

Resources