Java 通过Android AsyncTask发布多部分表单数据
我的问题是writeArgsToConn()函数……我想。我想不出如何实施它 我正在尝试使用AsyncTask类从Android设备发布多部分formdata。有人能帮忙吗?我想远离被贬低的org.apache.http.legacy,坚持使用最新的Android库 我能够为一个名为DoPostJSON的类使用类似的实现,该类使用内容类型:application/json,该类运行良好 同样的问题,但关于Reddit: 我在让nodejs express服务器检测发送的参数时遇到问题。我的DoPostJSON类工作正常,我的nodejs服务器能够检测参数……由于某种原因,DoPostMultiPart无法工作,nodejs服务器无法看到传入的参数。我觉得我用错了图书馆Java 通过Android AsyncTask发布多部分表单数据,java,android,android-asynctask,http-post,multipartform-data,Java,Android,Android Asynctask,Http Post,Multipartform Data,我的问题是writeArgsToConn()函数……我想。我想不出如何实施它 我正在尝试使用AsyncTask类从Android设备发布多部分formdata。有人能帮忙吗?我想远离被贬低的org.apache.http.legacy,坚持使用最新的Android库 我能够为一个名为DoPostJSON的类使用类似的实现,该类使用内容类型:application/json,该类运行良好 同样的问题,但关于Reddit: 我在让nodejs express服务器检测发送的参数时遇到问题。我的DoP
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;
}
}
}
公共类DoPostMultiPart扩展AsyncTask实现Post{
@凌驾
公共HttpURLConnection createConn(字符串操作)引发异常{
URL=新URL(Utils.host\u api+操作);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
conn.setRequestProperty(“连接”、“保持活动”);
conn.setRequestProperty(“缓存控制”、“无缓存”);
连接设置读取超时(35000);
连接设置连接超时(35000);
返回连接;
}
@凌驾
公共JSONObject getResponse(HttpURLConnection conn)引发异常{
int responseCode=conn.getResponseCode();
字符串响应=”;
if(responseCode==HttpsURLConnection.HTTP\u确定){
InputStream in=新的BufferedInputStream(conn.getInputStream());
BufferedReader responseStreamReader=新的BufferedReader(新的InputStreamReader(in));
字符串行=”;
StringBuilder StringBuilder=新的StringBuilder();
而((line=responseStreamReader.readLine())!=null)
stringBuilder.append(行).append(“\n”);
responseStreamReader.close();
response=stringBuilder.toString();
}否则{
抛出新异常(“响应代码:“+responseCode”);
}
连接断开();
返回新的JSONObject(响应);
}
//TODO:修复此函数
@凌驾
public void writeArgsToConn(JSONObject args,HttpURLConnection conn)引发异常{
//定义参数
字符串fullname=args.getString(“fullname”);
String email=args.getString(“电子邮件”);
字符串密码=args.getString(“密码”);
字符串confpassword=args.getString(“confpassword”);
位图pic=(位图)args.get(“pic”);
//在请求中插入参数
OutputStream os=conn.getOutputStream();
//如何插入字符串参数???
pic.compress(Bitmap.CompressFormat.JPEG,100,os);//这对吗???
os.flush();
os.close();
}
@凌驾
受保护的JSONObject doInBackground(JSONObject…参数){
JSONObject args=params[0];
试一试{
String action=args.getString(“action”);
HttpURLConnection conn=createConn(操作);
writeArgsToConn(args,conn);
返回getResponse(conn);
}捕获(例外e){
Utils.logStackTrace(e);
返回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;
}
我使用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;
}
使用一些http库来帮助您实现这一点。写自定义json http的东西太浪费时间了。可能是重复的,我对Android http请求的东西没有经验。我想知道人们是否能为我指明去哪个图书馆的正确方向。我不太清楚到底是什么。我看了那篇文章,但我在让服务器看到传入的参数时遇到了问题。那篇文章对我一点帮助都没有。使用一些http库来帮助你。写自定义json http的东西太浪费时间了。可能是重复的,我对Android http请求的东西没有经验。我想知道人们是否能为我指明去哪个图书馆的正确方向。我不太清楚到底是什么。我看了那篇文章,但我在让服务器看到传入的参数时遇到了问题。那篇文章对我毫无帮助。