响应代码400 OKHTTP

时间:2017-11-08 15:26:37

标签: java android okhttp

当我运行我的Android应用程序时,得到响应代码= 400 ,但是当我在浏览器和邮递员中测试相同的URL时,它的响应代码为200.我做错了什么。这是get Request方法

  public static String getRequest(String myUrl) throws IOException {

        InputStream is = null;

        try {

            URL url = new URL(myUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(30000 /*milliseconds*/);
            conn.setConnectTimeout(20000 /*milliseconds*/);
            conn.setRequestMethod("GET");

            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            int response = conn.getResponseCode();
            if (response != HttpURLConnection.HTTP_OK)
                is = conn.getErrorStream();
            else
            is = conn.getInputStream();

            Log.i(TAG, String.valueOf(response));


            // Convert the InputStream into a string
            return readStream(is);

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

我需要帮助。

2 个答案:

答案 0 :(得分:0)

你不需要打电话:

     override func viewDidLoad() {
            super.viewDidLoad()

    profileImage.layer.cornerRadius = profileImage.frame.size.width/2
            profileImage.clipsToBounds = true
            profileImage.contentMode = .scaleAspectFill
            let tapGestur = UITapGestureRecognizer(target: self, action: #selector(SignupVC.selectProfileImage))
            profileImage.addGestureRecognizer(tapGestur)
            profileImage.isUserInteractionEnabled = true
    }

 @objc func selectProfileImage() {
        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        PiActivityIndicator.startAnimating()
        present(pickerController, animated: true, completion: nil)
        pickerController.allowsEditing = true

    }

    extension SignupVC: UINavigationControllerDelegate,UIImagePickerControllerDelegate {


        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

            if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
                selectedImage = image
                profileImage.image = image
                self.profileActivityIndicator.startAnimating()

            }


            dismiss(animated: true, completion: nil)
        }
    }

用于GET请求。我认为您错过了一些标题,如(授权)

答案 1 :(得分:0)

所以我发现了问题,因为我使用OData过滤器进行查询,而真正的设备设备无法编码这样的URL我必须手动编码我的URL。

这就是我之前传递的内容:

   String url = *Const.HOUSE_URL+"PropertyListings?$filter=Price ge "+ minPrice+" and Price le "+maxPrice*

但编码时:String url = *Const.HOUSE_URL+"PropertyListings?$filter="+ **query** + minPrice+ **query1** + maxPrice;*

其中

String query = URLEncoder.encode("Price ge ", "utf-8");

String query1 = URLEncoder.encode(" and Price le ","utf-8");

然后传递给我的HTTP GET请求的最终URL是:

 String url = Const.HOUSE_URL+"PropertyListings?$filter="+ query + minPrice+ query1 + maxPrice;

其中minPrice和maxPrice是EditText的String值。

希望这对某人有所帮助。