繁体   English   中英

改造表单数据POST不返回JSON

[英]Retrofit form-data POST not return JSON

我在使用Apache HttpClient的服务器上尝试此POST并正常工作:

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(BASE_URL + "access.php");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
            nameValuePairs.add(new BasicNameValuePair(PARAMETER_COD, mAccessCode));
            nameValuePairs.add(new BasicNameValuePair(PARAMETER_USU, mUser));
            nameValuePairs.add(new BasicNameValuePair(PARAMETER_PAS, mPassword));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            try {
                HttpResponse response = httpclient.execute(httppost);
                //JSON RESPONSE
                String op = EntityUtils.toString(response.getEntity(), "UTF-8");

            } catch (IOException e) {
                e.printStackTrace();
            }
            //reset the message text field
        } catch (IOException e) {
            e.printStackTrace();
        }

但是我试图在改造中使我们无法使用:

@Multipart
@POST("access.php")
Call<String> authenticate(@Part("cod") String cod, @Part("usu") String usu, @Part("pas") String pas);

私人ApiInterface getInterfaceService(){

    Gson gson = new GsonBuilder()
            .setLenient()
            .create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    final ApiInterface mInterfaceService = retrofit.create(ApiInterface.class);
    return mInterfaceService;
}

private void loginProcessWithRetrofit(final User user){

    ApiInterface mApiService = this.getInterfaceService();
    Call<String> mService = mApiService.authenticate(String.valueOf(user.cod),user.pas,user.usu);
    mService.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {

            //String returnedResponse = mLoginObject.isLogin;
            Toast.makeText(LoginActivity.this, "Returned " + response.body(), Toast.LENGTH_LONG).show();
            showProgress(false);
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            call.cancel();
            Toast.makeText(LoginActivity.this, "Please check your network connection and internet permission", Toast.LENGTH_LONG).show();
            Log.e("Retrofit",t.toString());
        }
    });
}

还可以尝试其他帖子:

@POST("access.php")
Call<String> authenticate(@Body User user);

将HttpPost转换为Retrofit的正确格式是什么? 非常感谢。 问候

您缺少@FormUrlEncoded批注。

@FormUrlEncoded
@POST("access.php")
Call<Object> authenticate(@Field("cod") String cod, @Field("usu") String usu, @Field("pas") String pas);

采用

Call<JsonElement> mService

**代替 **

Call<String> mService;

在所有位置..您在其中添加<String>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM