Issue
Hello I try to create a Document in CouchDb using Volley but I have Problems with both the POST and PUT Method.
String url = "http://serverurl:port/database/";
StringRequest request = new StringRequest(Request.Method.POST
, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "onErrorResponse: ", error );
}
})
{
@Override
public Map<String,String> getHeaders()throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Accept-Charset", AppConfig.CHARSET);
params.put("Content-Type", "application/json");
String encoding = Base64.encodeToString(AppConfig.SERVER_COUCHDB_AUTHENIFICATIONDATA.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
params.put("Authorization","Basic "+ encoding);;
return params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("VolleyTest","Hello CouchDB");
return map;
}
};
Volley.newRequestQueue(this).add(request);
I get the following Error in my Logcat:
07-14 15:44:06.599 2003-2064/de.game_wordit.wordit E/Volley: [361] BasicNetwork.performRequest: Unexpected response code 415 for MYURL
07-14 15:44:06.603 2003-2003/de.game_wordit.wordit E/Volley: onErrorResponse:
com.android.volley.ServerError
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
If I try the PUT Request
String url = "http://serverurl:port/database/";
StringRequest request = new StringRequest(Request.Method.PUT
, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "onErrorResponse: ", error );
}
})
{
@Override
public Map<String,String> getHeaders()throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Accept-Charset", AppConfig.CHARSET);
params.put("Content-Type", "application/json");
String encoding = Base64.encodeToString(AppConfig.SERVER_COUCHDB_AUTHENIFICATIONDATA.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
params.put("Authorization","Basic "+ encoding);;
return params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("VolleyTest","Hello CouchDB");
return map;
}
};
Volley.newRequestQueue(this).add(request);
With the PUT Request this is the Error:
07-14 15:47:42.221 5144-5187/de.game_wordit.wordit E/Volley: [365] BasicNetwork.performRequest: Unexpected response code 412 for MYURL`07-14 15:47:42.223 5144-5144/de.game_wordit.wordit E/Volley: onErrorResponse:
com.android.volley.ServerError
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
I also tried `JsonObjectRequest but it also don't work and I get the same errors.
Solution
I figured something out by myself:
String url = "http://url:port/database/docname";
Map<String, String> map = new HashMap<>();
map.put("a","b");
final JSONObject json= new JSONObject(map);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.PUT
, url,json, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "onErrorResponse: ", error );
Log.e("Volley", "onErrorResponse: "+new String(error.networkResponse.data,StandardCharsets.UTF_8) );
}
})
{
@Override
public Map<String,String> getHeaders()throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Accept-Charset", AppConfig.CHARSET);
params.put("Content-Type", "application/json");
params.put("Accept","application/json");
String encoding = Base64.encodeToString(AppConfig.SERVER_COUCHDB_AUTHENIFICATIONDATA.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
params.put("Authorization","Basic "+ encoding);;
return params;
}
@Override
public byte[] getBody() {
return json.toString().getBytes();
}
};
String str = new String(request.getBody(), StandardCharsets.UTF_8);
Log.i("iii", "onCreate:" +str);
Volley.newRequestQueue(this).add(request);
with this you are able to create a Document with a fixed ID. If you don't type the ID in the URL the program tries to create a Database. Normally you should be able to create a Document with a ID which is created by CouchDb if you leave the Doc ID out. But this not works for me. But I can live with this because I need to set my Doc IDs on my own.
Answered By - tobzilla90
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.