Stringをサーバーに送信しようとしていますが、問題があります。Retrofit 2を使用したAndroidでのHTTP POSTリクエスト
これは私の方法です。
ppublic void intervalPost(View view) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://requestb.in/")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostInterface service = retrofit.create(PostInterface.class);
Call<ResponseBody> call = service.postTime(time2);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("Postavke", "Reached onResponse");
if (!response.isSuccess()) {
Log.d("Postavke", "No Success");
}
}
@Override
public void onFailure(Call<ResponseBody> call,Throwable t) {
// Toast for the moment
// Appropriate error handling code should be present here
Toast.makeText(Postavke.this, "Failed !", Toast.LENGTH_LONG).show();
}
});
}
およびインターフェース:
public interface PostInterface {
@FormUrlEncoded
@POST("/1b2bqxl1")
Call<ResponseBody> postTime(@Field("interval") String time);
}
私はスピナーから文字列TIME2を取得しています:あなたはスピナーに間隔を選んだとき
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String label = spinner.getSelectedItem().toString();
tokens = new StringTokenizer(label, " ");
time = tokens.nextToken().trim();
timeConvert = Integer.parseInt(time);
if (timeConvert == 1 || timeConvert == 2 || timeConvert == 3 || timeConvert == 6 || timeConvert == 12)
timeConvert = timeConvert * 60;
time2 = String.valueOf(timeConvert);
}
アイデアがある、ボタンクリックサーバーにtime2を送信する必要があります。
私はこのエラーを取得していたアプリケーションを実行すると:java.lang.IllegalStateException: Could not execute method for android:onClick
を私の質問は、このエラーを修正する方法であり、あなたがPOSTメソッドをチェックすることができる場合も、私はそれがうまくいくかはわかりません。
レイアウト:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical"
>
<TextView
android:text="@string/odaberi_interval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/odabirintervala"
android:textSize="20sp"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:textColor="#148299"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:layout_toRightOf="@id/odabirintervala"
android:drawSelectorOnTop="true"
></Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Promijeni interval"
android:layout_below="@id/spinner"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:onClick="intervalPost"
android:id="@+id/intervalButton"
/>
</RelativeLayout>
で
を変更しようと、あなたのレイアウトを表示することができますか?あなたの質問に更新した後、私にタグをつけてください。 – TruongHieu
エラーメッセージはレトロフィットとは関係ありません。スピナーの 'android:onClick'属性を設定していますか? – akash93
@TruongHieu質問にレイアウトを追加しました。 – mustangws