Api 与 Postman 一起工作,但不适用于 android 应用程序

     2023-05-07     31

关键词:

【中文标题】Api 与 Postman 一起工作,但不适用于 android 应用程序【英文标题】:Api work with Postman but not in android application 【发布时间】:2019-09-08 13:56:50 【问题描述】:

一个使用 Jersey 构建的 Java 方法,它接受两个参数并存储在数据库中,它可以与邮递员一起正常工作,但是当我在 Android 应用程序中使用它时它不起作用。我尝试使用 Volley 和 Retrofit 发送请求。

服务器端代码:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/register")
public Boolean registerUser(@FormParam("userName") String userName, @FormParam("password") String password) 
    System.out.println(userName+"\t"+password);
    String insertQuery = "INSERT INTO user(user_name,password,status) VALUES(?,?,?)";
    try 
        Connection con = MyConnection.getConnection();
        PreparedStatement prst = con.prepareStatement(insertQuery);
        prst.setString(1, userName);
        prst.setString(2, password);
        prst.setInt(3, 0);
        int count = prst.executeUpdate();
        con.close();
        System.out.println(count+" Row inserted");
        return true;
     catch (SQLException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
    

    return false;

安卓代码:

public void register(final String userName, final String password)

    User user = new User(userName, password, 1);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.1.13:8080/Demo_Application/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    JsonPlaceholderApi jsonPlaceholderApi = retrofit.create(JsonPlaceholderApi.class);

    Call<List<User>> call = jsonPlaceholderApi.register("application/x-www-form-urlencoded", user);
    call.enqueue(new Callback<List<User>>() 
        @Override
        public void onResponse(Call<List<User>> call, Response<List<User>> response) 

            if (!response.isSuccessful())
                Log.e("Response","Something went wrong."+response.toString());
                return;
            

            Log.d("Response",response.toString());

        

        @Override
        public void onFailure(Call<List<User>> call, Throwable t) 
            Log.e("Response",t.getMessage());
        
    );

Postman Response

截击请求:

public void registerVolley(final String userName, final String password)

    Map<String, String> param = new HashMap<>();
    param.put("userName", userName);
    param.put("password",password);

    JsonObjectRequest arrayRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.0.26:8080/Demo_Application/rs/test/register", new JSONObject(param), new com.android.volley.Response.Listener<JSONObject>() 

        @Override
        public void onResponse(JSONObject response) 
            Log.e("Response", response.toString());
        
    , new com.android.volley.Response.ErrorListener() 
        @Override
        public void onErrorResponse(VolleyError error) 
            Log.e("Response", error.toString());
        
    )
        @Override
        protected Map<String, String> getParams() throws AuthFailureError 
            Map<String, String> param = new HashMap<>();
            param.put("userName", userName);
            param.put("password",password);

            return param;
        

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError 
            Map<String, String> header =  new HashMap<>();
            header.put("Content-Type","application/json");
            return header;
        
    ;

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(arrayRequest);


【问题讨论】:

Android 从哪里获得它的数据库连接? android调用不可能直接到registerUser,可以吗?我们将需要查看您的邮递员请求的 curl 以及 android 正在使用的 curl。我敢打赌,如果您将这两个放在一起,您将自己找到问题所在。另外,以明文形式将密码输入某些数据库?你们疯了吗? 在服务器端,我无法获取用户名和密码。@FormParams 是否适用于 android 请求。 请怜悯Rest API。并找到使用改造或凌空抽射的教程。您正在将 Retrofit 、 Volley 和 Httpclinet 混合在一起。 当我要求使用 Volley 时,您认为它的工作原理是什么。 @SaurabhBhandari 没什么好想的。用户特定的一种方式。要么使用改造或凌空抽射。不要乱七八糟 【参考方案1】:

您的改造代码:

JsonPlaceholderApi  jsonPlaceholderApi  = retrofit.create(JsonPlaceholderApi.class);
    Call<Boolean> call = jsonPlaceholderApi.sign("userName", "password");
    call.enqueue(new Callback<Boolean>() 
        @Override
        public void onResponse(Call<Boolean> call, Response<Boolean> response) 

            if (!response.isSuccessful())
                Log.e("Response","Something went wrong."+response.toString());
                return;
            

            Log.d("Response",response.toString());

        

        @Override
        public void onFailure(Call<Boolean> call, Throwable t) 
            Log.e("Response",t.getMessage());
        
    );

您在 jsonPlaceholderApi 中的方法:

@FormUrlEncoded
    @POST("rs/test/register")
    Call<ResponseLogin> signIn(
            @Field("userName") String userName,
            @Field("password") String password
    );

【讨论】:

预期为 BEGIN_ARRAY,但在第 1 行第 6 列路径 $ 处为 BOOLEAN null null com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列 'user_name' 在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)中不能为空 你上面的代码是android的还是Server的?如果是android的,请分享你在postman中给出的请求,我会给你改造代码。 好的。因为您的响应返回布尔值,并且您期望在 android 中列出列表。请分享您的改造代码和您在邮递员中得到的响应。 看到你的服务器代码返回布尔值,你期待 > 调用。它将如何工作?【参考方案2】:

proguard-rules.pro

中添加以下代码
-keepattributes *Annotation*
-keepclassmembers class ** 
    @org.greenrobot.eventbus.Subscribe <methods>;

-keep enum org.greenrobot.eventbus.ThreadMode  *; 
-keep class com.app.appname.model.**  *; 

注意:用模型文件夹更改最后一行

【讨论】:

使用 JWT 访问 Rest API 适用于 Postman 但不适用于 Axios

】使用JWT访问RestAPI适用于Postman但不适用于Axios【英文标题】:AccesstoRestAPIusingJWTworkswithPostmanbutdoesnotworkwithAxios【发布时间】:2020-07-2108:50:50【问题描述】:我有一个使用Laravel制作的Apirest,并受JWT保护。登录正常工作,但是,当... 查看详情

使用 CORS 与 LocomotiveJs 一起休息 API。适用于本地机器,但不适用于 Amazon 或 Heroku

】使用CORS与LocomotiveJs一起休息API。适用于本地机器,但不适用于Amazon或Heroku【英文标题】:RestAPIwithLocomotiveJsusingCORS.WorksonlocalmachinebutnotonAmazonorHeroku【发布时间】:2014-03-0403:30:56【问题描述】:使用CORS和LocomotiveJs的RestAPI。适用... 查看详情

POST 请求适用于 Postman,但不适用于 Guzzle

】POST请求适用于Postman,但不适用于Guzzle【英文标题】:POSTRequestworkswithPostman,butnotwithGuzzle【发布时间】:2017-08-0623:17:38【问题描述】:在我的Laravel应用程序中,我需要定期使用Guzzle将数据POST到API。API使用不记名令牌进行身份验... 查看详情

Uber API 端点不适用于真实的服务器域,但可以很好地与沙箱一起使用

...berAPI端点不适用于真实的服务器域,但可以很好地与沙箱一起使用【英文标题】:UberAPIendpointsnotworkingwithrealserverdomainbutworkingwellwithsandbox【发布时间】:2015-11-0923:50:12【问题描述】:我已经测试UberAPI一个月了,最近看到了一些不... 查看详情

POST 请求适用于 Postman,但不适用于 axios.post()

】POST请求适用于Postman,但不适用于axios.post()【英文标题】:POSTrequestworkswithPostmanbutnotwithaxios.post()【发布时间】:2022-01-2107:35:53【问题描述】:我在React-JS和REST-api中使用节点创建了一个Note应用程序,我同时运行前端和服务器。... 查看详情

Api 调用适用于从 Postman 获取的令牌,但不适用于通过 nodejs 应用程序获取的令牌

】Api调用适用于从Postman获取的令牌,但不适用于通过nodejs应用程序获取的令牌【英文标题】:ApicallworkswithtokenacquiredfromPostman,butnotfromthetokenacquiredvianodejsapp【发布时间】:2021-05-0904:03:50【问题描述】:所以我正在尝试对第三方API... 查看详情

使用 Postman 工具向 Yammer API 发出 Get 请求,但不适用于 Vue-Resource

】使用Postman工具向YammerAPI发出Get请求,但不适用于Vue-Resource【英文标题】:MakingGetrequesttoYammerAPIworksusingPostmantoolbutnotwithVue-Resource【发布时间】:2021-05-2206:40:58【问题描述】:我正在尝试将YammerAPI集成到我的Vue.JS项目中,对于Http... 查看详情

C# API 调用不能与 HttpWebRequest 一起使用,但可以与 Postman 一起使用

】C#API调用不能与HttpWebRequest一起使用,但可以与Postman一起使用【英文标题】:C#APICalldoesn\'tworkwithHttpWebRequestbutdoworkwithPostman【发布时间】:2018-03-0616:24:28【问题描述】:我有以下邮递员请求:这会按预期返回一个URL:我正在尝... 查看详情

Multipart POST 适用于 Postman,但不适用于 Angular Http Client

】MultipartPOST适用于Postman,但不适用于AngularHttpClient【英文标题】:MultipartPOSTworksfromPostmanbutnotfromAngularHttpClient【发布时间】:2021-04-2712:40:08【问题描述】:我正在尝试上传类似于谷歌地图的评论,其中附有一些内容和可选图片。... 查看详情

POST 请求适用于 Postman,但不适用于 axios 或 .fetch()

】POST请求适用于Postman,但不适用于axios或.fetch()【英文标题】:POSTrequestworkswithPostman,butnotwithaxiosor.fetch()【发布时间】:2018-06-2218:47:28【问题描述】:我有一个问题,我已经处理了几天,但找不到解决方案。我用Lumen创建了一个AP... 查看详情

Google API 密钥不适用于 Android

...在我的应用程序中使用GoogleAPI,但只能让调试ssh与GoogleAPI一起工作。我有一个我签名的密钥库,过去我对以前的应用程序做过同样的事情并且没有问题。我还启用了所有API。我正在使用发布apk签署我的应用程序。我在SSH1中使用... 查看详情

使用 C# HttpClient API 和 postman 测试的区别?客户端调用适用于邮递员,但不适用于 C# httpClient getAsync

】使用C#HttpClientAPI和postman测试的区别?客户端调用适用于邮递员,但不适用于C#httpClientgetAsync【英文标题】:DifferencesbetweenusingC#HttpClientAPIandthepostmantesting?Clientcallworksonpostman,butnotC#httpClientgetAsync【发布时间】:2016-12-1319:37:44【问... 查看详情

路径不适用于 Blazor 中允许的 IdentityServer CORS 端点,但适用于 Postman

...路径不适用于Blazor中允许的IdentityServerCORS端点,但适用于Postman【英文标题】:PathwasnotforanallowedIdentityServerCORSendpointfromBlazor,butworksinPostman【发布时间】:2020-02-1220:24:48【问题描述】:在不同的项目中有一个带有IdentityServer4的Identit... 查看详情

Woocommerce API Post 方法不适用于通过 POSTMAN 的 HTTP 请求 - 获取无效签名

】WoocommerceAPIPost方法不适用于通过POSTMAN的HTTP请求-获取无效签名【英文标题】:WoocommerceAPIPostmethodnotworkingwithHTTPrequestViaPOSTMAN-GettingInvalidsignature【发布时间】:2020-11-1222:16:02【问题描述】:WoocommerceAPI版本-/wc/v3/Get方法工作正常-... 查看详情

加载 JSON 适用于 XMLHttpRequest 但不适用于 jQuery

...谷歌存储中获取JSON文件的内容。我可以使它与XMLHttpRequest一起工作,但不能与jquery一起工作,我不知道为什么。这行得通:varxhttp=newXMLHtt 查看详情

“prime sieve”的以下代码不适用于发布模式,但可以完美地与调试模式一起使用

...的以下代码不适用于发布模式,但可以完美地与调试模式一起使用【英文标题】:Thefollowingcodefor"primesieve"isnotworkingwithReleasemode,,butworksperfectlywithDebugmode【发布时间】:2012-08-1723:13:05【问题描述】:“primesieve”的以下代码... 查看详情

GET 请求适用于 Postman,但为啥它不适用于 ReactJS fetch?

】GET请求适用于Postman,但为啥它不适用于ReactJSfetch?【英文标题】:GETrequestworkswithPostmanbutwhydoesn\'titworkwithReactJSfetch?GET请求适用于Postman,但为什么它不适用于ReactJSfetch?【发布时间】:2016-12-1605:31:29【问题描述】:当我在Postman... 查看详情

WebSocket 不适用于 https 请求,并且它与使用 PHP 棘轮库的 http 一起正常工作

】WebSocket不适用于https请求,并且它与使用PHP棘轮库的http一起正常工作【英文标题】:WebSocketisnotworkingwithhttpsrequestanditsworkingfinewithhttpusingPHPratchetLibrary【发布时间】:2019-03-1108:55:06【问题描述】:我对WebSocket不太了解,我已经使... 查看详情