As通过HttpOk链接后台

1.AS可以链接后台;至于后台是哪种语言写的 都是可以的
2.在链接httpok的时候。需要引用包;
 

implementation 'com.squareup.okhttp:okhttp:2.7.0'
implementation 'com.squareup.okio:okio:1.7.0'
implementation 'com.squareup.okhttp3:okhttp:3.2.0'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
3.
OkHttpClient client = new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build();
String yhm="lyc";
String password="666666";
Request request = new Request.Builder().url("http://192.168.25.163:8011/Handler/AndroidStudio.ashx?action=login&yhm="+yhm+"&password="+password)
        .get().build();

Call call = client.newCall(request);
try {
    Response response = call.execute();
    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
    System.out.println(response.body().string());
} catch (IOException e) {
    e.printStackTrace();
}

4.可能会报错:

saveimg.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //图片路径
        Toast.makeText(MainActivity.this, path, Toast.LENGTH_SHORT).show();

        OkHttpClient client = new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build();
        String yhm="lyc";
        String password="666666";
        Request request = new Request.Builder().url("http://192.168.25.163:8011/Handler/AndroidStudio.ashx?action=login&yhm="+yhm+"&password="+password)
                .get().build();

        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
});

中 ,加上:
if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }

4.添加网络权限:
 


注意与application平级

你可能感兴趣的:(AS)