1

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@!

  • Use http libraries like Retrofit/Ion etc, and use Plain old java objects(Pojo) for creating and parsing json strings – Bhargav Feb 23 '16 at 12:40
  • In jsonobject,we are passing bitmap strings which converted from images. Showing errors as HTTP/1.1 413 Request Entity Too Large – sanjay premnadh Feb 23 '16 at 12:49
  • append your comment to the question, it's important, actually it's the actual question you should be asking,-- and i think it's web-server config issue. you need to enlarge allowed size, what is the server you are using? `Apache` ? – Yazan Feb 23 '16 at 13:13
  • At server side also,we increased max sixe but same response – sanjay premnadh Feb 23 '16 at 13:15
  • well, if the error message still the same then: 1) you need to increase it more, 2) you are not doing it right. so update the question, mention server type you are using Apache, IIS , ... etc – Yazan Feb 23 '16 at 13:19
  • our services are developed in .net & server is IIS – sanjay premnadh Feb 23 '16 at 13:20
  • our server is IIS and also we increased the size. – sanjay premnadh Feb 24 '16 at 07:43

0 Answers0