Android Studio 中引入org.apache.commons.*包具体做法

文章目录

Android Studio 中引入org.apache.commons.*包具体做法

1.可以直接在build.gradle(:app)中加入下面语句:

implementation 'org.apache.commons:commons-lang3:3.7'

如下图第4行代码,之后重新 sycn
Android Studio 中引入org.apache.commons.*包具体做法

2. 手动下载对应链接库jar,然后添加为项目的链接库

apache.commons 官网 下载

如下图:
Android Studio 中引入org.apache.commons.*包具体做法
找到你要的包下载下来,放到你的项目libs目录下
Android Studio 中引入org.apache.commons.*包具体做法
选中你要添加的包,右键 Add As Library ,然后重新 gradle sycn , 即可完成需要的链接jar的加入。

Android 网络编程时使用HttpClient :

1.import org.apache.http 找不到 (已解决)

参考博客

useLibrary 'org.apache.http.legacy'

具体见下图:文件build.gradle(:app)
Android Studio 中引入org.apache.commons.*包具体做法

2. 使用get、post方法访问URL地址(示例代码)

代码如下:

public class MainActivity extends AppCompatActivity {

    private static final String TAG="HttpClient";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                useHttpClientGet("http://www.baidu.com");
//            }
//        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                useHttpClientPost("http://120.95.135.44:8980/web/a/login?__login=true&__ajax=json&username=system&password=235880C505ACCDA5C581A4F4CDB81DA0");
            }
        }).start();
    }

    private HttpClient createHttpClient(){
        HttpParams mDefaultHttpParams = new BasicHttpParams();
        //设置连接超时
        HttpConnectionParams.setConnectionTimeout(mDefaultHttpParams,15000);
        //设置请求超时
        HttpConnectionParams.setSoTimeout(mDefaultHttpParams,15000);
        HttpConnectionParams.setTcpNoDelay(mDefaultHttpParams,true);
        HttpProtocolParams.setContentCharset(mDefaultHttpParams, HTTP.UTF_8);
        //持续握手
        HttpProtocolParams.setUseExpectContinue(mDefaultHttpParams,true);
        HttpClient mHttpClient = new DefaultHttpClient(mDefaultHttpParams);
        return mHttpClient;
    }

    private void useHttpClientGet(String url){
        HttpGet mHttpGet = new HttpGet(url);
        mHttpGet.addHeader("Connection","Keep-Alive");

        try {
            HttpClient mHttpClient=createHttpClient();
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            int code = mHttpResponse.getStatusLine().getStatusCode();
            if(null != mHttpEntity){
                InputStream mInputStream = mHttpEntity.getContent();
                String repose=converStreamToString(mInputStream);
                Log.d(TAG,"请求状态码:"+code+"\n请求结果:\n"+repose);
                mInputStream.close();;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String converStreamToString(InputStream is) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuffer sb = new StringBuffer();
        String line=null;
        while((line = reader.readLine()) != null){
            sb.append(line+"\n");
        }
        String repose = sb.toString();
        return repose;
    }

    private void useHttpClientPost(String url){
        HttpPost mHttpPost=new HttpPost(url);
        mHttpPost.addHeader("Connection","Keep-Alive");

        try {
            HttpClient mHttpClient=createHttpClient();
            List<NameValuePair> postParams = new ArrayList<>();
            //要传递的参数
            postParams.add(new BasicNameValuePair("ip","59.108.54.37"));
            mHttpPost.setEntity(new UrlEncodedFormEntity(postParams));
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            int code = mHttpResponse.getStatusLine().getStatusCode();
            if ( null != mHttpEntity){
                InputStream mInputStream=mHttpEntity.getContent();
                String respose = converStreamToString(mInputStream);
                Log.d(TAG, "请求状态码:" + code + "\n请求结果:\n"+respose);
                mInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
上一篇:上传文件中 MultipartFile 转为 File


下一篇:文件上传