【发布时间】:2018-05-30 08:59:59
【问题描述】:
我想通过 volley http 客户端库将数据从 android 发布到 hyperledger composer。下面是代码。
String url="http://50.23.0.202:31090/api/Doctor";
private void postUsingJson() throws JSONException {
RequestQueue requstQueue = Volley.newRequestQueue(this); // this = context
JSONObject jsonObject=new JSONObject();
jsonObject.put("$class", "org.acme.Doctor");
jsonObject.put("doctorID", "300");
jsonObject.put("doctorName", "Zubair ");
jsonObject.put("contact", "05654");
jsonObject.put("description", "Cardiologist");
jsonObject.put("schedule", "resource:org.acme.Schedule#2");
System.out.println("json "+jsonObject);
JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("response "+response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("hi "+error.getMessage());
}
}
);
jsonobj.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requstQueue.add(jsonobj);
}
After running this code i get this error
当我复制相同的 json(如上图所示)并通过 composer rest 服务器发布它时,它工作正常。
【问题讨论】:
-
如果得到 500 错误代码,那么它是服务器端错误,我还建议使用改造来进行 api 调用。它更容易理解。
-
似乎您使用此代码发送的内容会导致服务器端错误。我建议你改用改造,但你仍然可以通过 volley 获得一些信息。由于服务器在您在此代码之外调用它时正确地给您响应,我猜问题出在您发送的 json 中。只需更改凌空的日志级别。您必须在终端中执行此操作:“adb -s DEVICE_ID shell setprop log.tag.Volley VERBOSE”,其中 DEVICE_ID 是您连接的设备的 ID。然后,您将看到有关您发送到服务器的内容的详细信息
-
这可能与 JsonObjectRequest 中传递的参数有关 - 建议查看这两个解决方案都使用 @override 来正确执行它 -> itworld.com/article/2702452/development/… 和 Volley JSON POST 示例在这里 -> zoftino.com/…
标签: android http-post android-volley hyperledger-fabric hyperledger-composer