2

您好,我正在尝试从 android 到服务器的发布数据。我正在尝试使用HttpURLConnection.

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

这是我在stackoverflow上提出的问题链接。身份验证错误:无法响应任何这些质询:{} 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;

谢谢。

4

1 回答 1

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);

所以报告的 Content-Length 与实际的内容长度不匹配。

忽略httpcon.connect();中间的,它什么都不做,因为你已经连接了。

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

byte[] outputBytes = "{\"username\":\"uname\",\"password\":\"pass\"}".getBytes("UTF-8");
httpcon.setRequestProperty("Content-Length", Integer.toString(outputBytes.length()));
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);
于 2012-08-11T11:32:03.543 回答