在iOS中将BASE64加密图像作为字符串和其他信息发送

时间:2012-11-23 07:53:14

标签: iphone ios xamarin.ios

我使用Base64编码以String的形式存储图像。我希望将该图像发送到API并获取jSON对象响应。我使用ArrayList在Android中部署了相同的,但由于iOS没有ArrayList。怎么办呢。

ArrayList nameValuePairs = new ArrayList();

  nameValuePairs.add(new BasicNameValuePair("serialnumber",ReplaceString(UserData.objUserData.getencryptedTerminalid()).trim()));
  nameValuePairs.add(new BasicNameValuePair("mobileimei",ReplaceString(EncryptionHelper.encryptText(telephonyManager.getDeviceId())).trim()));
  nameValuePairs.add(new BasicNameValuePair("submerchantguid",ReplaceString(EncryptionHelper.encryptText(Payment.merchantId)).trim()));
  nameValuePairs.add(new BasicNameValuePair("transactionid",ReplaceString(EncryptionHelper.encryptText(Payment.transactionId))));
  nameValuePairs.add(new BasicNameValuePair("emailid",ReplaceString(EncryptionHelper.encryptText(userEmail)).trim()));
  nameValuePairs.add(new BasicNameValuePair("mobileno",ReplaceString(EncryptionHelper.encryptText(userMobile)).trim()));
  nameValuePairs.add(new BasicNameValuePair("signature",encodedSignImage));
  nameValuePairs.add(new BasicNameValuePair("photo",encodedPhotoImage.trim()));


  InputStream is;
  // open data output stream
  OutputStream dos;

  HttpClient httpclient = new DefaultHttpClient();

  HttpPost httppost = new

  HttpPost(Constants.INFO_SUBMIT);

  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

  HttpResponse response = httpclient.execute(httppost);

  HttpEntity entity = response.getEntity();

  is = entity.getContent();

  int ch;
  StringBuffer b = new StringBuffer();
  String responseString = "";

  while ((ch = is.read()) != -1) 
  {
   b.append((char) ch);
  }

  responseString = b.toString();
  JSONObject jsonObject = new JSONObject(responseString);
  statusString = (String) jsonObject.get("Status");

    }
 }

1 个答案:

答案 0 :(得分:0)

这里有三个单独的问题,我将逐一回答:

  1. 在与网络服务进行通信方面,您应该查看RestKitASIHTTPRequestAFNetworking或类似内容。如果您有兴趣自己编写,可以使用NSURLConnection中可以看到的{{1}}。您还可以查看URL Loading System Programming Guide中将会看到的各种示例,例如NSURLConnection Class Reference

  2. 在解析/创建JSON方面,您可以使用SimpleURLConnections

  3. 就base64编码而言,我已成功使用NSJSONSerialization。但我建议你看看Google GTMBase64 in GTM