嘿,我的Android CouchDB项目有些问题。
我在Android中使用Volley作为我的网络请求。
问题是我没有得到完整的JSON数据。 请求的网址是:
myserveradress:databaseport /数据库名称/ documentname /
通过应用程序的响应是:
{"ok":true,"id":"E-c5b6d7c16ecb41cd8dadaf3514c968","rev":"1-41f6abc7851b1ad60d169149c557e7fc"}
如果我通过浏览器尝试使用相同的网址,我的回答是正确的:
{"_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}}
奇怪的是,我在我的应用程序中的多个请求中使用它,而在某些请求中它运行得很好。
这是我的代码: 拳头我有一个助手级别的排球请求:
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
第二个输出我无法理解为什么我没有收到完整的数据。
答案 0 :(得分:2)
我解决了!!!
缓存是问题......
Volley.newRequestQueue(getContext()).getCache().clear();
如果我在进行Volley GET请求之前添加此内容