gpt4 book ai didi

android - 如何在 Android 中使用 HTTP POST 以 JSON 格式将图像作为 base64 字符串发送?

转载 作者:搜寻专家 更新时间:2023-11-01 08:41:25 25 4
gpt4 key购买 nike

我需要使用 HttpPost 将 json 请求发送到服务器。这是我当前的代码:

public static String makeRequest(String uri, String json) {
HttpURLConnection urlConnection;

String data = json;
String result = null;
try {
//Connect
urlConnection = (HttpURLConnection) ((new URL(uri).openConnection()));
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();

//Write
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(data);
writer.close();
outputStream.close();

//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));

String line = null;
StringBuilder sb = new StringBuilder();

while ((line = bufferedReader.readLine()) != null) {
System.out.println("Uploading............");
sb.append(line);
}

bufferedReader.close();
result = sb.toString();
System.out.println("Response : " +result);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

但它给了我 400,这意味着错误的请求。

注意:当 base64 路径很小时,只有它有效,否则无效。请帮助我。

最佳答案

请使用此代码:

try {

HttpURLConnection urlConnection;


String result = "";
try {
String data ="";
data = jsonObj.toString();
// String temp=URLEncoder.encode(uri, "UTF-8");
URL url = new URL(WebServiceConstants.getMethodUrl(WebServiceConstants.METHOD_UPDATEVENDER));
urlConnection = (HttpURLConnection) ((url.openConnection()));
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setChunkedStreamingMode(1024);

urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();

//Write
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(data);
writer.close();
outputStream.close();

//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));

String line = null;
StringBuilder sb = new StringBuilder();

while ((line = bufferedReader.readLine()) != null) {
System.out.println("Uploading............");
sb.append(line);
}

bufferedReader.close();
_responseMain = sb.toString();
System.out.println("Response of Image Upload : " +_responseMain);


} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// makeRequest(WebServiceConstants.getMethodUrl(WebServiceConstants.METHOD_UPDATEVENDER), jsonObj.toString());
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();

runOnUiThread( new Runnable() {
public void run()
{
Constant.showAlertDialog("Message",getResources().getString(R.string.communicationError), VendorEditProfile.this, false);
}
});

}

关于android - 如何在 Android 中使用 HTTP POST 以 JSON 格式将图像作为 base64 字符串发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223789/

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