嘿,我的Android CouchDB項目有一些問題。CouchDB通過Android返回錯誤的JSONObject GET響應Volley
我使用Volley作爲Android中的網絡請求。
問題是我沒有得到完整的JSON數據。 的請求的URL是:
myserveradress:databaseport /數據庫名稱/ documentname/
通過應用程序的響應是:
{"ok":true,"id":"E-c5b6d7c16ecb41cd8dadaf3514c968","rev":"1-41f6abc7851b1ad60d169149c557e7fc"}
如果我嘗試確切相同的URL thorugh我的瀏覽器我的迴應是對的: {"_id":"E-c5b6d7c16ecb41cd8dadaf3514c968","_rev":"1-41f6abc7851b1ad60d169149c557e7fc","player":{"solvedPuzzels":[false,false,false,false,false],"triesPerPuzzel":[0,0,0,0,0]},"invites":[],"settings":{"gameTime":4,"numberOfPlayers":2,"puzzels":7}}
奇怪的是th我在我的應用程序中使用了多個請求,而且在一些情況下它工作得很好。
這裏我的代碼: 拳我有一個輔助類的凌空要求:
public class NetworkVolley {
public static JsonObjectRequest POST(String url, final JSONObject json, Response.Listener listener){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.PUT
, url,json, listener,
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();
}
};
return request;
}
public static JsonObjectRequest GET(String url, Response.Listener listener){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
listener,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley User", "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");
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;
}
};
return request;
}
}
這裏是我的片段:
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_invite, container, false);
listView = (ListView) rootView.findViewById(R.id.listView);
TV_numberOfPlayers = (TextView) rootView.findViewById(R.id.playerInfo);
final InviteActivity activity = (InviteActivity) getActivity();
numberOfPlayers = activity.getNumberOfPlayers();
gameId = activity.getGameId();
gameUrl = "myserveradress:databaseport/databasename/" + gameId + "/";
userId = "userA";
getFriendList();
getGameObject();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
JSONArray invites = new JSONArray();
try {
invites = object.getJSONArray("invites");
for (int i=0;i<=invites.length();i++){
if (!(invites.length()==0) ||(!(invites.get(i).equals(parent.getItemAtPosition(position))))){
invites.put(parent.getItemAtPosition(position));
object.remove("invites");
object.put("invites", invites);
setInvite();
}else {
Log.i("Invite","Same invited Person");
break;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return rootView;
}
private void setInvite() {
Response.Listener<JSONObject> POSTinvite = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
getGameObject();
}
};
Volley.newRequestQueue(getContext()).add(NetworkVolley.POST(gameUrl,object,POSTinvite));
}
private void getGameObject() {
object = new JSONObject();
Response.Listener<JSONObject> gameObjListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response!=null){
object = response;
Log.i("Game Obj", "onResponse: "+object);
try {
TV_numberOfPlayers.setText(getString(R.string.inv_tv_invitetPlayers) + " " + object.getJSONArray("invites").length() + "/" + numberOfPlayers);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
Volley.newRequestQueue(getContext()).add(NetworkVolley.GET(gameUrl,gameObjListener));
}
private void getFriendList() {
friendsearchUrl = "myserveradress:databaseport/databasename/_design/friendsearch/_view/friendsearch?key="+"\""+userId+"\"";
friends = new ArrayList();
Response.Listener<JSONObject> friendsListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
for (int i=0;i<response.getJSONArray("rows").length();i++){
JSONObject object = new JSONObject(String.valueOf(response.getJSONArray("rows").get(i)));
friends.add(object.getString("value"));
}
adapter = new FriendsListAdapter(getActivity(),friends);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Volley.newRequestQueue(getContext()).add(NetworkVolley.GET(friendsearchUrl,friendsListener));
}
這裏的logcat的:
I/Game Obj: onResponse: {"_id":"E-69329c12008541949cba062559a3e2","_rev":"1-b0b6eb597bfe6905b06723953fbf6870","player":{"solvedPuzzels":[false,false,false,false,false],"triesPerPuzzel":[0,0,0,0,0]},"invites":[],"settings":{"gameTime":4,"numberOfPlayers":2}}
一切正常,這一點......
I/Game Obj: onResponse: {"ok":true,"id":"E-69329c12008541949cba062559a3e2","rev":"2-2e7fa50583c3dbd4eca8eaf103567da7"}
W/System.err: org.json.JSONException: No value for invites
第二輸出我不明白爲什麼我不recive完整的數據。
你能表現出一定的代碼?好像你在第一種情況下創建一個文檔。 –
我認爲數據庫存在問題,但我會顯示一些代碼。我在其他活動中創建文檔。然後,我收到文檔並在我收到錯誤的數據之前立即讀取它,但在URL末尾有一個反斜槓。然後我更改文檔並在單擊列表視圖中的對象後保存它。到目前爲止,一切正常,但我需要再次從數據庫中獲取數據,但它不起作用,但它與第二次使用相同URL調用的方法完全相同,但我沒有收到完整的數據。 –
你能跟蹤確切的HTTP請求嗎?通過應用程序的響應不是文檔(不是GET),而是您在PUT或POST新文檔之後得到的響應類型。 (這些ID也不同。) – pwagner