我正在尝试使用本教程将数据从我的应用中的xml表单发送到Google电子表格:[TUT] Send app data to a web spreadsheet (Google Sheets)
在完成一步一步的演练后,我能够在谷歌电子表格上写入数据,但点击“发送”按钮后我的应用程序崩溃了。
错误:
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.domain, PID: 9839
java.lang.AbstractMethodError: abstract method "void okhttp3.Callback.onResponse(okhttp3.Call, okhttp3.Response)"
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
questions_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/question_name_input_hint"
android:inputType="textAutoComplete" />
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/emergency_type">
<RadioButton
android:id="@+id/emergency_type_fire"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/type_fire" />
<RadioButton
android:id="@+id/emergency_type_accident"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/type_accident" />
<RadioButton
android:id="@+id/emergency_type_robbery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/type_robbery" />
<RadioButton
android:id="@+id/emergency_type_fight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/type_fight" />
<RadioButton
android:id="@+id/emergency_type_flood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/type_flood" />
<RadioButton
android:id="@+id/emergency_type_others"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/type_other" />
</RadioGroup>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_emergency"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_emergency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/hint_emergency" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_mobile" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/questions_submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="15dip"
android:elevation="6dip"
android:src="@android:drawable/ic_dialog_email"
support:pressedTranslationZ="12dip" />
</LinearLayout>
MainActvity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions_activity);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://docs.google.com/forms/d/e/")
.build();
final QuestionsSpreadsheetWebService spreadsheetWebService = retrofit.create(QuestionsSpreadsheetWebService.class);
nameInputField = (TextView) findViewById(R.id.input_name);
emergencyTypeSelect = (RadioGroup) findViewById(R.id.emergency_type);
emergencyInputField = (TextView) findViewById(R.id.input_emergency);
mobileInputField = (TextView) findViewById(R.id.input_mobile);
findViewById(R.id.questions_submit_button).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String nameInput = nameInputField.getText().toString();
String emergencyType = String.valueOf(emergencyTypeSelect.getCheckedRadioButtonId());
String emergencyInput = emergencyInputField.getText().toString();
String mobileInput = mobileInputField.getText().toString();
Call<Void> completeQuestionnaireCall = spreadsheetWebService.completeQuestionnaire(nameInput, emergencyType,emergencyInput, mobileInput);
completeQuestionnaireCall.enqueue(callCallback);
}
}
);
}
private final Callback<Void> callCallback = new Callback<Void>() {
@Override
public void onResponse(Response<Void> response) {
Log.d("XXX", "Submitted. " + response);
}
@Override
public void onFailure(Throwable t) {
Log.e("XXX", "Failed", t);
}
};
}
听众: QuestionsSpreadsheetWebService.java
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
public interface QuestionsSpreadsheetWebService {
@POST("my_url_endpoint/formResponse")
@FormUrlEncoded
Call<Void> completeQuestionnaire(
@Field("entry.242003698") String name,
@Field("entry.1813695726") String answerQuestionCat,
@Field("entry.261820127") String emergency,
@Field("entry.1790329242") String mobileNo
);
}