使用OKHttp获取我的restful服务响应null

时间:2016-12-13 07:51:55

标签: android okhttp restful-url

我是Android的新手,我得到服务响应null但是服务URL在浏览器中运行正常。当我运行我的应用程序屏幕时显示空白,没有数据来自我做错的服务,请帮助我。

我的片段:

public class OKHttpRequest {
OkHttpClient client = new OkHttpClient();

public static final MediaType JSON
        = MediaType.parse("application/json; charset=utf-8");


public String doGetRequest(String url) throws IOException {

    try {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();

    }
    catch (Exception e){
        e.printStackTrace();
    }
   return null;

}



String doPostRequest(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}}

OKHttp类:

char *myArray[] = { "aaaaaa", "ccccc", "bbbbbb", "111111" };

2 个答案:

答案 0 :(得分:0)

首先通过POSTMAN检查您的API,我认为您在Hader中缺少某些参数,或者由于响应不是来自服务器,因此可能在正文中。此外,我建议用户volly进行客户端 - 服务器通信

答案 1 :(得分:0)

此外,您可以使用Rest-client(添加Chrome和Firefox),如果正常使用HttpsURLConnection,它可能会对您有所帮助。

相关问题