下面是我需要传递给服务器的json对象:
{
"PostImages": [{
"FileFormat": "jpeg",
"id": "0",
"FileByteArray": "LONG BYTE ARRAY OF FILE",
"FileCaption": "",
"VideoLink": ""
},{
"FileFormat": "jpeg",
"id": "0",
"FileByteArray": "LONG BYTE ARRAY OF FILE",
"FileCaption": "",
"VideoLink": ""
}],
"AdminScheduleId": "0",
"CategoryId": "1",
"UserId": "4",
"Presenter2": "0",
"IdeaTitle": "gfgfdgdfgdfg",
"IsImplementedByOwn": "false",
"NoOfVolunteer": "0",
"PostId": "0",
"ClubId": "2",
"Description": " Dfgdfgdf",
"Presenter1": "0"
}
我已经通过将json对象传递给服务器来完成它。但它只是因为长字节数组而产生问题。
请参阅我的以下工作代码:
String res = null;
HttpPost request = new HttpPost(CREATE_IDEA_URL);
request.addHeader("Content-Type", "application/json; charset=utf-8");
//request.addHeader("Content-type", "application/x-www-form-urlencoded");
try {
JSONArray array=new JSONArray(PostImages); // Array of Images details
JSONObject jObject = new JSONObject()
.put("IdeaTitle",IdeaTitle)
.put("Description",Description)
.put("IsImplementedByOwn",IsImplementedByOwn)
.put("NoOfVolunteer",NoOfVolunteer)
.put("PostId",PostId)
.put("UserId",UserId)
.put("ClubId",ClubId)
.put("AdminScheduleId",AdminScheduleId)
.put("CategoryId",categoryId)
.put("Presenter1",Presenter1)
.put("Presenter2",Presenter2)
.put("PostImages",array);
StringEntity entity = new StringEntity(jObject.toString(2), "UTF-8"); // ADD THIS LINE TO HAVE ARABIC LANGUAGE IN POST
System.out.println("jObject is:::: "+jObject.toString());
request.setEntity(entity);
System.out.println("====the Entity is: "+entity.toString());
DefaultHttpClient httpClient = new DefaultHttpClient();
//----------------------
if(LC5_API.getCookiesFromLC5() == null){
}else{
CookieStore cookieStore = new BasicCookieStore();
for(Cookie cook : LC5_API.getCookiesFromLC5()){
cookieStore.addCookie(cook);
}
((AbstractHttpClient) httpClient).setCookieStore(cookieStore);
}
//-------------------
HttpResponse response = httpClient.execute(request);
//res = response.toString();
int resCode = response.getStatusLine().getStatusCode();
System.out.println("Our Res: "+jObject.toString());
System.out.println("Result code: "+resCode);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line="";
StringBuffer returnFromServer = new StringBuffer();
while ((line=in.readLine())!=null){
returnFromServer.append(line);
}
//Toast what we got from server
//Toast.makeText(getApplicationContext(),returnFromServer.toString(), Toast.LENGTH_LONG).show();
System.out.println("Responce: "+returnFromServer.toString());
res = returnFromServer.toString();
if (entity != null){
entity.consumeContent();
}
// Toast.makeText(getApplicationContext(),response.getStatusLine().getStatusCode()+"", Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
res=null;
}
System.out.println("Responcre is:"+res);
return res;*/
}
public static String Get_AdoptIdea(String PostId) {
String responce = null;
RestClient client = new RestClient(GET_ADOPT_IDEA);
// if(LC5_API.DEBUG) Log.d("SetCookie", LC5_API.SESSION_COOKIEName + ":"+ LC5_API.SESSION_COOKIEValue+"");
// client.AddHeader("Cookie", SESSION_COOKIEName+"="+LC5_API.SESSION_COOKIEValue);
client.AddParam("postId", PostId);
try {
client.Execute(RequestMethod.GET);
responce = client.getResponse();
} catch (ConnectTimeoutException e) {
e.printStackTrace();
if(DEBUG)Log.d(TAG, "");
responce=null;
}catch(Exception e)
{
responce=null;
}
if(DEBUG)Log.d(TAG, "Response: "+responce);
return responce;
以上代码非常完美。但我想将它与namevaluepair一起发送到服务器。
请参阅下面的代码:
String res = null;
try {
HttpClient client = new DefaultHttpClient();
String URL = CREATE_IDEA_URL;
HttpPost post = new HttpPost(URL);
List<NameValuePair> postParameters;
postParameters = new ArrayList<NameValuePair>();
//postParameters.add(new BasicNameValuePair("PostId", PostId));
postParameters.add(new BasicNameValuePair("IdeaTitle",IdeaTitle));
postParameters.add(new BasicNameValuePair("Description",Description));
postParameters.add(new BasicNameValuePair("IsImplementedByOwn",IsImplementedByOwn));
postParameters.add(new BasicNameValuePair("NoOfVolunteer",NoOfVolunteer));
postParameters.add(new BasicNameValuePair("PostId",PostId));
postParameters.add(new BasicNameValuePair("UserId",UserId));
postParameters.add(new BasicNameValuePair("ClubId",ClubId));
postParameters.add(new BasicNameValuePair("AdminScheduleId",AdminScheduleId));
postParameters.add(new BasicNameValuePair("CategoryId",categoryId));
postParameters.add(new BasicNameValuePair("Presenter1",Presenter1));
postParameters.add(new BasicNameValuePair("Presenter2",Presenter2));
//postParameters.add(new BasicNameValuePair("PostImages",ctx.getString(R.string.json_object_test)));
JSONArray array = new JSONArray(PostImages);
List<NameValuePair> param = new ArrayList<NameValuePair>();
for(int i=0; i<array.length();i++){
ImagesData img = new ImagesData();
String video = array.getJSONObject(i).getString("VideoLink").equalsIgnoreCase("") ? "xyz" : array.getJSONObject(i).getString("VideoLink");
System.out.println("VIDEO:: "+video);
param.add(new BasicNameValuePair("id", array.getJSONObject(i).getString("id")));
param.add(new BasicNameValuePair("FileCaption", array.getJSONObject(i).getString("FileCaption")));
param.add(new BasicNameValuePair("FileFormat", array.getJSONObject(i).getString("FileFormat")));
param.add(new BasicNameValuePair("VideoLink", video));
param.add(new BasicNameValuePair("FileByteArray", array.getJSONObject(i).getString("FileByteArray")));
postParameters.add(new BasicNameValuePair("PostImages",param.toString()));
}
post.setEntity(new UrlEncodedFormEntity(postParameters));
post.setHeader("Content-Type", "application/json; charset=utf-8");
if(LC5_API.DEBUG) Log.d("REQUEST FROM REST:", "The requested String is:"+CREATE_IDEA_URL+postParameters);
//--------------------------------
if(LC5_API.getCookiesFromLC5() == null){
}else{
CookieStore cookieStore = new BasicCookieStore();
//-------------------------------------------
for(Cookie cook : LC5_API.getCookiesFromLC5()){
cookieStore.addCookie(cook);
}
((AbstractHttpClient) client).setCookieStore(cookieStore);
}
//---------------------------------
HttpResponse responsePost = client.execute(post);
HttpEntity resEntityPost = responsePost.getEntity();
if (resEntityPost != null)
{
String response=EntityUtils.toString(resEntityPost);
res = response;
}
} catch (Exception e) {e.printStackTrace();}
return res;
现在,当我使用上面的代码传递值时,它给了我无效的JSON原语:IdeaTitle。
一切都很好但没有得到适当的回应。请帮助我如何传递具有json数组的json对象。
仅供参考,postImages是我的json数组,包含一些细节。
请帮帮我。