将curl请求转换为android HttpURLConnection Post Request

时间:2017-08-03 18:53:56

标签: android curl https

嗨,我正在开发一个Android应用程序,其中我使用mailchimp服务。简而言之 我不知道curl,我想将以下curl请求转换为httpurlconnection post请求。 任何帮助都会赞赏并感谢高级。这是卷曲代码

try {
                urlConnection = (HttpURLConnection) params[0].openConnection();
                urlConnection.setUseCaches(false);
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Authorization",basicAuth);
                urlConnection.setRequestProperty("Content-Type","application/json");
                JSONObject info= new JSONObject();
                info.put("email_address",email);
                info.put("status","subscribed");

                OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
                wr.write(info.toString());
                wr.flush();
                wr.close();
                StringBuilder sb=new StringBuilder();
                int HttpResult =urlConnection.getResponseCode();
                if(HttpResult ==HttpURLConnection.HTTP_OK){
                    BufferedReader br = new BufferedReader(new InputStreamReader(
                            urlConnection.getInputStream(),"utf-8"));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();

                    System.out.println("  "+sb.toString());

                }else{
                    System.out.println(urlConnection.getResponseMessage());
                }
                Log.e("authkey==> ",apiKey+"");
                //  InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            }catch (JSONException e){
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
                Log.d("URL","Error in URL Connection");
            } finally {
                urlConnection.disconnect();
                Log.d("URL","Error in URL Connection,Connection Closed");
            }
  

我编码它,下面是我的android代码,但在logcat我得到400   错误的要求

$f

0 个答案:

没有答案