How to send encoded Base64 image string to server using JSON object - android

I am new in android . i have send image to server using Json format as a string.
so i have Encoded image string i.e base64 string.
Json j = new JSonObject();
String Image_string = Base64.ToEncodedString(bytearray, Base64.Default);
j.put("image_file_content",Image_string);
But i am getting this error.
Please help me..wts wrong
400 Bad Request
Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.

Consider using URLEncoder
URLEncoder.encode("String to encode", "UTF-8");
Check out the docs here: http://developer.android.com/reference/java/net/URLEncoder.html

Related

Getting Server Error when Posting empty String in Volley

In the post request I am posting some parameter to the server as a request body . But unable to post empty string . So the question is that can we make post request with empty string if yes then how?
My code snippet is ..
HashMap<String, String> paramList = new HashMap<>();
paramList.put("ApplicationCode", Constant.Application_Code);
paramList.put("LoginType", Constant.getLoginType(spnLoginType.getSelectedItem().toString()));
paramList.put("BrandCode", Constant.BRAND);
paramList.put("CountryCode", Constant.CountryCode);
paramList.put("CompanyId", etCompanyId.getText().toString().toUpperCase());
paramList.put("UserId", etUserId.getText().toString().toUpperCase());
paramList.put("AppVersion", Constant.API_VERSION_VALUE);
paramList.put("IpAddress", "");
paramList.put("IpAddress", ""); here I am posting empty String that
does not work
And if I post like this paramList.put("IpAddress", "null"); worked
fine.
In the server-side validation, information is being sent to the server
and validated using one of server-side languages. If the validation
fails, the response is then sent back to the client .
But when send as a String does not worked
Why ?
Sloppy mistake coming from server side. Rectify your Server-side Validation .

Retrofit 500 internal server error

I have a post registration webserivce as the following
#FormUrlEncoded
#POST("register/default")
Observable<BaseResponse> doRegistration(#Field("name") String name,#Field(value = "email",encoded = true) String email,#Field("phone_number") String phone_number,#Field("password") String password,#Field("password_confirmation") String password_confirmation);
I've tried with postman to consume this service , and it replies with the response of registration , from android side it causes an exception (500 internal server error ) ,
when and only using a symbol # on the email field , also i've tried the encode option flag mentioned above, UTF-8 encoding but it doesn't interpreted as an email by server side.
is there is a retrofit related issue !
It was a server related issue on creating new record so larvae returns the default exception response with internal server error.

Sending a post in android with bad encoding

I'm sending post content to a server from android.
The problem is that the data at the server arrives wrong, with encoding problems, for example "{" arrives as "%7B%".
This is the code from android:
RequestParams params = new RequestParams();
params.put("alta", "{s}");
String ruta = "http://www.something.com/receive";
client.post(ruta, params,
new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
}
}
The server part is just receiving this data, like:
$data = $this->request->data;
$data =file_get_contents('php://input');
This issue is not directly related to text encoding per se.
As can be seen from the docs for RequestParams, text values are directly included in the url. As all text that is included in URLs has to be encoded to only include characters that are allowed in URLs (ASCII), text is url encoded.
AsyncHttpClient automatically does that encoding in the background, so you receive the strings in encoded form on the php side.
In order to get the original text you sent, you can use the rawurldecode() or urldecode() function on the php side to decode the encoded string you receive.
You need to use URLEncoder.encode(...) the the data part of you request.
At the server URL decode it.
You should be fine.

how to get the information from a http head in android

i've been given the task to send a POST message to the server giving it a JSON encoded message. The server would then send back a responce in a custom HTTP header field “X-SubmissionResponse”
so far i can successfully connect to the server (i know this because i get the responce code 202)
but i am having a lot of difficulty in getting the information from the responce, below is the code that i am currently using.
Error content not available
This code ends up returning null, Can anyone see what i am missing here?
This is the code above the if statement ^
Error content not available
Header name = response.getFirstHeader("X-SubmissionResponse");
String whatsInhere = "";
if (name != null)
whatsInhere = name.getValue();
Try using the correct methods of the Class Header.
See http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/Header.html
HttpHead head = new HttpHead();
creates a new HEAD request, empty, that does not do anything in itself.
You want the header from the response to your request. Get it by simply:
Header name = response.getFirstHeader("X-SubmissionResponse");

Google-api-client for Multipart POST request

I use google-api-client for android. I try to do multipart POST request with text data and image file. Code snippet for creating request is below:
InputStream stream = new FileInputStream(fileToSend);
InputStreamContent photoContent = new InputStreamContent("image/jpeg", stream);
MultipartRelatedContent multiContent =
new MultipartRelatedContent(content, photoContent);
HttpRequest request = getRequestFactory().buildPostRequest(googleUrl, multiContent);
content is key-value text content. As a result I get error 500.
What I'm doing wrong?
There is a guide here about how to do media upload with the google-api-java-client here:
https://code.google.com/p/google-api-java-client/wiki/MediaUpload
That said, I don't anything necessarily wrong with your code either. It is possible that the googleUrl is incorrect, or that content is not properly formatted. You might want to try adding a URL query parameter uploadType=multipart to specify that you are using multipart as the protocol.

Categories

Resources