JSON

OkHttpʹÓý̳Ì(3)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2015-10-11 10:16 ÎÒÒªÆÀÂÛ( )

Post·½Ê½Ìá½»Á÷ ÒÔÁ÷µÄ·½Ê½POSTÌá½»ÇëÇóÌå¡£ÇëÇóÌåµÄÄÚÈÝÓÉÁ÷дÈë²úÉú¡£Õâ¸öÀý×ÓÊÇÁ÷Ö±½ÓдÈëOkioµÄBufferedSink¡£ÄãµÄ³ÌÐò¿ÉÄÜ»áʹÓÃOutputStream£¬Äã¿ÉÒÔʹÓÃBufferedSink.outputStream()À´»ñÈ £ public static f

Post·½Ê½Ìá½»Á÷
ÒÔÁ÷µÄ·½Ê½POSTÌá½»ÇëÇóÌå¡£ÇëÇóÌåµÄÄÚÈÝÓÉÁ÷дÈë²úÉú¡£Õâ¸öÀý×ÓÊÇÁ÷Ö±½ÓдÈëOkioµÄBufferedSink¡£ÄãµÄ³ÌÐò¿ÉÄÜ»áʹÓÃOutputStream£¬Äã¿ÉÒÔʹÓÃBufferedSink.outputStream()À´»ñÈ¡¡£

public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { RequestBody requestBody = new RequestBody() { @Override public MediaType contentType() { return MEDIA_TYPE_MARKDOWN; } @Override public void writeTo(BufferedSink sink) throws IOException { sink.writeUtf8("Numbers\n"); sink.writeUtf8("-------\n"); for (int i = 2; i <= 997; i++) { sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i))); } } private String factor(int n) { for (int i = 2; i < n; i++) { int x = n / i; if (x * i == n) return factor(x) + " ¡Á " + i; } return Integer.toString(n); } }; Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(requestBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }

Post·½Ê½Ìá½»Îļþ
ÒÔÎļþ×÷ΪÇëÇóÌåÊÇÊ®·Ö¼òµ¥µÄ¡£

public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { File file = new File("README.md"); Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file)) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }

Post·½Ê½Ìá½»±íµ¥
ʹÓÃFormEncodingBuilderÀ´¹¹½¨ºÍHTML

private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { RequestBody formBody = new FormEncodingBuilder() .add("search", "Jurassic Park") .build(); Request request = new Request.Builder() .url("https://en.wikipedia.org/w/index.php") .post(formBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }

Post·½Ê½Ìá½»·Ö¿éÇëÇó
MultipartBuilder¿ÉÒÔ¹¹½¨¸´ÔÓµÄÇëÇóÌ壬ÓëHTMLÎļþÉÏ´«ÐÎʽ¼æÈÝ¡£¶à¿éÇëÇóÌåÖÐÿ¿éÇëÇó¶¼ÊÇÒ»¸öÇëÇóÌ壬¿ÉÒÔ¶¨Òå×Ô¼ºµÄÇëÇóÍ·¡£ÕâЩÇëÇóÍ·¿ÉÒÔÓÃÀ´ÃèÊöÕâ¿éÇëÇó£¬ÀýÈçËûµÄContent-Disposition¡£Èç¹ûContent-LengthºÍContent-Type¿ÉÓõϰ£¬ËûÃǻᱻ×Ô¶¯Ìí¼Óµ½ÇëÇóÍ·ÖС£

private static final String IMGUR_CLIENT_ID = "..."; private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image RequestBody requestBody = new MultipartBuilder() .type(MultipartBuilder.FORM) .addPart( Headers.of("Content-Disposition", "form-data; name=\"title\""), RequestBody.create(null, "Square Logo")) .addPart( Headers.of("Content-Disposition", "form-data; name=\"image\""), RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png"))) .build(); Request request = new Request.Builder() .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID) .url("https://api.imgur.com/3/image") .post(requestBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }

ʹÓÃGsonÀ´½âÎöJSONÏìÓ¦
GsonÊÇÒ»¸öÔÚJSONºÍJava¶ÔÏóÖ®¼äת»»·Ç³£·½±ãµÄapi¡£ÕâÀïÎÒÃÇÓÃGsonÀ´½âÎöGithub APIµÄJSONÏìÓ¦¡£
×¢Ò⣺ResponseBody.charStream()ʹÓÃÏìӦͷContent-TypeÖ¸¶¨µÄ×Ö·û¼¯À´½âÎöÏìÓ¦Ì塣ĬÈÏÊÇUTF-8¡£

private final OkHttpClient client = new OkHttpClient(); private final Gson gson = new Gson(); public void run() throws Exception { Request request = new Request.Builder() .url("https://api.github.com/gists/c2a7c39532239ff261be") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Gist gist = gson.fromJson(response.body().charStream(), Gist.class); for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) { System.out.println(entry.getKey()); System.out.println(entry.getValue().content); } } static class Gist { Map<String, GistFile> files; } static class GistFile { String content; }

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎ×ªÔØµÄ¸å¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬Çë×ªÔØÊ±Îñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Android½âÎöJsonËÙ¶È×î¿ìµÄ¿â:json

    Android½âÎöJsonËÙ¶È×î¿ìµÄ¿â:json

    2016-02-13 18:00

  • AndroidÖÐJSONÊý¾Ý¸ñʽ¶ÁÈ¡½âÎö´´½¨ÊÓÆµ½Ì³Ì

    AndroidÖÐJSONÊý¾Ý¸ñʽ¶ÁÈ¡½âÎö´´½¨ÊÓÆµ½Ì³Ì

    2016-02-05 19:00

  • HttpClient 4.3½Ì³Ì µÚÒ»Õ »ù±¾¸ÅÄî

    HttpClient 4.3½Ì³Ì µÚÒ»Õ »ù±¾¸ÅÄî

    2016-01-26 16:49

  • 7.2.2 Android JSONÊý¾Ý½âÎö

    7.2.2 Android JSONÊý¾Ý½âÎö

    2016-01-25 13:19

ÍøÓѵãÆÀ
_