forked from square/okhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dependency-free examples for the OkHttp front page.
I'm going to include code snippets from these.
- Loading branch information
1 parent
806caed
commit 954e8f5
Showing
5 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.squareup.okhttp.sample</groupId> | ||
<artifactId>sample-parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>guide</artifactId> | ||
<name>Sample: Guide</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup.okhttp</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
43 changes: 43 additions & 0 deletions
43
samples/guide/src/main/java/com/squareup/okhttp/guide/GetExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.squareup.okhttp.guide; | ||
|
||
import com.squareup.okhttp.OkHttpClient; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class GetExample { | ||
OkHttpClient client = new OkHttpClient(); | ||
|
||
void run() throws IOException { | ||
String result = get(new URL("https://raw.github.com/square/okhttp/master/README.md")); | ||
System.out.println(result); | ||
} | ||
|
||
String get(URL url) throws IOException { | ||
HttpURLConnection connection = client.open(url); | ||
InputStream in = null; | ||
try { | ||
// Read the response. | ||
in = connection.getInputStream(); | ||
byte[] response = readFully(in); | ||
return new String(response, "UTF-8"); | ||
} finally { | ||
if (in != null) in.close(); | ||
} | ||
} | ||
|
||
byte[] readFully(InputStream in) throws IOException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024]; | ||
for (int count; (count = in.read(buffer)) != -1; ) { | ||
out.write(buffer, 0, count); | ||
} | ||
return out.toByteArray(); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
new GetExample().run(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
samples/guide/src/main/java/com/squareup/okhttp/guide/PostExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.squareup.okhttp.guide; | ||
|
||
import com.squareup.okhttp.OkHttpClient; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class PostExample { | ||
OkHttpClient client = new OkHttpClient(); | ||
|
||
void run() throws IOException { | ||
byte[] body = bowlingJson("Jesse", "Jake").getBytes("UTF-8"); | ||
String result = post(new URL("http://www.roundsapp.com/post"), body); | ||
System.out.println(result); | ||
} | ||
|
||
String post(URL url, byte[] body) throws IOException { | ||
HttpURLConnection connection = client.open(url); | ||
OutputStream out = null; | ||
InputStream in = null; | ||
try { | ||
// Write the request. | ||
connection.setRequestMethod("POST"); | ||
out = connection.getOutputStream(); | ||
out.write(body); | ||
out.close(); | ||
|
||
// Read the response. | ||
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { | ||
throw new IOException("Unexpected HTTP response: " | ||
+ connection.getResponseCode() + " " + connection.getResponseMessage()); | ||
} | ||
in = connection.getInputStream(); | ||
return readFirstLine(in); | ||
} finally { | ||
// Clean up. | ||
if (out != null) out.close(); | ||
if (in != null) in.close(); | ||
} | ||
} | ||
|
||
String readFirstLine(InputStream in) throws IOException { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); | ||
return reader.readLine(); | ||
} | ||
|
||
String bowlingJson(String player1, String player2) { | ||
return "{'winCondition':'HIGH_SCORE'," | ||
+ "'name':'Bowling'," | ||
+ "'round':4," | ||
+ "'lastSaved':1367702411696," | ||
+ "'dateStarted':1367702378785," | ||
+ "'players':[" | ||
+ "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39}," | ||
+ "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}" | ||
+ "]}"; | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
new PostExample().run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters