XML Request with HttpClient throws Null Exception - java

try
{
String xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request_inquiry><partner_id>0999</ partner_id><terminal_type>6012</ terminal_ type><product_code>4001</product _code><date_time>20130715115100</date_time><trx_id>SDFSF11234424ADFA</trx_id><data><cust_id>030913320611</cust_id></data></request_inquiry>";
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
HttpPost httpPost = new HttpPost("202.169.43.53:52056/transaction");
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=ISO");
// httpPost.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(xmlReq.length()));
StringEntity se = new StringEntity(xmlReq, ContentType.TEXT_XML);
httpPost.setEntity(se);
System.out.println("Request>>"+httpPost);
StringBuilder html = new StringBuilder("");
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() != 200) {
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024] ;
while(in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println("Output HTML>> "+html.toString());
}
else{
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024] ;
while(in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println(html);
}
} catch (Exception ex) {
throw new SystemException(Common.ERROR_OTHER, ex.getMessage());
}
}
catch(Exception ex) {
System.out.println("Exception>>"+ex.getMessage());
}
I've tried many ways to send XML request to server. and one of the way is look likes the code above. And I have no idea why throws NullException? Is there something wrong with my code? Thanks for help.

The actual exception is in the line
HttpPost httpPost = new HttpPost("202.169.43.53:52056/transaction");
saying
java.lang.IllegalArgumentException
at java.net.URI.create(URI.java:841)
at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:76)
at Test.main(Test.java:22)
Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0: 202.169.43.53:52056/transaction
at java.net.URI$Parser.fail(URI.java:2810)
at java.net.URI$Parser.checkChars(URI.java:2983)
at java.net.URI$Parser.checkChar(URI.java:2993)
at java.net.URI$Parser.parse(URI.java:3009)
at java.net.URI.<init>(URI.java:577)
at java.net.URI.create(URI.java:839)
... 2 more
It is because the URI is missing the protocol like http:// or https://
Ex:
try {
String xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request_inquiry><partner_id>0999</ partner_id><terminal_type>6012</ terminal_ type><product_code>4001</product _code><date_time>20130715115100</date_time><trx_id>SDFSF11234424ADFA</trx_id><data><cust_id>030913320611</cust_id></data></request_inquiry>";
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 30);
httpClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 30);
HttpPost httpPost = new HttpPost("http://202.169.43.53:52056/transaction");
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=ISO");
// httpPost.setHeader(HttpHeaders.CONTENT_LENGTH,
// Integer.toString(xmlReq.length()));
StringEntity se = new StringEntity(xmlReq, ContentType.TEXT_XML);
httpPost.setEntity(se);
System.out.println("Request>>" + httpPost);
StringBuilder html = new StringBuilder("");
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() != 200) {
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024];
while (in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println("Output HTML>> " + html.toString());
} else {
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024];
while (in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println(html);
}
} catch (Exception ex) {
ex.printStackTrace();
}
Note: When you are logging exceptions make sure you log the stack trace as well, since it will give you more details about the exception like which class, method and line caused the exception.

Related

Printout results in while loop output different results in Java azure

I am sending json object to azure cloud in java successfully.but the problem is my reciever,the message is recieved well but the problem is when i want to send it back to PHP:
I am sending this message:
{"Id":"914897","Name":"Broken window","Description":"Window
broken","PriorityId":"1"}
As I receive this message I want to first printout the message to verify whether i am getting the results and i sent it.however inside the while loop is printing correct but outside a broken results here is my Code:
try {
Configuration config
= ServiceBusConfiguration.configureWithSASAuthentication(
);
ServiceBusContract service = ServiceBusService.create(config);
ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
//send object
HttpClient httpClient = new DefaultHttpClient();
Gson gson= new Gson();
while (true) {
ReceiveQueueMessageResult resultQM = service.receiveQueueMessage("mobile",opts);
BrokeredMessage message = resultQM.getValue();
if (message != null && message.getMessageId() != null) {
System.out.println("MessageID: " + message.getMessageId());
// Display the queue message.
System.out.print("From queue:");
byte[] b = new byte[20000000];
String message_from_queue = null;
String thu =null;
String jsonn = null;
int numRead = message.getBody().read(b);
while (-1 != numRead) {
message_from_queue = new String(b);
message_from_queue = message_from_queue .trim();
numRead = message.getBody().read(b);
//System.out.print("inside while" +message_from_queue + **"\n");//{"Id":"914897","Name":"Broken window","Description":"Window broken","PriorityId":"1"}**
try {
HttpPost request = new HttpPost("http://localhost:3308/emlive/index.php/Api/createDefect");
StringEntity params =new StringEntity("defect=" + message_from_queue );
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.addHeader("Accept","application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
//System.out.printf("---------------------------------Done-------------------------------");
// handle response here...
message.setSessionId("");
System.out.println(org.apache.http.util.EntityUtils.toString(response.getEntity()));
org.apache.http.util.EntityUtils.consume(response.getEntity());
}
catch (Exception ex) {
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown();
}
}
//System.out.print("outside while" +message_from_queue + "\n");//Broken window","Description":"Window broken","PriorityId":"1"}
System.out.println();
System.out.println("Custom Property: "
+ message.getProperty("MyProperty"));
//service.deleteMessage(message);
System.out.println("Deleting this message.");
//service.deleteMessage(message);
} else {
System.out.println("Finishing up - no more messages.");
break;
// Added to handle no more messages.
// Could instead wait for more messages to be added.
}
}
} catch (ServiceException e) {
System.out.print("ServiceException encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
} catch (Exception e) {
System.out.print("Generic exception encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
}
I am getting this results : Printing inside while loop:
while (-1 != numRead) {
message_from_queue = new String(b);
message_from_queue = message_from_queue .trim();
numRead = message.getBody().read(b);
System.out.print("inside while" +message_from_queue + **"\n");//{"Id":"914897","Name":"Broken window","Description":"Window broken","PriorityId":"1"}**
}
Printing outside while loop:
System.out.print("outside while" +message_from_queue + "\n");/*Broken window","Description":"Window broken","PriorityId":"1"}
All Thanks to Dominic Betts from this link https://azure.microsoft.com/en-us/documentation/articles/service-bus-java-how-to-use-queues/#comments
I used the following code to achieve my goal:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(message_from_queue );
I think the issue was caused by doing the POST request in the inside while loop. Codes in the inside while loop is for reading messages from queue, So the POST request of HttpClient should be in the outside while loop.
I refered to the doc https://azure.microsoft.com/en-us/documentation/articles/service-bus-java-how-to-use-queues/ and modified your code as below:
try {
Configuration config = ServiceBusConfiguration.configureWithSASAuthentication("<namespace>", "<sas_key_name>",
"<sas_key>", ".servicebus.windows.net");
ServiceBusContract service = ServiceBusService.create(config);
ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
// send object
// HttpClient httpClient = new DefaultHttpClient();
CloseableHttpClient httpClient = HttpClients.createDefault();
// Gson gson = new Gson();
while (true) {
ReceiveQueueMessageResult resultQM = service.receiveQueueMessage("mobile", opts);
BrokeredMessage message = resultQM.getValue();
if (message != null && message.getMessageId() != null) {
System.out.println("MessageID: " + message.getMessageId());
// Display the queue message.
System.out.print("From queue:");
byte[] b = new byte[20000000];
String message_from_queue = null;
// String thu = null;
// String jsonn = null;
int numRead = message.getBody().read(b);
while (-1 != numRead) {
message_from_queue = new String(b);
message_from_queue = message_from_queue.trim();
numRead = message.getBody().read(b);
// System.out.print("inside while" +message_from_queue +
// **"\n");//{"Id":"914897","Name":"Broken
// window","Description":"Window
// broken","PriorityId":"1"}**
}
// System.out.print("outside while" +message_from_queue +
// "\n");//Broken window","Description":"Window
// broken","PriorityId":"1"}
int statusCode = -1;
try {
HttpPost request = new HttpPost("http://localhost:3308/emlive/index.php/Api/createDefect");
StringEntity params = new StringEntity("defect=" + message_from_queue);
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.addHeader("Accept", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
// System.out.printf("---------------------------------Done-------------------------------");
// handle response here...
message.setSessionId("");
System.out.println(EntityUtils.toString(response.getEntity()));
EntityUtils.consume(response.getEntity());
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.close();
}
System.out.println();
System.out.println("Custom Property: " + message.getProperty("MyProperty"));
if (statusCode == 200) {
// Remove message from queue.
System.out.println("Deleting this message.");
service.deleteMessage(message);
}
} else {
System.out.println("Finishing up - no more messages.");
break;
// Added to handle no more messages.
// Could instead wait for more messages to be added.
}
}
} catch (ServiceException e) {
System.out.print("ServiceException encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
} catch (Exception e) {
System.out.print("Generic exception encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
}
Best Regards

StringBuilder lost data when convert to String

here what's the problem
I have problem when I tried to get String from my StringBuilder
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 128 * 1024);
StringBuilder dataResponseSB = new StringBuilder();
String line ;
while ((line = reader.readLine()) != null) {
dataResponseSB.append(line);
if (DataFactory.DEBUG_MODE) {
// all data here are complete
Log.i("===LoadDataActivity","line: "+line);
}
}
String rawdata = new String(dataResponseSB); // dataResponseSB.toString(); also not work
if (DataFactory.DEBUG_MODE) {
// data here are lost
Log.i("===LoadDataActivity","rawdata: "+rawdata);
}
(-) I receive a huge data from BufferedReader .readLine()
(-) I use Log to check and sure that I got about 5 line of 8000 Buffer Size per line and I am very sure that I have receive all data properly
(1) I append each line to StringBuilder Here
(-) after I append all the line to StringBuilder
(2) I try to convert it back to String
(-) Now, the problem, the when I check to new String here, the data have only 8192 (it should contain at least 30,000 or more)
What is the problem ? I am not sure it lost when it append to StringBuilder(1) or it lost when it convert back to String (2)
I add the code that I have tried below here ,, I have tried both UTF8 and without UTF8
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
//params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, );
params.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 128 * 1024);
HttpClient client = new DefaultHttpClient(params);
// HttpClient client = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(DataFactory.REQUEST_API_URL + "?id=" + DataFactory.USER_ID );
// Depends on your web service
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpConnectionParams.setSocketBufferSize(client.getParams(), 128 * 1024);
HttpResponse response = client.execute(httppost);
//response.setParams(client.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 128 * 1024));
//String rawdata = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
// String rawdata = EntityUtils.toString(response.getEntity());
String rawdata = getResponseBody(response.getEntity());
//Scanner s = new Scanner(response.getEntity().getContent()).useDelimiter("\\A");
//String rawdata = s.hasNext() ? s.next() : "";
/*
//BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// ===================
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 128 * 1024);
StringBuilder dataResponseSB = new StringBuilder();
String line ;
while ((line = reader.readLine()) != null) {
dataResponseSB.append(line);
if (DataFactory.DEBUG_MODE) {
Log.i("===LoadDataActivity","line: "+line);
}
}
dataResponseSB.trimToSize();
String rawdata = new String(dataResponseSB);
/*
InputStreamReader reader = new InputStreamReader(response.getEntity().getContent());
StringBuffer sb = new StringBuffer();
int c;
while ((c = reader.read()) != -1) {
sb.append((char)c);
if (DataFactory.DEBUG_MODE) {
//Log.i("===LoadDataActivity","line: "+line);
}
}
*/
I'm pretty sure this is the problem:
Log.i("===LoadDataActivity","rawdata: "+rawdata);
You're assuming that a log entry can include all of your data - I believe each log entry is limited to 8192 characters.
I suggest you log rawdata.length() and you'll see that it's actually got all of the data - it's just logging it that's failing.
try this,,
public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
System.out.println("GEN END : " + Calendar.getInstance().getTimeInMillis());
return buffer.toString();
}
// Try this way,hope this will help you to solve your problem...
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
System.out.println("Buffer : " + buffer.toString());

Transfering Base64 String over Http REST service with JSON response

I have a webservice that returnds a json response , the json response contains both plain text and base64 encoded images , I am consuming that service using android app so I implemented progress bar to indicate the progress .
Implementing progress bar forces me to use BufferedInputStream to read the response and update the progress based on what the app is reading .
The problem is that everything is working fine and the progress is updating correctly, but after collecting the response and exiting the while loop , I try to convert the string into json format using JSONObject.
Here is the code snippet
BufferedInputStream bis = new BufferedInputStream(responseEntity.getContent());
StringBuilder sb = new StringBuilder();
String line = null;
int total = 0 ;
int count = 0 ;
byte[] buffer = new byte[4096];
StringBuffer sBuffer = new StringBuffer();
StringWriter sw = new StringWriter();
String content = new String();
while((count = bis.read(buffer)) > 0){
content += new String(buffer,Charset.defaultCharset());
total += count;
publishProgress(""+(int )total*100/this.contentSize);
Log.i("updating",""+(int )total*100/this.contentSize);
}
bis.close();
// String content = new String(sb);
// Log.i("ServerRawresponse",content);
try {
Log.i("REsponse_Content",content.replaceAll("\"", ""));
responseString = new JSONObject(new JSONTokener(content.replaceAll("\"", "\\\"")));
//System.out.println(content);
} catch (JSONException e) {
e.printStackTrace();
}
Any help please
Try this methods works perfectly with me
HttpResponse WSresponse = httpclient.execute(httppost);
String response = getResponseBody(WSresponse.getEntity());
JSONObject jobj = new JSONObject(response);
public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
System.out.println("GEN START : " + Calendar.getInstance().getTimeInMillis());
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
return buffer.toString();
}

How to simply output a binary file in Perl and read it from Android (Java)?

What am I missing in my code? Maybe headers (I tried a lot of them). The picture is received on the client side, but it cannot read it (meaning it must have been corrupted, either information is added or subtracted).
The server side is:
my $file = "<The path of the file>";
my $length = (stat($file)) [10];
print "Content-type: image/jpg\n";
print "Content-length: $length \n\n";
#open FH,"$file";
#binmode STDOUT;
#while(<FH>){ print }
#close FH;
binmode STDOUT;
open my $file_s,'<', $file || die "Could not open $file: $!";
my $buffer = "";
while (read($file_s, $buffer, 1024)) {
print $buffer;
}
close($file_s);
The Android side is:
String filename = Environment.getExternalStorageDirectory().getPath() + "/somename.jpg";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("<some url>");
StringBuilder response = new StringBuilder();
Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
try {
HttpResponse httpResponse = httpclient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity messageEntity = httpResponse.getEntity();
InputStream is = messageEntity.getContent();
long filesize = httpResponse.getEntity().getContentLength();
FileOutputStream fileOutput = new FileOutputStream(new File(filename));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer, 0, 1024)) > 0) {
fileOutput.write(buffer, 0, len);
}
fileOutput.close();
}
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The right code for Perl is:
my $file = "<The path of the file>";
my $length = (stat($file)) [10];
print "Content-type: application/binary\n";
print "Content-length: $length \n\n";
open FH,"$file";
binmode STDOUT;
while(<FH>){
print
}
close FH;

Android JSON receive issue

I have a string problem, I used the below code for recieving JSON data from an URL, the code is working fine, but the problem is I am not getting full data only half of the JSON values are coming, I would like to know whether there is reason for this, if so means how to solve this problem. JSON string is very big
DefaultHttpClient http_client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
HttpResponse response = http_client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
int n = in.read(b);
while(n>0){
out.append(new String(b, 0, n));
n = in.read(b);
}
String resultdata = out.toString();
Log.d("Out data",resultdata);
Try getting data like this using BufferedReader
String line="";
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
// Read response
while ((line = rd.readLine()) != null) {
total.append(line);
}
String jsonString=total.toString();
InputStream is = entity.getContent();
BufferedReader out = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = out.readLine()) != null) {sb.append(line + "\n");
}
is.close();
String json = sb.toString();
try this
Please try this,
public static JSONObject getJson(String url){
InputStream is = null;
String result = "";
JSONObject jsonObject = null;
// HTTP
try {
HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch(Exception e) {
return null;
}
// Read response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),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) {
return null;
}
// Convert string to object
try {
jsonObject = new JSONObject(result);
} catch(JSONException e) {
return null;
}
return jsonObject;
}}
you have to remove this line
int n = in.read(b);
and add
int n=0;
while ((n= in.read(b)) != null)

Categories