Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
改造:在android中向服务器发送POST请求_Android_Json_Post_Retrofit - Fatal编程技术网

改造:在android中向服务器发送POST请求

改造:在android中向服务器发送POST请求,android,json,post,retrofit,Android,Json,Post,Retrofit,我正在使用改型来调用API。我正在向API发送post请求,但在回调中,我得到了如下{}的空JSON 以下是改装服务的代码 @POST("/com/searchusers.php") void getUser(@Body JSONObject searchstring, Callback<JSONObject> callBack); @POST(“/com/searchusers.php”) void getUser(@Body JSONObject searchstring,回调

我正在使用改型来调用API。我正在向API发送post请求,但在回调中,我得到了如下{}的空JSON

以下是改装服务的代码

@POST("/com/searchusers.php")
void getUser(@Body JSONObject searchstring, Callback<JSONObject> callBack);
@POST(“/com/searchusers.php”)
void getUser(@Body JSONObject searchstring,回调);
其中searchstring JSON类似于{“search”:“nitesh”}。作为回应,我应该得到用户“nitesh”的详细信息

下面是发送POST请求的代码

RetrofitService mRetrofitService = app.getRetrofitService();
    mRetrofitService.getUser(user, new Callback<JSONObject>() {

        @Override
        public void success(JSONObject result, Response arg1) {
            System.out.println("success, result: " + result);
        }

        @Override
        public void failure(RetrofitError error) {
            System.out.println("failure, error: " + error);
        }
    });
RefughtService mRetrofitService=app.getRefughtService();
mRetrofitService.getUser(用户,新回调(){
@凌驾
public void成功(JSONObject结果,响应arg1){
System.out.println(“成功,结果:+result”);
}
@凌驾
公共无效失败(错误){
System.out.println(“失败,错误:+错误”);
}
});
我得到这个输出 成功,结果:{}

预期产量为 成功,结果:{“name”:“nitesh”,…其余细节}

编辑: 我尝试使用Response而不是JSONObject,比如
回调
,然后将原始响应转换为字符串,得到了预期的结果。但问题是它在字符串中,我想要JSONObject中的响应

RetrofitService mRetrofitService = app.getRetrofitService();
mRetrofitService.getUser(user, new Callback<String>() {

    @Override
    public void success(String result, Response arg1) {
        System.out.println("success, result: " + result);
        JSONObject jsonObject = new JSONObject(result);
    }

    @Override
    public void failure(RetrofitError error) {
        System.out.println("failure, error: " + error);
    }
});
如何使用
回调
获得准确的结果?

它可能会帮助您

JSONObject responseJsonObj = new JSONObject(responseString);

在等待最好的回答之后,我想到了回答我自己的问题。这就是我解决问题的方法

我更改了RestAdapter的转换器,并创建了自己的转换器。下面是我的字符串转换器

static class StringConverter implements Converter {

    @Override
    public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
        String text = null;
        try {
            text = fromStream(typedInput.in());
        } catch (IOException ignored) {/*NOP*/ }

        return text;
    }

    @Override
    public TypedOutput toBody(Object o) {
        return null;
    }

    public static String fromStream(InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String newLine = System.getProperty("line.separator");
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
            out.append(newLine);
        }
        return out.toString();
    }
}
然后我在应用程序类中将这个转换器设置为RestAdapter

RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(BASE_URL)
        .setConverter(new StringConverter())
        .build();
    mRetrofitService = restAdapter.create(RetrofitService.class);
现在,每当我使用改装时,我都会得到字符串响应。然后我转换了字符串JSONObject

RetrofitService mRetrofitService = app.getRetrofitService();
mRetrofitService.getUser(user, new Callback<String>() {

    @Override
    public void success(String result, Response arg1) {
        System.out.println("success, result: " + result);
        JSONObject jsonObject = new JSONObject(result);
    }

    @Override
    public void failure(RetrofitError error) {
        System.out.println("failure, error: " + error);
    }
});
RefughtService mRetrofitService=app.getRefughtService();
mRetrofitService.getUser(用户,新回调(){
@凌驾
公共作废成功(字符串结果,响应arg1){
System.out.println(“成功,结果:+result”);
JSONObject JSONObject=新JSONObject(结果);
}
@凌驾
公共无效失败(错误){
System.out.println(“失败,错误:+错误”);
}
});

因此,我得到了JSON形式的结果。然后,我根据需要解析了这个JSON。

mRetrofitService.getUser(user,new Callback()和public void success(String result,Response arg1)。JSONObject应该是String,或者String result应该是JSONObject result。我尝试了您的代码,发现了这一点。我已经更正了代码,使用
回调
而不是
回调