0

우편 게시가 성공적으로 작동하는 게시물 요청이 있습니다.하지만 Android 앱에서 동일한 요청을하면 "내부 서버 오류"가 발생합니다. 이 두 요청 사이에 차이점이 있습니까?우체부 요청을 Java로 번역 할 수 없습니다

이죠

Postman request

안드로이드 앱

class MakeRequest extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... voids) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("https://sample.com/test"); 
      httppost.setHeader("Content-type", "application/json"); 
      httppost.setHeader("Authorization", getB64Auth("username", "password")); 

      try { 
       JSONObject identity = new JSONObject(); 
       identity.put("type", "number"); 
       identity.put("endpoint", "12345"); 

       JSONObject options = new JSONObject(); 
       options.put("num", "12345"); 

       JSONObject body = new JSONObject(); 
       body.put("identity", identity); 
       body.put("method", "test"); 
       body.put("options", options); 

       StringEntity se = new StringEntity(body.toString()); 
       httppost.setEntity(se); 
      } catch (IOException e) { 
       Log.d("IOException", e.toString()); 
      } catch (JSONException e) { 
       Log.d("JSONException", e.toString()); 
      } 

      try { 
       ResponseHandler handler = new BasicResponseHandler(); 
       HttpResponse response = httpclient.execute(httppost); 
       Log.d("HttpResponse", handler.handleResponse(response).toString()); 
      } catch (ClientProtocolException e) { 
       Log.d("ClientProtocolException", e.toString()); 
      } catch (IOException e) { 
       Log.d("IOException", e.toString()); 
      } 

      return null; 
     } 

     private String getB64Auth (String key, String secret) { 
      String source=key+":"+secret; 
      String ret="Basic "+Base64.encodeToString(source.getBytes(),Base64.URL_SAFE|Base64.NO_WRAP); 
      return ret; 
     } 

    } 

다른 생각/힌트 :

인증은 자바 코드에서 제대로 작동합니다. 잘못된 사용자 이름 & 암호로 시도했지만 "잘못된 승인"이 표시됩니다.

JSON 문자열은 우체부와 동일하게 입니다. 정확히입니다. 내가 body.toString()을 인쇄하여 우편 배달부에 복사하여 붙여 넣었습니다. 요청은 우편 배달부에서 잘 작동했습니다.

+0

이것 좀보세요 http://stackoverflow.com/questions/7181534/http-post-using-json-in-java/7181775#7181775 –

+0

링크를 제공해 주셔서 감사합니다! 나는 그 대답을 사용했지만, 작동시키기 위해서 content-type을 application/json으로 변경했다. – mravca

답변

0

이이 문제를 해결해야 httppost.setHeader("Content-type", "application/json");httppost.setHeader("Content-type", "application/form-data");에 또는 httppost.setHeader("Content-type", "application/x-www-form-urlencoded");

변경해야합니다.

관련 문제