错误:IllegalArgumentException:无法为retrofit2.Response

时间:2017-01-24 14:38:17

标签: android retrofit2 android-sensors restful-architecture

我已经完成了本教程(http://www.duchess-france.org/accelerometer-time-series-and-prediction-with-android-cassandra-and-spark/)来创建Accelerometer Rest API,只是为了查看我的localhost服务器端点(http://192.168.0.104/acceleration)上的数据值。但是我面临一个错误"无法为retrofit2.Response"

创建呼叫适配器

然而,在教程中使用了Retrofit(< 2.0),我使用的是Retorfit2.0(2.1)。因此,根据更新的库,很少进行任何更改。

以下是 AccelerometerAct.java

package accelerometer.sensor.com.acceleration;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Date;

import accelerometer.sensor.com.acceleration.model.Acceleration;
import retrofit2.Retrofit;



public class AccelerometerAct extends AppCompatActivity implements SensorEventListener{

    private String restURL;
    private TextView acceleration;
    private Button myStartButton;
    private Button myStopButton;

    private AccelerometerAPI accelerometerAPI;

    private SensorManager sm;
    private Sensor accelerometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accelerometer_activity);
        acceleration = (TextView) findViewById(R.id.acc);


        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        initRestApi();
        initActionButtons();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        Acceleration capturedAcceleration = getAccelerationFromSensor(event);
        updateTextView(capturedAcceleration);
        new SendAccelerationAsyncTask().execute(capturedAcceleration);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        //Do nothing
    }

    private void initRestApi() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            restURL = extras.getString(MainActivity.URL);
        }

        Retrofit restAdapter = new Retrofit.Builder()
                .baseUrl(restURL)
                .build();

        accelerometerAPI = restAdapter.create(AccelerometerAPI.class);
    }


    private void initActionButtons() {
        myStartButton = (Button) findViewById(R.id.start);
        myStopButton = (Button) findViewById(R.id.stop);

        myStartButton.setVisibility(View.VISIBLE);
        myStopButton.setVisibility(View.GONE);

        myStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSensor();
                myStartButton.setVisibility(View.GONE);
                myStopButton.setVisibility(View.VISIBLE);
            }
        });

        myStopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopSensor();
                myStartButton.setVisibility(View.VISIBLE);
                myStopButton.setVisibility(View.GONE);
                finish();
            }
        });
    }

    private void startSensor() {
        sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    private void stopSensor() {
        sm.unregisterListener(this);
    }


    private void updateTextView(Acceleration capturedAcceleration) {
        acceleration.setText("X:" + capturedAcceleration.getX() +
                "\nY:" + capturedAcceleration.getY() +
                "\nZ:" + capturedAcceleration.getZ() +
                "\nTimestamp:" + capturedAcceleration.getTimestamp());
    }


    private Acceleration getAccelerationFromSensor(SensorEvent event) {
        long timestamp = (new Date()).getTime() + (event.timestamp - System.nanoTime()) / 1000000L;
        return new Acceleration(event.values[0], event.values[1], event.values[2], timestamp);
    }


    private class SendAccelerationAsyncTask extends AsyncTask<Acceleration, Void, Void>{

        @Override
        protected Void doInBackground(Acceleration... params) {
            try {
                accelerometerAPI.sendAccelerationValues(params[0]);
            } catch(Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}

这是我的 MainActivity.java

package accelerometer.sensor.com.acceleration;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    public static final String URL = "restURL";
    private EditText restURL;

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

        restURL = (EditText) findViewById(R.id.inUrl);

        final Button myStartButton = (Button) findViewById(R.id.startAct);
        myStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AccelerometerAct.class);
                intent.putExtra(URL, restURL.getText().toString());
                startActivity(intent);
            }
        });
    }
}

我的 Acceleration.java 模型

    package accelerometer.sensor.com.acceleration.model;



public class Acceleration {

    private long timestamp;
    private double x;
    private double y;
    private double z;

    public Acceleration(float x_value, float y_value, float z_value, long     timestamp) {
        x= new Double(""+x_value);
    y= new Double(""+y_value);
    z= new Double(""+z_value);
        this.timestamp = timestamp;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double getZ() {
        return z;
    }
}

这是我的界面AccelerometerAPI.java

package accelerometer.sensor.com.acceleration;


import accelerometer.sensor.com.acceleration.model.Acceleration;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.POST;



public interface AccelerometerAPI {

    @POST("/acceleration")
    Response<Acceleration> sendAccelerationValues(@Body Acceleration acceleration);
}

以下是显示错误的logcat

**01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration 

W/System.err: java.lang.IllegalArgumentException: Unable to create call adapter for retrofit2.Response<accelerometer.sensor.com.acceleration.model.Acceleration>**
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     for method AccelerometerAPI.sendAccelerationValues
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:234)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at java.lang.reflect.Proxy.invoke(Proxy.java:393)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at $Proxy0.sendAccelerationValues(Unknown Source)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at **accelerometer.sensor.com.acceleration.AccelerometerAct$SendAccelerationAsyncTask.doInBackground(AccelerometerAct.java:134)**
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at **accelerometer.sensor.com.acceleration.AccelerometerAct$SendAccelerationAsyncTask.doInBackground(AccelerometerAct.java:129)**
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
01-24 09:25:48.439 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
01-24 09:25:48.441 1968-2023/accelerometer.sensor.com.acceleration W/System.err:     at java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:2)

如果您正在使用Kotlin,请寻找以下这些东西:-

  1. “暂停”已添加到您的API服务类的函数声明中。
  2. 您的数据类(或模型)已正确设置

答案 1 :(得分:0)

我认为,this question可能对您有帮助。

简短回答:在Call<Acceleration>界面中返回AccelerometerAPI

public interface AccelerometerAPI {
    @POST("/acceleration")
    Call<Acceleration> sendAccelerationValues(@Body Acceleration acceleration);
}

还在build.gradle中添加依赖项:

compile 'com.squareup.retrofit:retrofit:2.0.1'
compile 'com.squareup.retrofit:converter-gson:2.0.1'

并像这样构建Retrofit

Retrofit rest = new Retrofit.Builder()
    .baseUrl(endpoint)
    .addConverterFactory(GsonConverterFactory.create())
    .build();