How to send Headers in Android - android

Thanks to all of you in advance :)
I want to send Headers in android to my shoutcast server.
I need to connect this address:
http://s7.voscast.com:8234
and get response. I response is
GET / HTTP/1.(0|1)
Then again send header pass=somePassword
And I want to check what will be the response for pass?

assuming that you are using import org.apache.http.client.*
it would be a simple:
HttpGet request = new HttpGet(url);
request.setHeader("X-AUTHORIZATION", token);
There are also methods like setEntity:
HttpPost request = new HttpPost(url);
request.addHeader("X-AUTHORIZATION", token);
request.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jsonPost);
request.setEntity(se);
see: http://developer.android.com/reference/org/apache/http/client/HttpClient.html

Related

to send values to json post method

I'm creating an android application in which I would like to send some values to the server and in response the server gives me a json response. The values I pass through is of Json post method type.
I would like to know how can I pass the following values to the Json post url:
{
"CPSProfileInfo":
{
"cpsName": "auto",
"userId": "0",
"cpsAddress": "0#0India",
"searchLimit": "10#0",
"searchFlag": "0"
}
}
throught my application so far, I've been using
params.put("cpsName","auto");
But I don't know how to proceed with this type as I don't know how to send the value for CPSProfileInfo
Kindly help me solving this issue!!!
You can go through the tutorial android-json-parsing-tutorial...
First know how json format looks!
To send Json to server, you can try my code:
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
HttpClient httpclient = new DefaultHttpClient(myParams);
HttpPost httppost = new HttpPost("*your server addresss and query*");
httppost.setHeader("Content-type", "application/json");
//if needed for authorization:
httppost.setHeader("Authorization", "Bearer " + "*Your string token retrieved from server*");
StringEntity se = new StringEntity("*your json here*");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
If you have any questions, please ask.
Checkout Retrofit, it is an awesome networking library by Square.
When posting in retrofit you can just create a class for the object:
#POST("/users/new")
void createUser(#Body User user, Callback<User> cb);
This is posting to the url 'yourdefinedapi.com/users/new with an User object, which is just a POJO object :)
See detailed instructions here: http://square.github.io/retrofit/

Java Equivalent of curl query

I am trying to fetch current user info after making him log into his box, using the box sdk for android. In the box api documentation, they have mentioned everything using curls. I am not familiar with curl. So, can anyone please give me a java equivalent for this curl operation :
curl https://api.box.com/2.0/users/me-H "Authorization: Bearer ACCESS_TOKEN".
I have the users access token.So, please give me a java equivalent for the above curl operation.
You can use java HttpClient
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
httpPost.setHeader("Authorization", "Bearer ACCESS_TOKEN"); // add headers if needded
//set params
BasicNameValuePair[] params = new BasicNameValuePair[] {new BasicNameValuePair("param1","param value"),
new BasicNameValuePair("param2","param value")};
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( Arrays.asList(params), "utf-8");
httpPost.setEntity(urlEncodedFormEntity);
//execute request
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity); //here response in string you can parse it
The HttpClient object that the other answer proposes is now deprecated. I solved a CURL problem like yours (with the extra difficulty of uploading a .wav file). Check out my code in this this answer/question. How to upload a WAV file using URLConnection

Android HttpClient DELETE method with request body

I'm attempting to use an HttpClient DELETE method in order to delete an item from a list.I want to send the relevant item_id using request body.I am using the following way in order to send the data.
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient = HttpUtils.getNewHttpClient();
HttpDelete httpPostRequest = new HttpDelete(URL);
**httpPostRequest.setHeader("item_id",id);**
httpPostRequest.addHeader("Authorization", getB64Auth(username,password));
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
httpPostRequest.setHeader("Accept-Encoding", "gzip");
But i am unable to delete the item into the server database. How to user Request body in HttpDelete?
According to the Spec of HTTP/1.1 you cannot send a entity body with anything but POST and PUT.
Use a request parameter or an header attribute. You can use the URI Builder:
URI myURI = android.net.Uri.Builder.path(myPathString).query("item_id=1").build();
Based on the answer over here that should give you a DELETE request with an entity field. Once you've made your own request type you could then do;
List<NameValuePair> deleteParams = new ArrayList<>();
deleteParams.add(new BasicNameValuePair("item_id", id));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(deleteParams);
HttpDeleteWithBody deleteRequest = new HttpDeleteWithBody(URL);
deleteRequest.addHeader("Authorization", getB64Auth(username,password));
deleteRequest.setHeader("Accept", "application/json");
deleteRequest.setHeader("Content-type", "application/json");
deleteRequest.setHeader("Accept-Encoding", "gzip");
deleteRequest.setEntity(entity);

Error after submitting JSON data through POST

I'm trying to send data through POST to a web server.
Here is the code I'm using to perform the network call:
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
post.setEntity(se);
HttpResponse response = client.execute(post);
I've verified using jsonlint that json.toString() returns a valid JSON.
I'm getting this error message as a response:
11-29 11:03:53.080: I/Storefront(1470): Response = <HTML><HEAD><TITLE>Service Unavailable</TITLE></HEAD><BODY><H1>Service Unavailable - Zero size object</H1>The server is temporarily unable to service your request. Please try againlater.<P>Reference #15.163431d0.1322593433.2b040d2c</BODY></HTML>
The "Zero size object" message makes me think that the JSON data is not being sent properly.
Because you are receiving a valid response, it is probably an issue with how you are using the server. Make sure you are using the correct URL and sending the data in the expected manner.

application/soap+msbin1 in android

I just need to send request to webservice via normal HTTP POST inorder to get response.I passed required parameter on body well.While i run it.,i got "Cannot process the message because the content type 'text/json' was not the expected type 'application/soap+msbin1'." error.When i made research over this.,due to "Web Service required the request to have a specific Content-Type, namely "application/soap+msbin1".When i replaced expected content type.,i got Bad Request error.I donno how to recover from that.
My code:
...
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("My URL");
postMethod.setHeader( "Content-Type", "text/json");
postMethod.setHeader( "Cache-Control", "no-cache");
JSONObject json = new JSONObject();
json.put("userName", "My Username");
json.put("password", "My Password");
json.put("isPersistent",false);
postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
HttpResponse response = httpClient.execute(postMethod);
...
It looks like you are trying to call WCF SOAP service. That service expects correct SOAP communication (= no JSON) and moreover it uses MS binary message encoding of SOAP messages (that is what content type describes) is not interoperable so I doubt you will be able to use it on Android device (unless you find implementation of that encoding for Java / Android).
HttpPost request = new HttpPost(url);
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setHeader("Accept", "application/json");
request.setEntity(entity);
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
response = httpClient.execute(request);
}
Try using something like this. it worked for me.
Thanks.
N_JOY.

Categories

Resources