改进android不工作和retrofitError 401

时间:2015-12-17 03:01:01

标签: android retrofit domdocument

我正在使用Retrofit连接电子设备。但它使用不正确的数据进行响应。 请建议我解决这个问题。你们有任何想法是否与连接有关? myUrl:http://11.10.88.23/ajax_control&/

界面Exapi

public interface ExApi {
@GET("/ajax_control&")
public void getFeed(Callback<Document> response);
}

Mainclass

public class MainActivity extends Activity implements RequestInterceptor{
    Button mButton;
    public static final String BASE_URL="http://11.10.88.23/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mButton = (Button) findViewById(R.id.button);
            mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            requestData(BASE_URL);
                    }
            });
    }
    private void requestData(String url){
            RestAdapter adapter=new RestAdapter.Builder()
                    .setEndpoint(url).build();
            ExApi api=adapter.create(ExApi.class);
            api.getFeed(new Callback<Document>() {
                    @Override
                    public void success(Document document, Response response) {
                            Toast.makeText(getApplicationContext(), "value: " + document.getElementsByTagName("LED").item(0).getChildNodes().item(0).getNodeValue(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void failure(RetrofitError retrofitError) {
                            Log.d("lai loi: ", retrofitError.getMessage());
                    }
            });

    }

    @Override
    public void intercept(RequestFacade requestFacade) {
            final String author=encodeCredentialsForBasicAuthorization();
            requestFacade.addHeader("Authorization", author);
    }
    private String encodeCredentialsForBasicAuthorization() {
            final String userAndPassword = "user:admin";
            return "Basic " + Base64.encodeToString(userAndPassword.getBytes(), Base64.NO_WRAP);
    }
}

1 个答案:

答案 0 :(得分:2)

错误manager.search(searchTerm) .timeout(i -> /* do timeout stuff */, 1, TimeUnit.SECONDS) .subscribe(item -> /* do search result stuff */); 表示由于凭据无效而拒绝请求。这种身份验证称为基本身份验证,您也可以在HTTP头中传递这些参数。

这是一篇关于如何在改造中做到这一点的文章:https://futurestud.io/blog/android-basic-authentication-with-retrofit/