2016-08-12 9 views
1

私は私の携帯電話にSMSを送信するためにPlivo(VoIPサーバ)を使用してPOSTリクエストを送信しようとしていますが、私はちょうどBAD REQUEST(リクエストコード400)を受け取ります。アンドロイド - のHttpsURLConnection - BAD REQUEST

私が受信したエラーは、それが間違っているところ、私は見つけることができません"error": "invalid json data sent in raw POST"

です。

誰かが私を助けることができますか?

 final AsyncTask<String, Void, Boolean> request = new AsyncTask<String, Void, Boolean>() { 

      @Override 
      protected Boolean doInBackground(String... strings) { 

       Boolean retorno = true; 

       HttpsURLConnection request = null; 
       try { 
        request = (HttpsURLConnection) finalUrl.openConnection(); 

        System.out.println("Define POST"); 
        request.setRequestMethod("POST"); 
        request.setUseCaches(false); 
        request.setDoOutput(true); 

        System.out.println("Define propriedades"); 
        request.setRequestProperty("Content-Type", "application/json"); 
        request.setRequestProperty("Accept", "application/json"); 
        request.setRequestProperty("Accept-Charset", "UTF-8"); 
        request.setRequestProperty("charset", "UTF-8"); 

        System.out.println("Define autenticação"); 
        String autenticacao = authID + ":" + authToken; 
        String basic = "Basic " + new String(Base64.encode(autenticacao.getBytes(), Base64.NO_WRAP)); 
        request.setRequestProperty("Authorization", basic); 

        System.out.println("Define parâmetros"); 
        JSONObject params = new JSONObject(); 
        params.put("dst", numeroDestino); 
        params.put("src", numeroOrigem); 
        params.put("text", body); 

        System.out.println("Faz conexão e requisição"); 
        request.connect(); 
        OutputStreamWriter postParaEnvio = new OutputStreamWriter(request.getOutputStream()); 
        postParaEnvio.write(URLEncoder.encode(params.toString(), "UTF-8")); 
        postParaEnvio.flush(); 
        postParaEnvio.close(); 

        int codigoStatus = request.getResponseCode(); 

        for (Map.Entry<String, List<String>> header : request.getHeaderFields().entrySet()) { 
         System.out.println(header.getKey() + "=" + header.getValue()); 
        } 

        if (codigoStatus == 202) { 
         System.out.println("OK"); 
        } else { 
         System.out.println("Falha"); 
         System.out.println(codigoStatus); 

         retorno = false; 
        } 

       } catch (IOException e) { 
        System.out.println("Falha Exception"); 
        e.printStackTrace(); 

        retorno = false; 
       } catch (JSONException e) { 
        e.printStackTrace(); 

        retorno = false; 
       } finally { 

        request.disconnect(); 
       } 

       return retorno; 
      } 

      @Override 
      protected void onPostExecute(Boolean result) { 

       indicadorDeAtividade.dismiss(); 

       if(result) { 

       } else { 
        mostrarErro(0, R.string.erroEnviarCodigo); 
       } 
      } 
     }.execute(); 
+0

あなたは400でどのような追加情報を得ていますか? –

+0

なぜ単にボレーのような図書館を使うのではなく、本当に物事をはるかに簡単にする – has19

+0

Plivoセールスエンジニアはこちら。 400のレスポンスにはapi_idとそれがなぜ成功しなかったのかを示すエラーメッセージが伴います。さらなる質問がある、またはそれがまだ分からない場合は、support @ plivo.comまでお問い合わせください。 – CharlieC

答えて

0

Json objectを送信する場合、それはStringに渡す必要がありますが、それはコード化することはできません

を解決しました。

postParaEnvio.write(URLEncoder.encode(params.toString(), "UTF-8"));からpostParaEnvio.write(params.toString());に変更して問題を解決しました。

0

私はこれがあなたを助けるかもしれないと思う。ところで 、
マイ依存性:gradle->
コンパイル 'com.android.support:appcompat-v7:23.4.0'

class BackGround extends AsyncTask<String, String, String> { 

    protected String doInBackground(String... params) { 
     String name = params[0]; 
     String password = params[1]; 
     String email = params[2]; 
     String phone=params[3]; 

     String data=""; 
     int tmp; 

     try { 
      URL url = new URL("http://domain.com/yourFile.php"); 
      String urlParams = "name="+name+"&password="+password+"&email="+email+"&phone="+phone; 

      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setDoOutput(true); 
      OutputStream os = httpURLConnection.getOutputStream(); 
      os.write(urlParams.getBytes()); 
      os.flush(); 
      os.close(); 
      InputStream is = httpURLConnection.getInputStream(); 
      while((tmp=is.read())!=-1){ 
       data+= (char)tmp; 
      } 
      is.close(); 
      httpURLConnection.disconnect(); 
      return data; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      return "Exception: "+e.getMessage(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return "Exception: "+e.getMessage(); 
     } 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     if(s.equals("")){ 
      s="Data Saved! Success..."; 
     } 
     Toast.makeText(ctx, s, Toast.LENGTH_LONG).show(); 
    } 
} 
+0

はい。それは助けになった。どうもありがとうございました。 – GuiDupas