Search in sources :

Example 46 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method dianzan.

// 点赞
public static String dianzan(int score) {
    FormBody.Builder buider = new FormBody.Builder();
    buider.add("score", score + "");
    FormBody formBody = buider.build();
    String url = "http://dianzan.myqcloud.com" + "/rank.php";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return new JSONObject(response.body().string()).toString(4);
        } else {
            return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        return "error:" + e.getMessage();
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request)

Example 47 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method getBookInfo.

public static String getBookInfo(String bookid) {
    // &pageSize=20&keyword
    FormBody.Builder buider = new FormBody.Builder();
    buider.add("bid", bookid);
    // 
    FormBody formBody = APIheper.getFormBuider(buider).build();
    // String url=host + "/api/recom/index";
    String url = host + "/api/book/cartoon-info";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return new JSONObject(response.body().string()).toString();
        } else {
            return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        return "error:" + e.getMessage();
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request)

Example 48 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method getCategory.

public static String getCategory() {
    // &pageSize=20&keyword
    FormBody.Builder buider = new FormBody.Builder();
    // 
    FormBody formBody = APIheper.getFormBuider(buider).build();
    String url = host + "/api/book/category";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return new JSONObject(response.body().string()).toString(4);
        } else {
            return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        return "error:" + e.getMessage();
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request)

Example 49 with FormBody

use of okhttp3.FormBody in project My-MVP by REBOOTERS.

the class Okhttp3DemoActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    handler = new MyHandler();
    setContentView(R.layout.activity_okhttp_three_demo);
    tv = (TextView) findViewById(R.id.editText);
    loading = (ProgressBar) findViewById(R.id.loading);
    imageView = (ImageView) findViewById(R.id.image);
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancle", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            progressDialog.dismiss();
            downloadCall.cancel();
        }
    });
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setTitle("下载文件");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    findViewById(R.id.get).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            tv.setText("");
            loading.setVisibility(View.VISIBLE);
            client = new OkHttpClient();
            Request.Builder builder = new Request.Builder().url(BASE_URL).method("GET", null);
            request = builder.build();
            Call mCall = client.newCall(request);
            mCall.enqueue(new MyCallback());
        }
    });
    findViewById(R.id.post).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            tv.setText("");
            loading.setVisibility(View.VISIBLE);
            client = new OkHttpClient();
            RequestBody formBody = new FormBody.Builder().build();
            request = new Request.Builder().url(BASE_URL).post(formBody).build();
            Call mCall = client.newCall(request);
            mCall.enqueue(new MyCallback());
        }
    });
    findViewById(R.id.post1).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            tv.setText("");
            loading.setVisibility(View.VISIBLE);
            client = new OkHttpClient();
            RequestBody formBody = new FormBody.Builder().add("login_name", "15101180298").add("image_verifty_code", "1236").add("password", "123456").build();
            request = new Request.Builder().url("http://cybershop4-dev-restapi.dev.co-mall/api/session").addHeader("channel", "2").addHeader("os", "android").addHeader("unique", "11112").post(formBody).build();
            Call mCall = client.newCall(request);
            mCall.enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e(TAG, "onFailure: e=" + e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.e(TAG, "onResponse: response=" + response);
                    if (response.code() == 201) {
                        // Log.e(TAG, "onResponse: body==" + response.body().string());
                        String body = response.body().string();
                        Log.e(TAG, "onResponse: body=" + body);
                        Message msg = new Message();
                        msg.what = 200;
                        msg.obj = body;
                        handler.sendMessage(msg);
                    }
                }
            });
        }
    });
    /**
     * http://cybershop4-dev-restapi.dev.co-mall/api/members/members_created_by_mobile
     * ?phone_number=15101180298&password=123456&sms_verify_code=1234&captcha=1234
     */
    findViewById(R.id.post2).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            client = new OkHttpClient();
            FormBody mFormBody = new FormBody.Builder().add("phone_number", "15101180298").add("password", "123456").add("sms_verify_code", "1234").add("captcha", "1234").build();
            request = new Request.Builder().url("http://cybershop4-dev-restapi.dev.co-mall/api/members/members_created_by_mobile").post(mFormBody).build();
            Call mCall = client.newCall(request);
            mCall.enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e(TAG, "onFailure: e=" + e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.e(TAG, "onResponse: response=" + response);
                }
            });
        }
    });
    findViewById(R.id.post3).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            client = new OkHttpClient();
            FormBody mFormBody = new FormBody.Builder().add("quantity", "1").build();
            request = new Request.Builder().url("http://cybershop4-dev-restapi.dev.co-mall/api/carts/mine/baskets/items?goods_id=30055").addHeader("channel", "2").addHeader("os", "android").addHeader("unique", "11112").addHeader("userSession", "72D212F46592CC833B55A94D6A683E5D").post(mFormBody).build();
            Call mCall = client.newCall(request);
            mCall.enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e(TAG, "onFailure: e=" + e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.e(TAG, "onResponse: response=" + response);
                }
            });
        }
    });
    findViewById(R.id.downloadFile).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            progressDialog.show();
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                client = new OkHttpClient();
                request = new Request.Builder().url(DOWNLOAD_URL).build();
                downloadCall = client.newCall(request);
                downloadCall.enqueue(new DownloadCallback());
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Message(android.os.Message) DialogInterface(android.content.DialogInterface) Request(okhttp3.Request) FormBody(okhttp3.FormBody) IOException(java.io.IOException) ProgressDialog(android.app.ProgressDialog) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Response(okhttp3.Response) Callback(okhttp3.Callback) RequestBody(okhttp3.RequestBody)

Example 50 with FormBody

use of okhttp3.FormBody in project Pokemap by omkarmoghe.

the class MainActivity method getToken.

/**
 * This definitely needs to be cleaned up a little but yeah successfully intercepts the OAuth requests and gets the token.
 * @param username
 * @param password
 * @throws IOException
 */
private void getToken(final String username, String password) throws IOException {
    // Maximum password length is 15 (sign in page enforces this limit, API does not)
    final String trimmedPassword = password.length() > 15 ? password.substring(0, 15) : password;
    final OkHttpClient client = new OkHttpClient.Builder().hostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    }).cookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()))).followRedirects(false).followSslRedirects(false).build();
    Request initialRequest = new Request.Builder().addHeader("User-Agent", "Niantic App").url(LOGIN_URL).build();
    client.newCall(initialRequest).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            Log.e(TAG, "fuck :(", e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String body = response.body().string();
            try {
                JSONObject data = new JSONObject(body);
                Log.d(TAG, data.toString());
                RequestBody formBody = new FormBody.Builder().add("lt", data.getString("lt")).add("execution", data.getString("execution")).add("_eventId", "submit").add("username", username).add("password", trimmedPassword).build();
                Request interceptRedirect = new Request.Builder().addHeader("User-Agent", "Niantic App").url(LOGIN_URL).post(formBody).build();
                client.newCall(interceptRedirect).enqueue(new Callback() {

                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "fuck :(", e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        // should be a 302 (redirect)
                        Log.d(TAG, String.valueOf(response.code()));
                        // should contain a "Location" header
                        Log.d(TAG, response.headers().toString());
                        if (response.code() != 302 || response.header("Location") == null) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(getBaseContext(), getString(R.string.toast_credentials), Toast.LENGTH_SHORT).show();
                                }
                            });
                            return;
                        }
                        String ticket = response.header("Location").split("ticket=")[1];
                        RequestBody loginForm = new FormBody.Builder().add("client_id", CLIENT_ID).add("redirect_uri", REDIRECT_URI).add("client_secret", PTC_CLIENT_SECRET).add("grant_type", "refresh_token").add("code", ticket).build();
                        Request loginRequest = new Request.Builder().addHeader("User-Agent", "Niantic App").url(LOGIN_OAUTH).post(loginForm).build();
                        client.newCall(loginRequest).enqueue(new Callback() {

                            @Override
                            public void onFailure(Call call, IOException e) {
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String rawToken = response.body().string();
                                String cleanToken = rawToken.replaceAll("&expires.*", "").replaceAll(".*access_token=", "");
                                // success!
                                Log.d(TAG, cleanToken);
                                token = cleanToken;
                            }
                        });
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PersistentCookieJar(com.franmontiel.persistentcookiejar.PersistentCookieJar) SetCookieCache(com.franmontiel.persistentcookiejar.cache.SetCookieCache) SSLSession(javax.net.ssl.SSLSession) Request(okhttp3.Request) FormBody(okhttp3.FormBody) JSONException(org.json.JSONException) ByteString(com.google.protobuf.ByteString) SharedPrefsCookiePersistor(com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor) IOException(java.io.IOException) HostnameVerifier(javax.net.ssl.HostnameVerifier) Response(okhttp3.Response) Callback(okhttp3.Callback) JSONObject(org.json.JSONObject) RequestBody(okhttp3.RequestBody)

Aggregations

Request (okhttp3.Request)61 Response (okhttp3.Response)58 FormBody (okhttp3.FormBody)54 RequestBody (okhttp3.RequestBody)43 IOException (java.io.IOException)40 Call (okhttp3.Call)33 Callback (okhttp3.Callback)29 JSONObject (org.json.JSONObject)20 Map (java.util.Map)16 HttpUrl (okhttp3.HttpUrl)10 OkHttpClient (okhttp3.OkHttpClient)10 HashMap (java.util.HashMap)9 MultipartBody (okhttp3.MultipartBody)9 ArrayList (java.util.ArrayList)6 MediaType (okhttp3.MediaType)6 TypeToken (com.google.gson.reflect.TypeToken)4 File (java.io.File)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 List (java.util.List)2 ProgressRequestBody (me.ccrama.redditslide.util.ProgressRequestBody)2