OKHTTP


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();
  }
}


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();
  }
}

猜你喜欢

转载自zheyiw.iteye.com/blog/2245846