ASIFormDataRequest POST操作问题

liubing198714 发布于 2013/10/17 16:42
阅读 1K+
收藏 0
iOS

这个是我向服务器发送请求的数据


NSString *strURL;
    if([[SingleGlobal sharedSingleton].strGlobalIsLoginSuccess isEqualToString:@"Success"])
    {
        strURL = [NSString stringWithFormat:ORDER_SUBMIT@"%@",[SingleGlobal sharedSingleton].strGlobalLoginedUserName];
    }
    else
    {
        //未登录,临时用户temp
        strURL = [NSString stringWithFormat:ORDER_SUBMIT@"temp"];
    }
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
    [request setRequestMethod:@"POST"];
    
    [request setPostValue:_fatherMenuID forKey:@"Category"];
    [request setPostValue:_remarkTextView.text forKey:@"Remark"];
    [request setPostValue:_consigneeTextView.text forKey:@"Consignee"];
    [request setPostValue:_phoneTextView.text forKey:@"Phone"];
    [request setPostValue:[SingleGlobal sharedSingleton].strGlobalShippingAddress  forKey:@"Address"];
    [request setPostValue:@"" forKey:@"Date"];
    [request setPostValue:@"" forKey:@"Hour"];
    [request setPostValue:@"" forKey:@"Min"];
    [request setPostValue:@"" forKey:@"AreaCode"];
    [request setPostValue:[NSNumber numberWithInt:_intUserPoints] forKey:@"Score"];
    [request setPostValue:_strReceiptTime forKey:@"Time"];
    for(int i = 0; i < _buyGoodsCartArray.count; i++)
    {
        NSMutableDictionary *buyGoodsCartDic = [[NSMutableDictionary alloc]init];
        NSDictionary *dic = [_buyGoodsCartArray objectAtIndex:i];
        [buyGoodsCartDic setObject:[dic objectForKey:@"Pid"] forKey:@"Pid"];
        [buyGoodsCartDic setObject:[dic objectForKey:@"Shop"] forKey:@"Shop"];
        [buyGoodsCartDic setObject:[dic objectForKey:@"ProCode"] forKey:@"Code"];
        [buyGoodsCartDic setObject:[dic objectForKey:@"Num"] forKey:@"Num"];
        [_goodsCartArray addObject:buyGoodsCartDic];
        [buyGoodsCartDic release];
    }
    [request setPostValue:_goodsCartArray forKey:@"Cart"];
    
    [request startSynchronous];
    
    NSError *error = [request error];
    
    if (!error)
    {
        //[request setResponseEncoding:NSUTF8StringEncoding];
        [request setDefaultResponseEncoding:NSUTF8StringEncoding];
        NSString *json = [request responseString];
        
        json = [json stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
    }
    else
    {
       
        NSLog(@"数据读取失败,请稍后重试!");
        
    }


调试的时候 返回的json数据打印出来是这样 呢  
 
 

Printing description of json:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
  <head>
    <title>请求错误</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">请求错误</p>
      <p xmlns="">æœåŠ¡å™¨å¤„ç†è¯·æ±‚æ—¶é‡åˆ°é”™è¯¯ã€‚æœ‰å…³æž„é€ æœ‰æ•ˆæœåŠ¡è¯·æ±‚çš„å†…å ¹ï¼Œè¯·å‚é˜…<a rel="help-page" href=" http://mobile.ings.cn/android/help">æœåŠ¡å¸ åŠ é¡µ</a>。异常消息为“Unexpected character encountered while parsing value: C. Path '', line 0, position 0.”。有关详细信息,请参阅服务器日志。异常堆栈跟踪为: </p>
      <p>   åœ¨ Newtonsoft.Json.JsonTextReader.ParseValue()
   åœ¨ Newtonsoft.Json.JsonTextReader.ReadInternal()
   åœ¨ Newtonsoft.Json.JsonTextReader.Read()
   åœ¨ Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
   åœ¨ Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   åœ¨ Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   åœ¨ Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   åœ¨ ClientService.IngsService.SaveCart(String UserName, Stream InputStream) ä½ç½  F:\å·¥ä½œç ºé—´\银联活动 E-Shop\WCFService\IngsService.svc.cs:行号 455
   åœ¨ SyncInvokeSaveCart(Object , Object[] , Object[] )
   åœ¨ System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
   åœ¨ System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   åœ¨ System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   åœ¨ System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
   åœ¨ System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
    </div>
  </body>
</html>
(lldb) 
 
请问这个问题该怎么解决呢   纠结一下午了呢  也不知道是哪个地方出现错误了呢
 
加载中
0
芳仔小脚丫
芳仔小脚丫

你这返回的不是json数据;顺便你的代码好混乱,这个提问时可以插入代码的,格式会好很多;

要得到json数据,请导入jsonKit,去网上找,添加进去之后,然后在你的

NSString *json = [request responseString]; //这里得到的responseString不是json格式,只是string
         
json = [json stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
在后面加上

NSString *json = [[request responseString] JSONValue]; //加上这个得到的应该就是JSON格式的数据
         
json = [json stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];


0
l
liubing198714

jsonkit 我导入了呢,最后我检查了 提交的JSON数据格式不太对呢 ,谢谢你了呢  

0
tfc
tfc
我看ios 都看迷糊了
0
helloapple
helloapple

我遇到了和你一樣的問題,你參數格式問題是怎麼解決的?可以幫我看看麼?謝謝。http://www.oschina.net/question/1451153_143606#AnchorAnswer689199

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部