user1584610
user1584610

Reputation:

how to post using HttpUrlConnection in android -> getting error : java.io.IOException: content-length promised 82 bytes, but received 43

Hello here I am trying for the post data from android to server.I am trying using HttpURLConnection.

Here I am sending username & password for Authentication for entering data for particular user in drupal. I have also tried to post data with various other methods. Using DefaultHttpClient but no luck. I am getting 401 error with using DefaultHttpClient.

Here is the link of question that I have asked on stackoverflow. Authentication error: Unable to respond to any of these challenges: {} Android - 401 Unauthorized

SO please help. Thanks for listening.

Here is my code.

 public static class post_idea extends AsyncTask<Void, Void, Void> {

  String strResponse1;
  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   super.onPreExecute();

   pgb.setVisibility(View.VISIBLE);
  }

  @Override
  public Void doInBackground(Void... params) {
   // TODO Auto-generated method stub


   // String url = "http://testingd7.mobileapplicationtesters.com/my_android_drupal/user/login";
      String url = "http://testingd7.mobileapplicationtesters.com/my_android_drupal/node.json";
  // String url = "http://mobiappdevelopers.com/drupal_test/my_android_drupal/node.json";
   //String url = "http://www.drupal7.mobileapplicationtesters.com/my_services/node.json";

   strResponse1 = makeWebForPostIdea(url,title,body);

   System.out.println("=========> Response from post  idea => "
     + strResponse1);

   return null;
  }

  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   super.onPostExecute(result);

   pgb.setVisibility(View.GONE);


  }



public static String makeWebForPostIdea(String url123, String title,String body)
  {

      HttpURLConnection httpcon = null;

      JSONObject json = null;
      JSONObject jsonnode = null;


        try {
            JSONObject jsonvalue = new JSONObject();
            jsonvalue.put("value", body.toString());

            JSONArray array = new JSONArray();
            array.put(jsonvalue);

            jsonnode = new JSONObject();
            jsonnode.put("und", array);

            System.out.println("@@@@@@2    jsonnode=======>"+jsonnode.toString());

            json = new JSONObject();
            json.put("title",title);
            json.put("body", jsonnode);
            json.put("type","article");

            System.out.println("value of the combine node=======>"+json.toString());  


        } catch (JSONException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }

      try {
          httpcon = (HttpURLConnection) ((new URL(url123).openConnection()));
          httpcon.setDoOutput(true);
          httpcon.setRequestProperty("Content-Type", "application/json");
          httpcon.setRequestProperty("Accept", "application/json");
        httpcon.setRequestMethod("POST");

        String urlParameters =
                "type=" + URLEncoder.encode("page", "UTF-8") +
                "title=" + URLEncoder.encode(title, "UTF-8") +
                "body" + URLEncoder.encode(jsonnode.toString(), "UTF-8") ;

        httpcon.setRequestProperty("Content-Length", "" + 
                 Integer.toString(urlParameters.getBytes().length));


    } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


      try {
        httpcon.connect();
         byte[] outputBytes = "{\"username\":\"uname\",\"password\":\"pass\"}".getBytes("UTF-8");
          OutputStream os = httpcon.getOutputStream();
          os.write(outputBytes);

          os.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

Thanks.

Upvotes: 2

Views: 6108

Answers (1)

John Watts
John Watts

Reputation: 8875

You set the content length here:

httpcon.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));

But the content you sent comes from here:

byte[] outputBytes = "{\"username\":\"uname\",\"password\":\"pass\"}".getBytes("UTF-8");
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);

So the reported Content-Length does not match the actual content length.

Ignore the httpcon.connect(); in the middle, it does nothing because you are already connected.

Instead, you need to do:

byte[] outputBytes = "{\"username\":\"uname\",\"password\":\"pass\"}".getBytes("UTF-8");
httpcon.setRequestProperty("Content-Length", Integer.toString(outputBytes.length()));
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);

Upvotes: 3

Related Questions