2016-08-26 3 views
0

나는 POST를 사용하여 안드로이드에서 loopj.Web 서비스를 사용하여 편안한 웹 서비스에 데이터를 테스트하려고합니다. 내가 실패하려고 게시하려고하면 logcat에 다음 메시지가 표시됩니다.loopj에 헤더를 추가하는 방법 POST

cz.msebera.android.httpclient.client.HttpResponseException : 나는 Content-Type을 추가하지하고 생각하는 오류에서 지원되지 않는 미디어 유형

- 응용 프로그램/JSON을 헤더에. 추가하는 방법에 대한 몇 가지 예를 보았습니다. 하지만 정말 혼란 스럽습니다. 누군가 나를 도울 수 있습니다.

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     etemployeeId = (EditText)findViewById(R.id.employeeId); 
     etfirstName = (EditText)findViewById(R.id.firstName); 

     buttonRegister = (Button) findViewById(R.id.btnRegister); 
     buttonRegister.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       registerUser(view); 
      } 
     }); 
     getEmployees(); 
    } 
    public void registerUser(View view) { 
     RequestParams params = new RequestParams(); 

     String employeeId = etemployeeId.getText().toString(); 
     String firstName = etfirstName.getText().toString(); 
     params.put("employeeId", employeeId); 
     params.put("firstName", firstName); 

    EmployeeRestClient.post("RestExample/employee", params, new JsonHttpResponseHandler(){ 
      @Override 
      public void onSuccess(int statusCode, Header[] headers, JSONObject response) { 
       super.onSuccess(statusCode, headers, response); 
Log.d("Callback", "onSuccess response"); 
       Toast.makeText(MainActivity.this, 
         "You are successfully registered!", Toast.LENGTH_LONG).show(); 
        } 
     @Override 
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { 
        super.onFailure(statusCode, headers, responseString, throwable); 
        Log.d("Callback", "onFailure responseString"); 
        Toast.makeText(MainActivity.this, 
          "failed", Toast.LENGTH_LONG).show(); 
       }     
      }); 
     } 
    } 

답변

0

두 가지 옵션이 있습니다. 하나 : 헤더를 추가하려면 EmployeeRestClient에서 .addHeader (headerKey, headerValue)를 호출하면됩니다. 둘째 : 비슷한 경우 JSONObject를 StringEntity로 감싸서 사용자를 배치해야했지만 RequestParams는 나에게 적합하지 않았습니다. 이 경우 네 번째 매개 변수는 내용 유형이기도합니다. 당신은 둘 다 필요하지 않습니다, 그들은 서로를 무시합니다. 어떤 경우 든 커서를 post 메소드 위에 놓고 Ctrl + P를 눌러 매개 변수 정보 및 대체 매개 변수를 찾으십시오.

관련 문제