gpt4 book ai didi

java - 通过 Android AsyncTask 发布多部分表单数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:12 24 4
gpt4 key购买 nike

我的问题是 writeArgsToConn() 函数......我认为。我不知道如何实现它。

我正在尝试使用 AsyncTask 类从 Android 设备发布多部分表单数据。有人能帮忙吗?我想远离那些已经贬值的 org.apache.http.legacy 东西并坚持使用最新的 Android 库。

我能够对名为 DoPostJSON 的类使用类似的实现,该类使用 Content-Type: application/json 并且该类工作正常。

同样的问题,但在 Reddit 上:https://redd.it/49qqyq

我在让 NodeJS Express 服务器检测发送的参数时遇到问题。我的 DoPostJSON 类工作正常,我的 NodeJS 服务器能够检测参数...由于某种原因,DoPostMultiPart 不起作用,NodeJS 服务器无法看到传入的参数。我觉得我使用该库的方式错误。

public class DoPostMultiPart extends AsyncTask<JSONObject, Void, JSONObject> implements Post{

@Override
public HttpURLConnection createConn(String action) throws Exception{
URL url = new URL(Utils.host_api + action);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

conn.setDoInput(true);
conn.setDoOutput(true);

conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cache-Control", "no-cache");

conn.setReadTimeout(35000);
conn.setConnectTimeout(35000);
return conn;
}

@Override
public JSONObject getResponse(HttpURLConnection conn) throws Exception {
int responseCode = conn.getResponseCode();
String response = "";
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(in));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null)
stringBuilder.append(line).append("\n");
responseStreamReader.close();
response = stringBuilder.toString();
} else {
throw new Exception("response code: " + responseCode);
}
conn.disconnect();
return new JSONObject(response);
}

// TODO: fix this function
@Override
public void writeArgsToConn(JSONObject args, HttpURLConnection conn) throws Exception {
// define paramaters
String fullname = args.getString("fullname");
String email = args.getString("email");
String password = args.getString("password");
String confpassword = args.getString("confpassword");
Bitmap pic = (Bitmap) args.get("pic");

// plugin paramters into request
OutputStream os = conn.getOutputStream();
// how do I plugin the String paramters???
pic.compress(Bitmap.CompressFormat.JPEG, 100, os); // is this right???
os.flush();
os.close();
}

@Override
protected JSONObject doInBackground(JSONObject... params) {
JSONObject args = params[0];
try {
String action = args.getString("action");
HttpURLConnection conn = createConn(action);
writeArgsToConn(args, conn);
return getResponse(conn);
} catch (Exception e) {
Utils.logStackTrace(e);
return null;
}
}
}

最佳答案

我通过使用 OkHttpClient 库解决了我的问题。

JSONObject args = params[0];
try
{
final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("fullname", args.getString("fullname"))
.addFormDataPart("email", args.getString("email"))
.addFormDataPart("password", args.getString("password"))
.addFormDataPart("confpassword", args.getString("confpassword"))
.addFormDataPart("pic", "profile.png", RequestBody.create(MEDIA_TYPE_PNG, (File) args.get("pic")))
.build();

Request request = new Request.Builder()
.url(Utils.host_api + args.getString("action"))
.post(requestBody)
.build();

OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
return new JSONObject(response.body().string());
}
catch (Exception e)
{
Utils.logStackTrace(e);
return null;
}

关于java - 通过 Android AsyncTask 发布多部分表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35904710/

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