2015-05-12 20 views
14

Tôi đang cố gắng không thành công để sử dụng API trên Android bằng thư viện Retrofit nhưng trong khi sử dụng POSTMAN tôi có thể thấy kết quả mong đợi.Gửi yêu cầu đăng bài với thông số sử dụng trang bị thêm

Postman LẬP

  • Url api (base + controller) Phương pháp

  • HTTP thiết phải POST

  • Clicked từ dữ liệu hoặc x-www-form- urlencoded

  • Sau đó, tôi chuyển hai tham số trên các trường khóa/giá trị.

Retrofit ANDROID LẬP

@POST("/GetDetailWithMonthWithCode") 
void getLandingPageReport(@Query("code") String code, 
          @Query("monthact") String monthact, 
          Callback<LandingPageReport> cb); 

@FormUrlEncoded 
@POST("/GetDetailWithMonthWithCode") 
void getLandingPageReport(@Field("code") String code, 
          @Field("monthact") String monthact, 
          Callback<LandingPageReport> cb); 

Không ai trong số các lựa chọn này hoạt động. Nhưng tôi nhận được kết quả là {}.

CẬP NHẬT

thiết lập tương tự bằng cách sử dụng các lớp chuẩn HttpClient (và HttpPost) hoạt động tốt.

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url); 

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); 
urlParameters.add(new BasicNameValuePair("code", "testcode")); 
urlParameters.add(new BasicNameValuePair("monthact", "feb-2015")); 

post.setEntity(new UrlEncodedFormEntity(urlParameters)); 

HttpResponse response = client.execute(post); 

Tại sao tôi không thể thực hiện yêu cầu này và nhận được phản hồi chính xác khi trang bị thêm?

CẬP NHẬT 2

@POST("/GetDetailWithMonthWithCode") 
void getLandingPageReport(@Query("code") String code, 
          @Query("monthact") String monthact, 
          Callback<List<LandingPageReport>> cb); 

@FormUrlEncoded 
@POST("/GetDetailWithMonthWithCode") 
void getLandingPageReport(@Field("code") String code, 
          @Field("monthact") String monthact, 
          Callback<List<LandingPageReport>>> cb); 

Sau khi chơi đùa Tôi nghĩ rằng tôi đã tìm thấy nguồn gốc của vấn đề. Tôi đã cập nhật mã trang bị thêm của mình để nhận được List<LandingPageReport>. Nhưng bây giờ lỗi này xảy ra

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Lý do là tôi tiêu thụ 2 api (webapi và wcf). Tất cả các đáp ứng json khác của tôi là mảng các đối tượng. [{}, {}] nhưng trong cuộc gọi này, tôi đã nhận được điều này

{ 
    "GetDetailWithMonthWithCodeResult": [ 
    { 
     "code": "test", 
     "field1": "test", 
    } 
    ] 
} 

Nhưng tôi vẫn không thể phân tích cú pháp phản hồi.

+0

'GetDetailWithMonth & Code' phần này có lẽ là không hợp lệ –

+0

những gì bạn có ý nghĩa với điều đó? làm thế nào tôi có thể làm điều đó làm việc? trên iOS hoạt động và những gì tôi làm là tôi chuyển đổi thành NSDATA chuỗi này "par1 = adads & par2 = asd" và đưa nó vào yêu cầu. – Adam

+0

biểu tượng '&' hoạt động như một dấu phân cách cho các tham số yêu cầu, vì vậy bạn nên đổi tên phương thức API (bên trong API) thành một cái gì đó như 'GetDetailWithMonthAndCode' –

Trả lời

10

Tôi đã tìm thấy giải pháp. Vấn đề là một vấn đề trong cấu trúc lớp học của tôi. Vì vậy, tôi cập nhật chúng như các mẫu sau.

public class LandingPageReport { 

    private ArrayList<LandingPageReportItem> GetDetailWithMonthWithCodeResult; 

    // + Getter Setter methods 
} 

public class LandingPageReportItem { 

    private String code; 

    private String field1; 

    // + Getter Setter methods 
} 

Và sau đó tôi sử dụng cấu hình này trang bị thêm

@POST("/GetDetailWithMonthWithCode") 
void getLandingPageReport(@Field("code") String code, 
          @Field("monthact") String monthact, 
          Callback<LandingPageReport> cb); 
+0

Câu trả lời hay ... Hãy giữ nó. –

13

Đây là một giải pháp đơn giản mà chúng ta không cần phải sử dụng JSON

public interface RegisterAPI { 
@FormUrlEncoded 
@POST("/RetrofitExample/insert.php") 
public void insertUser(
     @Field("name") String name, 
     @Field("username") String username, 
     @Field("password") String password, 
     @Field("email") String email, 
     Callback<Response> callback); 
} 

phương pháp để gửi dữ liệu

private void insertUser(){ 
    //Here we will handle the http request to insert user to mysql db 
    //Creating a RestAdapter 
    RestAdapter adapter = new RestAdapter.Builder() 
      .setEndpoint(ROOT_URL) //Setting the Root URL 
      .build(); //Finally building the adapter 

    //Creating object for our interface 
    RegisterAPI api = adapter.create(RegisterAPI.class); 

    //Defining the method insertuser of our interface 
    api.insertUser(

      //Passing the values by getting it from editTexts 
      editTextName.getText().toString(), 
      editTextUsername.getText().toString(), 
      editTextPassword.getText().toString(), 
      editTextEmail.getText().toString(), 

      //Creating an anonymous callback 
      new Callback<Response>() { 
       @Override 
       public void success(Response result, Response response) { 
        //On success we will read the server's output using bufferedreader 
        //Creating a bufferedreader object 
        BufferedReader reader = null; 

        //An string to store output from the server 
        String output = ""; 

        try { 
         //Initializing buffered reader 
         reader = new BufferedReader(new InputStreamReader(result.getBody().in())); 

         //Reading the output in the string 
         output = reader.readLine(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

        //Displaying the output as a toast 
        Toast.makeText(MainActivity.this, output, Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       public void failure(RetrofitError error) { 
        //If any error occured displaying the error as toast 
        Toast.makeText(MainActivity.this, error.toString(),Toast.LENGTH_LONG).show(); 
       } 
      } 
    ); 
} 

Bây giờ chúng ta có thể nhận được po st yêu cầu bằng cách sử dụng php aur bất kỳ kịch bản phía máy chủ khác.

Nguồn Android Retrofit Tutorial

-1

Cách quyến rũ nhất trong quan điểm của tôi là gửi nó trong POST Body này có nghĩa là bạn sẽ có một tạo một POJO mới nhưng một số có thể muốn thực hiện này nhiều nhất.

public interface APIInterface { 
    @POST("/GetDetailWithMonthWithCode") 
    List<LandingPageReport> getLandingPageReport(@Body Report report); 
} 

Sau đó, hãy tạo POJO của bạn bằng hàm tạo, getters và setters.

public static class Report { 
    private String code; 
    private String monthact; 

    public Report(String code, String monthact) { 
     this.code = code; 
     this.monthact = monthact; 
    } 

    // Getters and Setters... 
} 

Và chỉ gọi nó là cách thông thường.

Call<List<Report>> request = apiInterface 
    .createRetrofitAPIInterface() 
    .getLandingPageReport(new Report(code, monthact)); 

Tôi hy vọng điều đó đủ rõ ràng!

2

build.gradle

 compile 'com.google.code.gson:gson:2.6.2' 

     compile 'com.squareup.retrofit2:retrofit:2.1.0'// compulsory 

     compile 'com.squareup.retrofit2:converter-gson:2.1.0' //for retrofit conversion 
========================================================= 
**Login APi Put Two Parameter** 
========================================================= 
    { 
     "UserId": "1234", 
     "Password":"1234" 
    } 

========================================================= 
**Login Response** 
========================================================= 
    { 
     "UserId": "1234", 
     "FirstName": "Keshav", 
     "LastName": "Gera", 
     "ProfilePicture": "312.113.221.1/GEOMVCAPI/Files/1.500534651736E12p.jpg" 
    } 
========================================================= 
**APIClient.java** 
========================================================= 

    import retrofit2.Retrofit; 
    import retrofit2.converter.gson.GsonConverterFactory; 

    class APIClient { 

     public static final String BASE_URL = "Your Base Url "; 
     private static Retrofit retrofit = null; 

     public static Retrofit getClient() { 
      if (retrofit == null) { 
       retrofit = new Retrofit.Builder() 
         .baseUrl(BASE_URL) 
         .addConverterFactory(GsonConverterFactory.create()) 
         .build(); 
      } 
      return retrofit; 
     } 
    } 
========================================================= 
**APIInterface interface** 
========================================================= 
    interface APIInterface { 

     @POST("LoginController/Login") 
     Call<LoginResponse> createUser(@Body LoginResponse login); 
    } 

========================================================= 
**Login Pojo** 
========================================================= 

    package pojos; 

    import com.google.gson.annotations.SerializedName; 

    public class LoginResponse { 


     @SerializedName("UserId") 
     public String UserId; 
     @SerializedName("FirstName") 
     public String FirstName; 
     @SerializedName("LastName") 
     public String LastName; 
     @SerializedName("ProfilePicture") 
     public String ProfilePicture; 
     @SerializedName("Password") 
     public String Password; 
     @SerializedName("ResponseCode") 
     public String ResponseCode; 
     @SerializedName("ResponseMessage") 
     public String ResponseMessage; 

     public LoginResponse(String UserId, String Password) { 
      this.UserId = UserId; 
      this.Password = Password; 
     } 

     public String getUserId() { 
      return UserId; 
     } 

     public String getFirstName() { 
      return FirstName; 
     } 

     public String getLastName() { 
      return LastName; 
     } 

     public String getProfilePicture() { 
      return ProfilePicture; 
     } 

     public String getResponseCode() { 
      return ResponseCode; 
     } 

     public String getResponseMessage() { 
      return ResponseMessage; 
     } 
    } 

========================================================= 
**MainActivity** 
========================================================= 

    package com.keshav.retrofitloginexampleworkingkeshav; 

    import android.app.Dialog; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    import pojos.LoginResponse; 
    import retrofit2.Call; 
    import retrofit2.Callback; 
    import retrofit2.Response; 
    import utilites.CommonMethod; 

    public class MainActivity extends AppCompatActivity { 

     TextView responseText; 
     APIInterface apiInterface; 

     Button loginSub; 
     EditText et_Email; 
     EditText et_Pass; 
     private Dialog mDialog; 
     String userId; 
     String password; 

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

      apiInterface = APIClient.getClient().create(APIInterface.class); 

      loginSub = (Button) findViewById(R.id.loginSub); 
      et_Email = (EditText) findViewById(R.id.edtEmail); 
      et_Pass = (EditText) findViewById(R.id.edtPass); 

      loginSub.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (checkValidation()) { 
         if (CommonMethod.isNetworkAvailable(MainActivity.this)) 
          loginRetrofit2Api(userId, password); 
         else 
          CommonMethod.showAlert("Internet Connectivity Failure", MainActivity.this); 
        } 
       } 
      }); 
     } 

     private void loginRetrofit2Api(String userId, String password) { 
      final LoginResponse login = new LoginResponse(userId, password); 
      Call<LoginResponse> call1 = apiInterface.createUser(login); 
      call1.enqueue(new Callback<LoginResponse>() { 
       @Override 
       public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) { 
        LoginResponse loginResponse = response.body(); 

        Log.e("keshav", "loginResponse 1 --> " + loginResponse); 
        if (loginResponse != null) { 
         Log.e("keshav", "getUserId   --> " + loginResponse.getUserId()); 
         Log.e("keshav", "getFirstName  --> " + loginResponse.getFirstName()); 
         Log.e("keshav", "getLastName  --> " + loginResponse.getLastName()); 
         Log.e("keshav", "getProfilePicture --> " + loginResponse.getProfilePicture()); 

         String responseCode = loginResponse.getResponseCode(); 
         Log.e("keshav", "getResponseCode --> " + loginResponse.getResponseCode()); 
         Log.e("keshav", "getResponseMessage --> " + loginResponse.getResponseMessage()); 
         if (responseCode != null && responseCode.equals("404")) { 
          Toast.makeText(MainActivity.this, "Invalid Login Details \n Please try again", Toast.LENGTH_SHORT).show(); 
         } else { 
          Toast.makeText(MainActivity.this, "Welcome " + loginResponse.getFirstName(), Toast.LENGTH_SHORT).show(); 
         } 
        } 
       } 

       @Override 
       public void onFailure(Call<LoginResponse> call, Throwable t) { 
        Toast.makeText(getApplicationContext(), "onFailure called ", Toast.LENGTH_SHORT).show(); 
        call.cancel(); 
       } 
      }); 
     } 

     public boolean checkValidation() { 
      userId = et_Email.getText().toString(); 
      password = et_Pass.getText().toString(); 

      Log.e("Keshav", "userId is -> " + userId); 
      Log.e("Keshav", "password is -> " + password); 

      if (et_Email.getText().toString().trim().equals("")) { 
       CommonMethod.showAlert("UserId Cannot be left blank", MainActivity.this); 
       return false; 
      } else if (et_Pass.getText().toString().trim().equals("")) { 
       CommonMethod.showAlert("password Cannot be left blank", MainActivity.this); 
       return false; 
      } 
      return true; 
     } 
    } 
=========================================== 
**CommonMethod.java** 
=========================================== 

    public class CommonMethod { 


     public static final String DISPLAY_MESSAGE_ACTION = 
       "com.codecube.broking.gcm"; 

     public static final String EXTRA_MESSAGE = "message"; 

     public static boolean isNetworkAvailable(Context ctx) { 
      ConnectivityManager connectivityManager 
        = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
      return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
     } 

     public static void showAlert(String message, Activity context) { 

      final AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setMessage(message).setCancelable(false) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

         } 
        }); 
      try { 
       builder.show(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
    } 
========================================================= 
**activity_main.xml** 
========================================================= 
    <LinearLayout android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:orientation="vertical" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 

      <ImageView 
       android:id="@+id/imgLogin" 
       android:layout_width="200dp" 
       android:layout_height="150dp" 
       android:layout_gravity="center" 
       android:layout_marginTop="20dp" 
       android:padding="5dp" 
       android:background="@mipmap/ic_launcher_round" 
       /> 

      <TextView 
       android:id="@+id/txtLogo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/imgLogin" 
       android:layout_centerHorizontal="true" 
       android:text="Holostik Track and Trace" 
       android:textSize="20dp" 
       android:visibility="gone" /> 

      <android.support.design.widget.TextInputLayout 
       android:id="@+id/textInputLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="@dimen/box_layout_margin_left" 
       android:layout_marginRight="@dimen/box_layout_margin_right" 
       android:layout_marginTop="8dp" 
       android:padding="@dimen/text_input_padding"> 

       <EditText 
        android:id="@+id/edtEmail" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="5dp" 
        android:ems="10" 
        android:fontFamily="sans-serif" 
        android:gravity="top" 
        android:hint="Login ID" 
        android:maxLines="10" 
        android:paddingLeft="@dimen/edit_input_padding" 
        android:paddingRight="@dimen/edit_input_padding" 
        android:paddingTop="@dimen/edit_input_padding" 
        android:singleLine="true"></EditText> 

      </android.support.design.widget.TextInputLayout> 

      <android.support.design.widget.TextInputLayout 
       android:id="@+id/textInputLayout2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/textInputLayout1" 
       android:layout_marginLeft="@dimen/box_layout_margin_left" 
       android:layout_marginRight="@dimen/box_layout_margin_right" 
       android:padding="@dimen/text_input_padding"> 

       <EditText 
        android:id="@+id/edtPass" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:focusable="true" 
        android:fontFamily="sans-serif" 
        android:hint="Password" 
        android:inputType="textPassword" 
        android:paddingLeft="@dimen/edit_input_padding" 
        android:paddingRight="@dimen/edit_input_padding" 
        android:paddingTop="@dimen/edit_input_padding" 
        android:singleLine="true" /> 

      </android.support.design.widget.TextInputLayout> 

      <RelativeLayout 
       android:id="@+id/rel12" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/textInputLayout2" 
       android:layout_marginTop="10dp" 
       android:layout_marginLeft="10dp" 
       > 

       <Button 
        android:id="@+id/loginSub" 
        android:layout_width="wrap_content" 
        android:layout_height="45dp" 
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true" 
        android:background="@drawable/border_button" 
        android:paddingLeft="30dp" 
        android:paddingRight="30dp" 
        android:layout_marginRight="10dp" 
        android:text="Login" 
        android:textColor="#ffffff" />  
      </RelativeLayout> 

    </LinearLayout> 
Các vấn đề liên quan