开发者

Error While sending Json data to server with Base64 encoded image string

开发者 https://www.devze.com 2023-04-09 06:04 出处:网络
in my code i am sending the json data like this ByteArrayOutputStream baos = new ByteArrayOutputStream();

in my code i am sending the json data like this

ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        imgbmp.compress(Bitmap.CompressFormat.PNG, 90, baos);
                        byte[] b = baos.toByteArray();
                        String encodedImage = Base64.encodeBytes(b);
                        jsonstr = new JSONStringer().object().key("Text")
                                .value(msg).key("Files").array().object().key(
                                        "ContentType").value("image/png").key(
                                        "Content").value(encodedImage)
                                .endObject().endArray().key("AuthToken").value(token)
                                .endObject();

StringEntity entity = new StringEntity(jsonstr.toString());
                    entity.setContentType("application/json;charset=UTF-8");
                    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
                    request.setEntity(entity);
                    DefaultHttpClient httpClient = new DefaultHttpClient();

                    HttpResponse res = httpClient.execute(request);

and i am getting this error while executing the HttpClient

09-30 11:56:21.571: INFO/Request(22161): org.apache.http.clien开发者_开发技巧t.methods.HttpPost@468613b0
09-30 11:56:21.586: INFO/JSON ANS(22161): <Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"><Code><Value>Receiver</Value><Subcode><Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</Value></Subcode></Code><Reason><Text xml:lang="en-US">The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. </Text></Reason><Detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException i:nil="true"/><Message>The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. </Message><StackTrace>   at System.Convert.FromBase64String(String s)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at Zoodig.Core.WebServices.DataContracts.FileContract.GetStream()&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at Zoodig.WebServices.Services.AthletesService.PostUpdate(PostUpdateContract data)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at SyncInvokePostUpdate(Object , Object[] , Object[] )&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)&#xD;
09-30 11:56:21.586: INFO/JSON ANS(22161):    at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.FormatException</Type></ExceptionDetail></Detail></Fault>

Please if anyone can understand this and tell me whats happening here..where are the posibilities that i have made any mistakes..and why i am getting the error?

Thank You. MKJParekh


i got the answer... i was not making any kind of mistakes

that was problem with org.json

i switched to another..and all it works

nesting too deep in JSON... should I switch to XML?


Looking at your code, you are using enimg to add the image data to your JSON and not encodedImage. If this is a cut & paste error in your question, then I suspect that there may be a problem in your Base64 encoding. Have you tried using the encodeToString() method of android.util.Base64 instead?

Even if you need to support API versions earlier than 8, this will prove whether it's a Base64 error, and once you've determined that you'll know what you have to fix.


Don't pass files/images with json. You will get "out of memory" errors if files/images are big.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号