[英]HTTP POST request in Android using Retrofit 2
我正在嘗試將 String 發送到服務器,但我遇到了問題。
這是我的方法。
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>
從錯誤中您會收到消息“@Field 參數只能與表單編碼一起使用。”
這意味着,您需要將 @FormUrlEncoded 放在您的界面上
@FormUrlEncoded
@POST("/1gics1o1")
Call<Response> postTime(@Field("interval") String time);
錯誤“Caused by: android.os.NetworkOnMainThreadException”表示您的網絡進程在主線程中的時間過長,因為您沒有使用異步方法。 嘗試改變
call.execute();
使用入隊方法
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");
}else{
Log.d("Postavke", "responsebody => "+ response.body().toString());
}
}
@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();
}
});
您在主線程上進行改造請求。 嘗試使用AsyncTask或RxAndroid或Volley 。
你也可以試試這個答案: Retrofit 在主線程上進行網絡調用嗎?
編輯。 例子:
public void intervalPost(View view) {
try {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://requestb.in/")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostInterface service = retrofit
.create(PostInterface.class);
.postTime(time2)
.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, Response<Response> response) {
// code here
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
// code here
}
});
} catch (IOException Ex) {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.