如何在android中使用HttpUrlConnection发布 - >获取错误:java.io.IOException:content-length承诺82字节,但收到43

时间:2012-08-11 11:19:39

标签: android httpurlconnection

你好,我正在尝试从android到server的帖子数据。我正在尝试使用HttpURLConnection

我在这里发送用户名&用于在drupal中为特定用户输入数据的身份验证的密码。我还试图用各种其他方法发布数据。使用DefaultHttpClient但没有运气。使用DefaultHttpClient时出现401错误。

以下是我在stackoverflow上提出的问题链接。 Authentication error: Unable to respond to any of these challenges: {} Android - 401 Unauthorized

所以请帮忙。谢谢收听。

这是我的代码。

 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;

感谢。

1 个答案:

答案 0 :(得分:3)

您可以在此处设置内容长度:

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

但您发送的内容来自此处:

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

因此报告的内容长度与实际内容长度不符。

忽略中间的httpcon.connect();it does nothing because you are already connected.

相反,你需要这样做:

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