I'm getting the response like this:
HttpResponse response = httpclient.execute(httpget);
response.getStatusLine().toString());
In this case the response is:
HTTP/1.1 500 invalid user credentials
Ho do I get the message only, without the code?
**invalid user credentials**
Try this:
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
String messageOnly = statusLine.getReasonPhrase();
int codeOnly = statusLine.getStatusCode();
Simple, do this
String responseStr = EntityUtils.toString(response.getEntity());
This will work!
You could simply get the message like this
String message = response.getStatusLine().toString().substring(12);
Use HttpURLConnection's getResponseMessage() See this
try this :
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
and then convert this input stream into string......
Use getReasonPhrase() method of StatusLine interface.
e.g.
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
statusLine.getReasonPhrase()
Related
I used to post json stringer using the following code but having lot of deprecation warning.Can anyone help me out to show me the correct way for POSTing.Please have a look on my code that i'm using for posting currently.
HttpPost httppost = new HttpPost(F_URL);
System.out.println("URL...." + F_URL);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-Type", "application/json");
JSONStringer jsonStringer = new JSONStringer().object()
.key("putmicdata").object().key("CompanyID")
.value(companyid).key("ValueHeader")
.value(valueheader).key("ValueHeaderDetail")
.value(valueheaderdetail).endObject();
StringEntity entity = new StringEntity(
jsonStringer.toString());
System.out.println("String...."
+ jsonStringer.toString());
entity.setContentType(new BasicHeader(
HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(entity);
response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println("StatusCode for MIC" + statusCode);
if (response != null) {
HttpEntity httpEntity = response.getEntity();
total = EntityUtils.toString(httpEntity);
dbhelper.getWritableDatabase();
dbhelper.DELETE_MICUOMINTERNAL(loc);
dbhelper.closeDatabase();
}
result = "success";
you can use HttpUrlConnectionor if you want just opening a webaddress just use
new URL("youraddress.com").openStream();
I am hoping to get an answer to an issue I am having. I have 2 web service methods. The first is a login webmethod which returns a SessionID and the second is a getExpenseTypes which should return an array of expenses but instead tells me that the SessionID is invalid.
My Login Request
HttpPost post = new HttpPost(new URI(urlset.getUrl()));
post.setHeader("SOAPAction", urlset.getAction());
post.setHeader("Content-Type", urlset.getContentType());
post.setEntity(new StringEntity(login.getSoapLogin(args[0].trim(),
args[1].trim())));
HttpClient client = customHttpClient.getNewHttpClient();
HttpResponse response = client.execute(post);
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = stringManipulator.convertStreamToString(instream);
sessionID = parseSessionId.parseSessionId(result);
instream.close();
}
And it returns
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><LoginResponse xmlns="http://tempuri.org/"><LoginResult>8e576dc05aa74c699640bb12094e0bde</LoginResult></LoginResponse></s:Body></s:Envelope>
I assumed that 8e576dc05aa74c699640bb12094e0bde was the SessionID but later I found out that this is a GUID.
So the second webmethod
HttpPost post = new HttpPost(new URI(urlset.getUrl()));
post.setHeader("SOAPAction", "http://tempuri.org/IMobileService/GetExpenseTypes");
post.setHeader("Content-Type", urlset.getContentType());
Log.e("args[0]", args[0]);
String sentity = getExpenseType.getExpenseTypeTest(args[0]
.trim());
post.setEntity(new StringEntity(sentity));
CustomHttpClient customHttpClient = new CustomHttpClient();
HttpClient client = customHttpClient.getNewHttpClient();
HttpResponse response = client.execute(post);
statusCode = response.getStatusLine().getStatusCode();
if (entity != null) {
instream = entity.getContent();
result = stringManipulator.convertStreamToString(instream);
instream.close();
}
Where the stored session id is concatenated into a StringEntity like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetExpenseTypes>
<!--Optional:-->
<tem:sessionID>2b89eff0ebcf4fb8a11636256d94553e</tem:sessionID>
</tem:GetExpenseTypes>
</soapenv:Body>
</soapenv:Envelope>
and it returns:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetExpenseTypesResponse xmlns="http://tempuri.org/"><GetExpenseTypesResult>ERR: Session 00000000000000000000000000000000 won't be found.</GetExpenseTypesResult></GetExpenseTypesResponse></s:Body></s:Envelope>
Though I was expecting:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetExpenseTypesResponse xmlns="http://tempuri.org/">
<GetExpenseTypesResult>[{"Id":3,"ExpenseTypeName":"Accomodation","CustomerId":null},{"Id":4,"ExpenseTypeName":"Communication","CustomerId":null},{"Id":12,"ExpenseTypeName":"Entertainment","CustomerId":2},{"Id":1,"ExpenseTypeName":"Flight","CustomerId":null},{"Id":10,"ExpenseTypeName":"Fuel","CustomerId":2},{"Id":11,"ExpenseTypeName":"Meals","CustomerId":2},{"Id":2,"ExpenseTypeName":"Mileage","CustomerId":2},{"Id":13,"ExpenseTypeName":"Office Supplies","CustomerId":2},{"Id":6,"ExpenseTypeName":"Subsistence","CustomerId":null},{"Id":5,"ExpenseTypeName":"Transport","CustomerId":null}]</GetExpenseTypesResult>
</GetExpenseTypesResponse>
</s:Body>
</s:Envelope>
So, I have been told that it is because the SessionID is sent as a GUID but I have never used these before. I just plugged in what looked like a string as the session id. This is clearly wrong. How do I handle receiving the GUID instead of a session id?
Thanks.
I've used POST methods many times in the past but got this weird web service. It works fine on a chrome extension but i am unable to get this response using android . Please add anything in the code if missing
Here is the code i am using
HttpParams param = new BasicHttpParams();
param.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(param);
HttpPost httppost = new HttpPost(url);
Utils.showLogs(MyConstants.TAG, "ApplicationURL: " + url);
httppost.setHeader("Content-type", contentType);
HttpConnectionParams.setConnectionTimeout(param, MyConstants.WEBSERVICE_TIMEOUT);
HttpConnectionParams.setSoTimeout(param, MyConstants.WEBSERVICE_TIMEOUT);
String response = null;
try {
if (jsonObject == null) {
StringEntity entity = new StringEntity(json, HTTP.UTF_8);
httppost.setEntity(entity);
} else {
StringEntity stringEntity = new StringEntity(jsonObject.toString());
httppost.setEntity(stringEntity);
}
// Execute HTTP Post Request
HttpResponse httpResponse = httpclient.execute(httppost);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return MyConstants.SERVER_NOT_RESPONDING;
}
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Thanks
Did you add <uses-permission android:name="android.permission.INTERNET" /> in AndroidManifest.xml ?
If not, add this should be fine!
This question already has answers here:
Android HttpPost: how to get the result
(5 answers)
Closed 9 years ago.
I'm using
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
in order to send data to my webservice. How could I get the http response code (like 200, 404, 500, etc) ?
I'm not able to use getResponseCode() from HttpURLConnection
More of your code would be helpful here but the way I do it is this:
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = client.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode()
by
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int code = statusline.getStatusCode();
I would like to use HTTP post and get methods to retrieve images for a gallery view.
How would i go about doing this?
Do a search here for how to use HttpGet - there are lots of examples. But once you have a valid response you will need to use BitmapFactory.decodeStream to get a bitmap (something like the example code below) which you can draw into the ImageView.
HttpResponse response = HttpClient.execute(request);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpStatus.SC_OK)
{
InputStream inputStream = entity.getContent();
Bitmap rawBitmap = null;
BitmapFactory.Options bitmapOpt = new BitmapFactory.Options();
bitmapOpt.inDither = true;
bitmapOpt.inPurgeable = true;
bitmapOpt.inInputShareable = true;
bitmapOpt.inTempStorage = null;
rawBitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOpt);
}