如何在android中使用HttpUrlConnection发布->;获取错误:java.io.IOException:内容长度承诺为82字节,但收到43字节

如何在android中使用HttpUrlConnection发布->;获取错误:java.io.IOException:内容长度承诺为82字节,但收到43字节,android,httpurlconnection,Android,Httpurlconnection,您好,我正在尝试从android向服务器发送数据。我正在尝试使用HttpURLConnection 在这里,我发送用户名和密码进行身份验证,以便在drupal中输入特定用户的数据。我还尝试用各种其他方法发布数据。使用DefaultHttpClient但运气不佳。使用DefaultHttpClient时出现401错误 这是我在stackoverflow上提出的问题的链接 所以请帮忙。谢谢收听 这是我的密码 public static class post_idea extends AsyncTa

您好,我正在尝试从android向服务器发送数据。我正在尝试使用
HttpURLConnection

在这里,我发送用户名和密码进行身份验证,以便在drupal中输入特定用户的数据。我还尝试用各种其他方法发布数据。使用DefaultHttpClient但运气不佳。使用DefaultHttpClient时出现401错误

这是我在stackoverflow上提出的问题的链接

所以请帮忙。谢谢收听

这是我的密码

 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;
公共静态类post\u任务{
字符串strResponse1;
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
pgb.setVisibility(View.VISIBLE);
}
@凌驾
公共Void doInBackground(Void…params){
//TODO自动生成的方法存根
//字符串url=”http://testingd7.mobileapplicationtesters.com/my_android_drupal/user/login";
字符串url=”http://testingd7.mobileapplicationtesters.com/my_android_drupal/node.json";
//字符串url=”http://mobiappdevelopers.com/drupal_test/my_android_drupal/node.json";
//字符串url=”http://www.drupal7.mobileapplicationtesters.com/my_services/node.json";
strResponse1=makeWebForPostIdea(url、标题、正文);
System.out.println(“==========>来自post idea的响应=>”
+strResponse1);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
pgb.setVisibility(视图已消失);
}
公共静态字符串makeWebForPostIdea(字符串url123、字符串标题、字符串正文)
{
HttpURLConnection httpcon=null;
JSONObject json=null;
JSONObject jsonnode=null;
试一试{
JSONObject jsonvalue=新的JSONObject();
jsonvalue.put(“value”,body.toString());
JSONArray数组=新的JSONArray();
put(jsonvalue);
jsonnode=新的JSONObject();
jsonnode.put(“und”,数组);
System.out.println(“@@@@2 jsonnode=====>”+jsonnode.toString());
json=新的JSONObject();
json.put(“title”,title);
put(“body”,jsonnode);
put(“type”、“article”);
System.out.println(“联合节点的值=====>”+json.toString());
}捕获(JSONException e3){
//TODO自动生成的捕捉块
e3.printStackTrace();
}
试一试{
httpcon=(HttpURLConnection)((新URL(url123.openConnection()));
httpcon.setDoOutput(真);
setRequestProperty(“内容类型”、“应用程序/json”);
setRequestProperty(“接受”、“应用程序/json”);
httpcon.setRequestMethod(“POST”);
字符串参数=
“type=“+urlcoder.encode”(“第页”,“UTF-8”)+
“title=“+URLEncoder.encode”(标题,“UTF-8”)+
“body”+URLEncoder.encode(jsonnode.toString(),“UTF-8”);
httpcon.setRequestProperty(“内容长度”和“+
toString(urlParameters.getBytes().length));
}捕获(协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
httpcon.connect();
byte[]outputBytes=“{\'用户名\':\'取消名称\',\'密码\':\'通过\'}”。getBytes(“UTF-8”);
OutputStream os=httpcon.getOutputStream();
写入(输出字节);
os.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;

谢谢。

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

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.CONTRONTHORE(;)/<代码>中间,

相反,您需要执行以下操作:

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

您是否已授予internet权限?可能是的副本