我必须以这种形式发布一些来自android的数据
[
{
"title": "song's title",
"artists": "artist1, artist2,....",
"album": "album name",
"genre": "genre name",
"added_at": "datetime etc",
"counter": x (int value i.e. 3),
"cover_art": "image data"(File)
},
{
"title": "song's title",
"artists": "artist1, artist2,....",
"album": "album name",
"genre": "genre name",
"added_at": "datetime etc",
"counter": x (int value i.e. 3),
"cover_art": "image data"(File)
},
.......and so on.
]
我如何从改造中发布此信息。据我所知,人们可以在改造中上传图像为Multipart,但这里我有包含图像数据的json数组。 请提前帮助,谢谢。
答案 0 :(得分:0)
好的,所以我这样做了 - 我用这些字段创建了一个类 -
public class PostServerSongDM {
String title , artists , album , genre, added_at;
long counter;
byte[] cover_art;
public PostServerSongDM(String t , String ar , String al , String ge, String addat , long cou , String path){
title = t;
artists = ar;
album = al;
genre = ge;
added_at = addat;
counter = cou;
File image = new File(path.substring(7));
int size = (int) image.length();
cover_art = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(image));
buf.read(cover_art, 0, cover_art.length);
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
然后像这样创建一个 Retofit 界面 -
public interface PostSongList {
@POST("your api")
Call<SampleResponse> request(@Body ArrayList<PostServerSongDM> list);
}
并且通过这种方式,我能够使用精确的json数据格式向服务器发布请求。
P.S - 我不知道这是否是一个明显的解决方案,但我现在无法解决这个问题,现在我做到了。