gpt4 book ai didi

java - 无法在android中通过JSON将base64字符串图像发送到asp.net web服务

转载 作者:行者123 更新时间:2023-12-01 04:45:36 26 4
gpt4 key购买 nike

我在 ASP.net 中创建了一个 RESTful 的 Web 服务。我想从 Android 应用程序使用这些服务。基本上我使用 Android 的默认库发送请求并获取 JSON 格式的响应。

这是我的代码:

public class PostImagesHandler {
final static String TAG = "WebHandlerHttpPost";
Context context;
ImagesBean imageBean;

public String postImagesRequest(String URL, Context context,
ImagesBean imgBean) {
this.context = context;
String result = null;
this.imageBean = imgBean;

try {
HttpPost request = new HttpPost(URL);
System.out.println("WHAT IS URL :: " + URL);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
request.addHeader("Pragma", "no-cache"); // HTTP 1.0.
request.addHeader("Expires", "0"); // Proxies.


Bitmap bm = BitmapFactory.decodeFile("sdcard/dotTest9.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, baos);
byte[] bytes_ = baos.toByteArray();

String image_str = Base64.encodeToString(readFile().getBytes(),
Base64.NO_WRAP);
appendLog(image_str);

JSONStringer userJson = new JSONStringer().object()
.key("objPostSectionImages").object().key("ID")
.value("1").key("SUB_ID").value("1").key("MainTaskId")
.value("1").key("ImageBase64").value(image_str)
.key("actualAuditDate").value(getCurrentDateTime()).endObject()
endObject();


Log.d(TAG, "SECTION IMAGE STRING :: " + userJson.toString());

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

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 30000);
HttpConnectionParams.setSoTimeout(params, 10000);

// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);

if (response != null) {
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));

String line;
while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}

result = sb.toString();
Log.d(TAG, "AUDIT SECTION IMAGES Response: " + result);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public void appendLog(String text) {
File logFile = new File("sdcard/dotTest7.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
// BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(text);
// buf.newLine();
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@SuppressLint("SimpleDateFormat")
public static String getCurrentDateTime() {
return new SimpleDateFormat("MM-dd-yyyy").format(Calendar.getInstance()
.getTime());
}
}

我可以通过这种方式发送小图像,但在发送大图像(超过 5 KB)时,我遇到了问题。有时我会收到“请求错误”,有时它会起作用。

最佳答案

也许您的 ASP.NET Web 服务会转义 base64 字符串中的正斜杠?这使得该数据从正常应用程序的角度来看被破坏了。

在小图像中,base64 转换后获得 / 符号的概率明显低于大图像,因此您会遇到这种随机错误情况。

您可以检查服务结果字符串中是否有 /\ 符号,并将其替换为 / 并检查它是否有效。

关于java - 无法在android中通过JSON将base64字符串图像发送到asp.net web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15927235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com