Search in sources :

Example 46 with Request

use of com.squareup.okhttp.Request in project remusic by aa112901.

the class HttpUtil method postNetease.

public static void postNetease(Context context, String j) {
    try {
        String action = "https://music.163.com/weapi/login/";
        RequestBody formBody = new FormEncodingBuilder().add("params", "9NdyZTlp0Q/f1E1ora4tGM0uLYXqh7MD0mk7632ilWQvRDPZ02UkHrGFUccwW4HZYpacpPnmE+oMr/HI/vhuQvg8zYKgDP6NOaXG8nKDJpQTfOAiXT5KDrJOvb7ejSj/").add("encSeckey", "ae878167c394a959699c025a5c36043d0ae043c42d7f55fe4d1191c8ac9f3abe285b78c4a25ed6d9394a0ba0cb83a9a62de697199bd337f1de183bb07d6764a051495ea873ad615bb0a7e69f44d9168fc78ed1d61feb142ad06679dce58257ee9005756a18032ff499a4e24f7658bb59de2219f21f568301d43dba500e0c2d3b").build();
        String json = "{\"params\": \"9NdyZTlp0Q/f1E1ora4tGM0uLYXqh7MD0mk7632ilWQvRDPZ02UkHrGFUccwW4HZYpacpPnmE+oMr/HI/vhuQvg8zYKgDP6NOaXG8nKDJpQTfOAiXT5KDrJOvb7ejSj/\",  " + "\"encSecKey\": \"ae878167c394a959699c025a5c36043d0ae043c42d7f55fe4d1191c8ac9f3abe285b78c4a25ed6d9394a0ba0cb83a9a62de697199bd337f1de183bb07d6764a051495ea873ad615bb0a7e69f44d9168fc78ed1d61feb142ad06679dce58257ee9005756a18032ff499a4e24f7658bb59de2219f21f568301d43dba500e0c2d3b\"}";
        RequestBody requestBody = RequestBody.create(MediaType.parse("JSON"), json);
        Log.e("post", "p");
        Request request = new Request.Builder().url(action).header("Content-Type", "application/x-www-form-urlencoded").header("Host", "music.163.com").header("Cookie", "appver=1.5.0.75771").header("Referer", "http://music.163.com/").header("Connection", "keep-alive").header("Accept-Encoding", "gzip,deflate").header("Accept", "*/*").header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36").post(requestBody).build();
        mOkHttpClient.setCookieHandler(new CookieManager(new PersistentCookieStore(context.getApplicationContext()), CookiePolicy.ACCEPT_ALL));
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.e("respose", response.body().string());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) CookieManager(java.net.CookieManager) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestBody(com.squareup.okhttp.RequestBody)

Example 47 with Request

use of com.squareup.okhttp.Request in project remusic by aa112901.

the class HttpUtil method getOut.

public static void getOut(final String url) {
    try {
        mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
        mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
        Request request = new Request.Builder().url(url).build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + "gedangein" + ".json");
            byte[] c = new byte[1024];
            while (response.body().source().read(c) != -1) {
                fo.write(c);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.squareup.okhttp.Response) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) FileOutputStream(java.io.FileOutputStream) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 48 with Request

use of com.squareup.okhttp.Request in project remusic by aa112901.

the class HttpUtil method getBitmapStream.

public static Bitmap getBitmapStream(Context context, String url, boolean forceCache) {
    try {
        File sdcache = context.getExternalCacheDir();
        //File cacheFile = new File(context.getCacheDir(), "[缓存目录]");
        //30Mb
        Cache cache = new Cache(sdcache.getAbsoluteFile(), 1024 * 1024 * 30);
        mOkHttpClient.setCache(cache);
        mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
        mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
        Request.Builder builder = new Request.Builder().url(url);
        if (forceCache) {
            builder.cacheControl(CacheControl.FORCE_CACHE);
        }
        Request request = builder.build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            return _decodeBitmapFromStream(response.body().byteStream(), 160, 160);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cache(com.squareup.okhttp.Cache) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache)

Example 49 with Request

use of com.squareup.okhttp.Request in project remusic by aa112901.

the class HttpUtil method getResposeJsonObject.

public static JsonObject getResposeJsonObject(String action1, Context context, boolean forceCache) {
    try {
        Log.e("action-cache", action1);
        File sdcache = context.getCacheDir();
        //File cacheFile = new File(context.getCacheDir(), "[缓存目录]");
        //30Mb
        Cache cache = new Cache(sdcache.getAbsoluteFile(), 1024 * 1024 * 30);
        mOkHttpClient.setCache(cache);
        mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
        mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
        Request.Builder builder = new Request.Builder().url(action1);
        if (forceCache) {
            builder.cacheControl(CacheControl.FORCE_CACHE);
        }
        Request request = builder.build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String c = response.body().string();
            Log.e("cache", c);
            JsonParser parser = new JsonParser();
            JsonElement el = parser.parse(c);
            return el.getAsJsonObject();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) JsonElement(com.google.gson.JsonElement) Request(com.squareup.okhttp.Request) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cache(com.squareup.okhttp.Cache) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) JsonParser(com.google.gson.JsonParser)

Example 50 with Request

use of com.squareup.okhttp.Request in project Android-Universal-Image-Loader by nostra13.

the class OkHttpImageDownloader method getStreamFromNetwork.

@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
    Request request = new Request.Builder().url(imageUri).build();
    ResponseBody responseBody = client.newCall(request).execute().body();
    InputStream inputStream = responseBody.byteStream();
    int contentLength = (int) responseBody.contentLength();
    return new ContentLengthInputStream(inputStream, contentLength);
}
Also used : ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) InputStream(java.io.InputStream) Request(com.squareup.okhttp.Request) ResponseBody(com.squareup.okhttp.ResponseBody)

Aggregations

Request (com.squareup.okhttp.Request)73 Response (com.squareup.okhttp.Response)47 IOException (java.io.IOException)41 OkHttpClient (com.squareup.okhttp.OkHttpClient)22 RequestBody (com.squareup.okhttp.RequestBody)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)11 File (java.io.File)10 Callback (com.squareup.okhttp.Callback)9 InputStream (java.io.InputStream)6 Gson (com.google.gson.Gson)4 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 MediaType (com.squareup.okhttp.MediaType)4 ResponseBody (com.squareup.okhttp.ResponseBody)4 FileOutputStream (java.io.FileOutputStream)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 Uri (android.net.Uri)3 Cache (com.squareup.okhttp.Cache)3 Call (com.squareup.okhttp.Call)3