We are trying to jsonarray server using HttpPost. In jsonarrray consists of jsonobjects with prams. But iam getting null while sending to server. below code:
class EventPosting extends AsyncTask<Void, Void, JSONArray> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading Data...");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONArray doInBackground(Void... params) {
//return postJsonObject(URLs.Search.URL, makingJson());
return postJsonObject("http://192.168.3.245:1985/MyService.svc/postevents", makingJson());//local
// return postJsonObject("http://192.168.3.223:1985/MyService.svc/SearchData", makingJson());//local
}
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
if (result!=null) {
pDialog.dismiss();
Log.d("Dgetta: ", "" + result);
// Toast.makeText(getActivity(), "Successfully post json object", Toast.LENGTH_LONG).show();
// Toast.makeText(getActivity(),"Data Retrieved Successfully",Toast.LENGTH_LONG).show();
}else {
pDialog.dismiss();
}
}
}
public JSONObject makingJson() {
jsonObj = new JSONObject();
Log.d("Checking", "ch_name: " + p_title + ":" + p_description);
try {
jsonObj.put(URLs.PostEvents.param1, p_title);
jsonObj.put(URLs.PostEvents.param2, p_description);
jsonObj.put(URLs.PostEvents.param3, p_title + "_" + event_Datetime);//Img1Name
jsonObj.put(URLs.PostEvents.param4, p_title + "_" + event_Datetime);//Img2Name
jsonObj.put(URLs.PostEvents.param5, p_title + "_" + event_Datetime);//Img3Name
jsonObj.put(URLs.PostEvents.param6, bitmap_string1);//Img1Path
jsonObj.put(URLs.PostEvents.param7, "TestImg2");//Img2Path*//*
jsonObj.put(URLs.PostEvents.param8, "TestImg2");//Img3Path
jsonObj.put(URLs.PostEvents.param9, "TestClubID");//ClubId
jsonObj.put(URLs.PostEvents.param10, "TestPostID");//PostedBY
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("JSonOBJ",""+jsonObj);
return jsonObj;
}
public JSONArray postJsonObject(String url, JSONObject loginJobj){
Log.d("NewJson",""+loginJobj);
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
//http://localhost:9000/api/products/GetAllProducts
HttpPost httpPost = new HttpPost(url);
System.out.println(url);
String jsonStr = "";
// 4. convert JSONObject to JSON to String
jsonStr = loginJobj.toString();
System.out.println(jsonStr);
// 5. set json to StringEntity
StringEntity se = new StringEntity(loginJobj.toString());
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStreamjj = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStreamjj != null){
resultStr = convertInputStreamToString(inputStreamjj);
json = new JSONArray(resultStr);
}
else{
resultStr = "Did not work!";
}
} catch (Exception e) {
Log.d("inputStreamjj", e.getLocalizedMessage());
}
Log.d("Result", resultStr);
// try {
//
//
// } catch (JSONException e) {
// e.printStackTrace();
// }
// 11. return result
return json;
}
private String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
Log.d("New Data: ",":" +result);
inputStream.close();
return result;
}
In inputstream in showing null Plz guide us.advance thanks@!