0

I face a problem to get Json data into my application. On get action. I have solve the problem for POST. We have a ssl certificate but seems for get and post the way is little different. I can't figure out. So if people can give me an help here. I will appreciate.

package com.example.administrator.superclass.Utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Looper;
import android.util.Log;

import com.example.administrator.superclass.MyApp;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Okhttp_Util {
    public static String Ok_Get(Context context,String url, HashMap<String, String> hashMap) {
        String data = "";
//        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .connectTimeout(10, TimeUnit.SECONDS)
//                .readTimeout(10,TimeUnit.SECONDS)
//                .build();
        OkHttpClient okHttpClient= MyApp.handleSSLHandshake();
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : hashMap.keySet()) {
            builder.add(key, hashMap.get(key));
        }
        builder.build();
        RequestBody requestBody = builder.build();
        Request request = new Request.Builder().url(url).build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            data = response.body().string();
        } catch (IOException e) {
            if(e instanceof SocketTimeoutException){//判断超时异常
                Looper.prepare();
                if (ProgressDialog_Util.isshow){
                    ProgressDialog_Util.Dialog_dismiss();
                }
                Toast_util.Toast_show(context,"连接超时!请更换网络后重试!");
                Log.e("geterr1: ",e.toString() );
                Looper.loop();
            }
            if(e instanceof ConnectException){//判断连接异常,我这里是报Failed to connect to 10.7.5.144
                Looper.prepare();
                if (ProgressDialog_Util.isshow){
                    ProgressDialog_Util.Dialog_dismiss();
                }
                Toast_util.Toast_show(context,e.toString());
                Log.e( "geterr2: ",e.toString() );
                Looper.loop();
            }
        }
        return data;
    }

    public static String Ok_Post(Context context,String url, HashMap<String, String> hashMap) {
        String data = "";
//        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .connectTimeout(10, TimeUnit.SECONDS)
//                .readTimeout(10,TimeUnit.SECONDS)
//                .build();
        OkHttpClient okHttpClient= MyApp.handleSSLHandshake();
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : hashMap.keySet()) {
            builder.add(key, hashMap.get(key));
        }
        builder.build();
        RequestBody requestBody = builder.build();
        Request request = new Request.Builder().post(requestBody).url(url).build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            data = response.body().string();
        } catch (IOException e) {
            if(e instanceof SocketTimeoutException){//判断超时异常
                Looper.prepare();
                if (ProgressDialog_Util.isshow){
                    ProgressDialog_Util.Dialog_dismiss();
                }
                Toast_util.Toast_show(context,"连接超时!");
                Log.e("posterr1: ",e.toString() );
                Looper.loop();
            }
            if(e instanceof ConnectException){//判断连接异常,我这里是报Failed to connect to 10.7.5.144
                Looper.prepare();
                if (ProgressDialog_Util.isshow){
                    ProgressDialog_Util.Dialog_dismiss();
                }
                Toast_util.Toast_show(context,e.toString());
                Log.e( "posterr2: ",e.toString() );
                Looper.loop();
            }
        }
        return data;
    }
}

From the compilation i get an error :

enter image description here

1 Answers1

0

The error indicates that you are calling your get request without passing a HashMap. My suggestion would be to either pass an empty HashMap if you have some URLs that require a body. Else remove it entirely:

public static String Ok_Get(Context context,String url) {
    String data = "";
    OkHttpClient okHttpClient= MyApp.handleSSLHandshake();
    FormBody.Builder builder = new FormBody.Builder();
    builder.build();
    Request request = new Request.Builder().url(url).build();

If you still need it, when you call it make sure you include the HashMap:

Ok_Get(context, "your/url/string/here", Collections.<String, String>emptyMap())
Scott Evans
  • 374
  • 1
  • 10
  • Dear Scott Evans, Thanks for your help, I've done as per you said from your first suggestion. But get this error when i compile ... uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details. We not so specialist in Android studio . So maybe here we have something to learn about. Thanks for your help Appreciated – peuportier aurelien Nov 28 '19 at 06:59
  • @peuportieraurelien for that issue please see the following thread: https://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning – Scott Evans Nov 28 '19 at 07:24