发布Robospice和okHttp

时间:2015-05-15 21:00:47

标签: post okhttp robospice

我使用Robospice和okHttp执行POST:

public String loadDataFromNetwork() throws Exception {
    uriBuilder = Uri.parse(url).buildUpon();
    uri = new URI(uriBuilder.build().toString());
    tmp = "user=" + user + "&password=" + pwd
    HttpURLConnection connect = new OkUrlFactory(client).open(uri.toURL());
    // Send post request
    connect.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(connect.getOutputStream());
    wr.writeBytes(tmp);
    wr.flush();
    wr.close();
    // Read the response
    in = connect.getInputStream();
}

是否有更好的方式发送帖子(使用Robospice / okHttp )?

NB :我的代码工作正常,只是想知道它是否正确...

1 个答案:

答案 0 :(得分:0)

问题在于,如果我想像那样使用okHttp POST:

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url("https://api.github.com/markdown/raw")
            .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, parameters))
            .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());

使用Robospice,RequestBody和newCall以及isSuccessful无法解决!

我是否有解决方案使用带有Robospice的okHttp帖子? (我知道怎么做GET,但不知道POST ......)