我想使用 POST METHOD
将此json正文发送到服务器$ for f in $(find -type f -name *.mkv); do echo "$f"; done
./homeworks/2017-04-03
00-54-57
homework3b.mkv
./homeworks/2017-04-03
00-21-36
homework1.mkv
./homeworks/2017-04-03
我在网上尝试了很多可用的解决方案,但是大多数解决方案都告诉格式化字符串并发送到服务器。有没有正确的方法使用齐射将此正文发送到服务器端。请帮我。感谢。
答案 0 :(得分:5)
让我们从底部开始。首先,
JSONObject json1= new JSONObject();
json1.put("name","Host");
json1.put("value","google.com");
在此之后我们将上面的对象放在一个数组
中JSONArray jsonArray = new JSONArray();
jsonArray.put(json1);
现在我们将上面的数组和命令id添加到一个新对象
JSONObject json2= new JSONObject();
json2.put("command_id","165");
json2.put("arguments",jsonArray);
同样,这是命令数组的对象
JSONArray jsonArray2 = new JSONArray();
jsonArray2.put(json2);
现在我们将上面的数组和设备类型放在最终对象
中JSONObject json3= new JSONObject();
json3.put("device_type","SUP");
json3.put("commands",jsonArray2);
现在您可以将json3对象转换为字符串并发送到服务器。
json3.toString;
答案 1 :(得分:0)
为您想要的json数据创建一个JSONObject json = new JSONObject(device_type(在您的情况下))并将其发布到volley
答案 2 :(得分:0)
您可以使用此课程:
public class GsonRequest<T> extends Request<T> {
public static final String HEADER_CONTENT_TYPE = "Content-Type";
public static final String CONTENT_TYPE_TEXT_HTML = "text/html";
public static final int HTTP_STATUS_OK = 200;
protected final Type mTypeOfT;
private final Response.Listener<T> mListener;
protected final Gson mGson;
protected String mUrl = "";
protected boolean mIsCached = false;
/**
* Create a new Gson Json-parser request.
*
* @param method the Http method see {@link com.android.volley.Request.Method}
* @param typeOfT type of generic.
* @param url request url.
* @param listener response listener.
* @param errorListener error listener.
*/
public GsonRequest (final int method, final Type typeOfT, final String url,
final Response . Listener < T > listener,
final Response . ErrorListener errorListener) {
this(method, typeOfT, url, listener, errorListener, new Gson ());
}
/**
* Create a new Gson Json-parser request with a custom gson instance (useful for specifying
* custom date formats, etc.)
*
* @param method the Http method see {@link com.android.volley.Request.Method}
* @param typeOfT type of generic.
* @param url request url.
* @param listener response listener.
* @param errorListener error listener.
* @param gson custom Gson instance.
*/
public GsonRequest (final int method, final Type typeOfT, final String url,
final Response . Listener < T > listener,
final Response . ErrorListener errorListener, final Gson gson) {
super(method, url, errorListener);
mListener = listener;
mTypeOfT = typeOfT;
mGson = gson;
mUrl = url;
}
@Override
protected Response < T > parseNetworkResponse (final NetworkResponse response) {
try {
String charset = HttpHeaderParser . parseCharset (response.headers);
Log.d("response", "1");
final String responseData = new String (response.data, charset);
Log.d("response", "2");
logResponse("Request finished with response from the server:", response, responseData);
if (isHtmlFacade(response)) {
return Response.error(new VolleyError ());
}
T responseObject;
if (responseData.startsWith("{\"list\":")) {
Log.d("response", "if");
String json = responseData . substring (responseData.indexOf(":") + 1, responseData.indexOf("]")+1);
responseObject = mGson.fromJson(json, mTypeOfT);
} else {
Log.d("response-single", responseData);
responseObject = mGson.fromJson(responseData, mTypeOfT);
}
return Response.success(responseObject, HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
return Response.error(new ParseError (e));
}
}
/**
* @param response the response to check
* @return true if the response contains html according to its Content-Type and the status is
* 200 OK.
*/
private boolean isHtmlFacade(NetworkResponse response) {
Map<String, String> headers = response . headers;
String contentType = headers . get (HEADER_CONTENT_TYPE);
return response.statusCode == HTTP_STATUS_OK && contentType != null
&& contentType.contains(CONTENT_TYPE_TEXT_HTML);
}
@Override
protected void deliverResponse(final T response) {
mListener.onResponse(response);
}
}
然后致电:
GsonRequest<Model> request = new GsonRequest<Model>(Request.Method.POST,Model.class,"http://requestUrl", onsuccessListener, onerrorListener)