Get List<byte[]> from InputStream - java

What I'm trying to do is to get List<byte[]> from InputStream. I'm sending HTTP request to an API that returns List<byte[]> and I want to get that list from the content body.
HttpPost httpPost = new HttpPost(endPoint);
CloseableHttpClient httpclient = HttpClients.createDefault();
httpPost.setEntity(new ByteArrayEntity(ReqBody));
CloseableHttpResponse response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
System.out.println(response.getEntity().getContentType());
return response.getEntity().getContent();

Related

Unable to make http POST request in java?

static HttpClient httpclient = new DefaultHttpClient();
static HttpPost httppost = new HttpPost("http://servername:6405/biprws/logon/long");
public static void main(String[] args) throws ClientProtocolException, IOException {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("userName", "Administrator"));
postParameters.add(new BasicNameValuePair("password", "test"));
postParameters.add(new BasicNameValuePair("auth", "secEnterprise"));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
httppost.addHeader("accept", "application/json");
httppost.addHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httppost);
Header s = response.getFirstHeader("logontoken");
String s1 = s.getValue();
System.out.println(s1);// null pointer exception here
}
Running the code above i am not able to add request body to the POST request. How can i achieve this?
Alternative method i followed:
HttpClient client1 = new DefaultHttpClient();
HttpPost post = new HttpPost("http://servername:6405/biprws/logon/long");
String json = "{\"UserName\":\"Administrator\",\"Password\":\"test\",\"Auth\":\"secEnterprise\"}";
StringEntity entity = new StringEntity(json,"UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
System.out.println(entity);
post.setHeader("Accept", "application/json");
HttpResponse response = client1.execute(post);
BufferedReader rd1 = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String result1 = null;
String line1 = "";
result1 = rd1.readLine();
System.out.println(result1);
Still i am not able to make request.
You are successfully receiving a response which does not contain the "logontoken" header. Very possibly because the response is not an HTTP 200 OK response. Why? We don't know, it all depends on the protocol that your server implements on top of HTTP.
That having been said, the use of both httppost.setEntity(new UrlEncodedFromEntity(postParameters)) and httppost.addHeder("Content-Type", "application/json") does not look right to me. A URL-encoded form entity is not of json content type. So, either convert your post parameters to json, or lose the content-type header.

Remove or replace special character in namevaluepair for http post request android

I have a problem when I tried to post my data it logged as :
[ObserverTRID=5QEET3TE10,
ObsType=Evaluate,
ObsDate=17-Jul-2014,
ObsTime=09:22:12,
// == loc
ObsTitle=bolllloooo,
//==obstset
MediaData={"MediaData":[{"RunNo":0,"Filename":"http://url/5QEET3TE10_3_5_20140717092206.jpg","MediaNo":4"Desc":01 Image_17-Jul-14No Marker.jpg}]},
PVPMediaCount=1,
SourceDevice=motorola XT1032 Android v19 App v2.0001,
ObsDept,
ObsSite,
TargetTRID=5QEET3TE10]
But I need to append every namevalue with "&" instead of "," This is my httppost request.
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
This is my request i am posting the URL with the parameter nameevaluepair
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mUrl);
if(mNameValuePairs==null){
mNameValuePairs = new ArrayList<NameValuePair>(1);
mNameValuePairs.add(new BasicNameValuePair("PostEvent", "Null"));
}
Log.e("namevalue ", mNameValuePairs.toString());
There I am settig entity by URLEncoding
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
HttpEntity httpEntity = response.getEntity();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
use this code in for parsing class

How to resolve org.apache.http.conn.ConnectTimeoutException in Android?

I am working on service related application.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000000);
HttpConnectionParams.setSoTimeout(params, 15000000);
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(),true);
String str_Server_Host = Control_PWD_Server_Name();
String requestEnvelope = String.format(envelope_oe);
// HttpPost httpPost = new HttpPost(str_Server_Host);
HttpPost httpPost = new HttpPost(str_Server_Host);
httpPost.setHeader("SOAPAction",
"http://tempuri.org/updateTransportStatus");
httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");
httpPost.setEntity(new StringEntity(requestEnvelope));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
String str = Html.fromHtml(line).toString();
This is the exception I am getting:
org.apache.http.conn.ConnectTimeoutException: Connect to /10.64.0.184:9092 timed out
How to resolve above exception in android? please give any suggestion or related senario.

HttpPost - type of http request method

Hi :) I got a simple problem, but very annoying.
I'm trying to send http post request using HttpPost class
This is part of the method which returns InputStream:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url+path);
ArrayList<NameValuePair> paramsList = new ArrayList<NameValuePair>();
paramsList.add(new BasicNameValuePair("key1", value1));
paramsList.add(new BasicNameValuePair("key2", value2));
paramsList.add(new BasicNameValuePair("key3", value3));
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
return is;
But the problem is on the server, because server "thinks" that im sending GET request instead of POST. The question is: where is mistake? Im using similar part of code in other application and it works fine.
Cheers. :)
Please try
httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
Instead Of
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));

Adding parameter to HttpPost on Apache's httpclient

I am trying to set some Http parameters in the HttpPost object.
HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);
It looks like the parameter is not set at all. Do you have any idea why this is happening?
Thank you
For those who hopes to find the answer using HttpGet, here's one (from https://stackoverflow.com/a/4660576/330867) :
StringBuilder requestUrl = new StringBuilder("your_url");
String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());
NOTE: This doesn't take in consideration the state of your_url : if there is already some parameters, if it already contains a "?", etc. I assume you know how to code/search and will adapt regarding your case.
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);

Categories