OKHTTP是安全的吗?

时间:2016-01-21 12:49:25

标签: android https okhttp

我正在构建一个需要将用户登录到我们服务器的Android应用程序,并且连接需要是安全的(HTTPs)。我的问题是,我应该为此目的使用OKHTTP库吗?

我使用该库来记录用户,如下所示:

public class MainActivity extends AppCompatActivity {
public static final MediaType JSON
        = MediaType.parse("application/json; charset=utf-8");
private static final String TAG = MainActivity.class.getSimpleName();
private JSONObject responseJson;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    final JSONObject myJson = new JSONObject();
    try {
        myJson.put("udid","c376e418-da42-39fb-0000-d821f1fd2804");
        myJson.put("email","email
        myJson.put("password","password");
    } catch (JSONException e) {
        e.printStackTrace();
    }

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    //Your code goes here
                   String response =  post("https://ADDRESS/v1/auth", myJson.toString());
                    responseJson = new JSONObject(response);
                    String message = responseJson.getString("message");
                    String token = responseJson.getString("token");
                    Log.d(TAG,"Response message: " + message);
                    Log.d(TAG,"Response token: " + token);
                    Log.d("MainActivity",response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

}

String post(String url, String json) throws IOException {
    OkHttpClient client = new OkHttpClient();
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}
}

1 个答案:

答案 0 :(得分:2)

  

我应该为此目的使用OKHTTP库吗?

OkHttp支持https网址,大多数支持Android的HTTP客户端库也是如此。