请问如何在 android 改造中 post 数组对象 JSON 中的字符串
please how can I post String in array Object JSON in android retrofit
请问我如何post这个JSON
{
"name": "Premium Quality",
"type": "simple",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
}
]
}
到 android 改造
我已经成功发送了一个字符串,但是这个数组我不能
喜欢发送图片
我的代码
API
{
public interface interfaceAPI
@POST("wp-json/wc/v3/products?consumer_key=ck_006dd346d4c87128f924a93983e7d51be528d1&consumer_secret=cs_d4b421317da42bd58a3f3db6a4cc4644d20a78e3")
Call<Product> PRODUCT_CALL_POST(@Body HashMap<Object, Object> map);
}
CLASS POST
package com.example.myapplication;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.myapplication.Model.Product;
import com.example.myapplication.Model.images;
import com.example.myapplication.callAPI.interfaceAPI;
import com.example.myapplication.databinding.ActivityPostBinding;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class post extends AppCompatActivity {
ActivityPostBinding binding;
private final static int intent_get_img = 1;
String name= "name";
String regular_price= "regular_price";
String src = "src";
//String images = "images";
images images;
Uri uriimg;
interfaceAPI interfaceAPI ;
@Override
protected void onCreate(Bundle savedInstanceState) {
binding = ActivityPostBinding.inflate(getLayoutInflater());
super.onCreate(savedInstanceState);
setContentView(binding.getRoot());
binding.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent getimg = new Intent(Intent.ACTION_GET_CONTENT);
getimg.setType("image/*");
startActivityForResult(getimg, intent_get_img);
}
});
binding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
postproduct();
}
});
}
private void postproduct()
{
try {
JSONArray imagesA = new JSONArray();
JSONObject images =new JSONObject();
images.put("src", uriimg.toString());
imagesA.put(images);
HashMap<Object, Object> post_map =new HashMap<>();
post_map.put("name",binding.productName.getText().toString());
post_map.put("regular_price",binding.price.getText().toString());
// images =new images(uriimg.toString());
post_map.put("images",new Gson().toJson(imagesA));
//method
getPostCall(post_map);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getPostCall(HashMap<Object, Object> hashMap)
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://carddoom.com/shop/")
.addConverterFactory(GsonConverterFactory.create())
.build();
interfaceAPI = retrofit.create(com.example.myapplication.callAPI.interfaceAPI.class);
Call<Product> productCall= interfaceAPI.PRODUCT_CALL_POST(hashMap );
productCall.enqueue(new Callback<Product>() {
@Override
public void onResponse(Call<Product> call, Response<Product> response) {
if (response.isSuccessful()) {
Toast.makeText(post.this, "Successfully post Product", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(post.this, "API Error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Product> call, Throwable t) {
Toast.makeText(post.this, "API Erorr: "+ t.getMessage() , Toast.LENGTH_SHORT).show();
binding.button.setText(t.getMessage());
Log.getStackTraceString(t);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode== intent_get_img && data!= null && resultCode== Activity.RESULT_OK){
uriimg = data.getData();
Picasso.get().load(uriimg).into(binding.imageView);
}
}
}
我试过使用 JSONObject,但它不起作用
HashMap 部分解决了问题。
希望得到您的帮助
任何帮助将不胜感激:)
祝你好运
辛苦了几天,我的问题终于解决了:)
HashMap<Object,Object> map = new HashMap<>();
map.put( name ,"Premium Quality222" );
map.put( regular_price ,"21.99" );
images = new images("http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg");
ArrayList<images> imagesArrayList = new ArrayList<com.example.myapplication.Model.images>();
imagesArrayList.add(images);
map.put(imagesString , imagesArrayList);
ArrayList<categories> categoriesArrayList = new ArrayList<>();
categories = new categories(111);
categoriesArrayList.add(categories);
map.put(categoriesString, categoriesArrayList);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://carddoom.com/shop/")
.addConverterFactory(GsonConverterFactory.create())
.build();
interfaceAPI = retrofit.create(com.example.myapplication.callAPI.interfaceAPI.class);
Call<Product2> productCall= interfaceAPI.PRODUCT_CALL_POST(map);
请问我如何post这个JSON
{
"name": "Premium Quality",
"type": "simple",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
}
]
}
到 android 改造
我已经成功发送了一个字符串,但是这个数组我不能 喜欢发送图片
我的代码
API
{
public interface interfaceAPI
@POST("wp-json/wc/v3/products?consumer_key=ck_006dd346d4c87128f924a93983e7d51be528d1&consumer_secret=cs_d4b421317da42bd58a3f3db6a4cc4644d20a78e3")
Call<Product> PRODUCT_CALL_POST(@Body HashMap<Object, Object> map);
}
CLASS POST
package com.example.myapplication;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.myapplication.Model.Product;
import com.example.myapplication.Model.images;
import com.example.myapplication.callAPI.interfaceAPI;
import com.example.myapplication.databinding.ActivityPostBinding;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class post extends AppCompatActivity {
ActivityPostBinding binding;
private final static int intent_get_img = 1;
String name= "name";
String regular_price= "regular_price";
String src = "src";
//String images = "images";
images images;
Uri uriimg;
interfaceAPI interfaceAPI ;
@Override
protected void onCreate(Bundle savedInstanceState) {
binding = ActivityPostBinding.inflate(getLayoutInflater());
super.onCreate(savedInstanceState);
setContentView(binding.getRoot());
binding.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent getimg = new Intent(Intent.ACTION_GET_CONTENT);
getimg.setType("image/*");
startActivityForResult(getimg, intent_get_img);
}
});
binding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
postproduct();
}
});
}
private void postproduct()
{
try {
JSONArray imagesA = new JSONArray();
JSONObject images =new JSONObject();
images.put("src", uriimg.toString());
imagesA.put(images);
HashMap<Object, Object> post_map =new HashMap<>();
post_map.put("name",binding.productName.getText().toString());
post_map.put("regular_price",binding.price.getText().toString());
// images =new images(uriimg.toString());
post_map.put("images",new Gson().toJson(imagesA));
//method
getPostCall(post_map);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getPostCall(HashMap<Object, Object> hashMap)
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://carddoom.com/shop/")
.addConverterFactory(GsonConverterFactory.create())
.build();
interfaceAPI = retrofit.create(com.example.myapplication.callAPI.interfaceAPI.class);
Call<Product> productCall= interfaceAPI.PRODUCT_CALL_POST(hashMap );
productCall.enqueue(new Callback<Product>() {
@Override
public void onResponse(Call<Product> call, Response<Product> response) {
if (response.isSuccessful()) {
Toast.makeText(post.this, "Successfully post Product", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(post.this, "API Error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Product> call, Throwable t) {
Toast.makeText(post.this, "API Erorr: "+ t.getMessage() , Toast.LENGTH_SHORT).show();
binding.button.setText(t.getMessage());
Log.getStackTraceString(t);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode== intent_get_img && data!= null && resultCode== Activity.RESULT_OK){
uriimg = data.getData();
Picasso.get().load(uriimg).into(binding.imageView);
}
}
}
我试过使用 JSONObject,但它不起作用
HashMap 部分解决了问题。
希望得到您的帮助
任何帮助将不胜感激:) 祝你好运
辛苦了几天,我的问题终于解决了:)
HashMap<Object,Object> map = new HashMap<>();
map.put( name ,"Premium Quality222" );
map.put( regular_price ,"21.99" );
images = new images("http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg");
ArrayList<images> imagesArrayList = new ArrayList<com.example.myapplication.Model.images>();
imagesArrayList.add(images);
map.put(imagesString , imagesArrayList);
ArrayList<categories> categoriesArrayList = new ArrayList<>();
categories = new categories(111);
categoriesArrayList.add(categories);
map.put(categoriesString, categoriesArrayList);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://carddoom.com/shop/")
.addConverterFactory(GsonConverterFactory.create())
.build();
interfaceAPI = retrofit.create(com.example.myapplication.callAPI.interfaceAPI.class);
Call<Product2> productCall= interfaceAPI.PRODUCT_CALL_POST(map);