2011-09-30 69 views
1

我发送JSON数据这样错误虽然与Base64编码的图像串发送JSON数据到服务器在我的代码

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); 

但在执行HttpClient的

我得到这个错误
09-30 11:56:21.571: INFO/Request(22161): [email protected] 
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> 

请如果任何人都可以理解这一点,并告诉我这里发生了什么事情..哪里是我犯了什么错误的可能性..以及为什么我得到呃ROR?

谢谢。 MKJParekh

回答

1

我得到的答案... 我没有做任何那样的失误

,这是与org.json问题

我切换到another..and所有它的工作原理

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

+0

嗨,我面临类似的问题,你可以告诉我你做了什么来解决这个问题>你有什么其他库切换到 – user2586488

+1

检查此@ user2586488 http://stackoverflow.com/questions/338586/a-better-java-json-library – MKJParekh

0

查看您的代码,您正在使用enimg将图像数据添加到您的JSON中,而不是encodedImage。如果这是在你的问题中删除&粘贴错误,那么我怀疑你的Base64编码可能存在问题。您是否尝试过使用android.util.Base64encodeToString()方法?

即使您需要支持早于8的API版本,这也可以证明它是否是Base64错误,并且一旦您确定自己将知道需要修复的内容。

+0

是的,这是打字错误..我使用encodedImage – MKJParekh

0

不要通过json传递文件/图像。如果文件/图像很大,则会出现“内存不足”错误。

相关问题