先在Manifest里 设置网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
添加依赖:
compile 'com.loopj.android:android-async-http:1.4.9'
创建一个类 HttpGet.java :
package com.example.myapplication20;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import java.net.URL;
/**
* Created by lenovo on 2018/4/10.
*/
public class HttpGet {
private static final String URL = "http://103.244.59.105:8014/paopaoserver/";//网络地址
private static AsyncHttpClient client = new AsyncHttpClient(); //创建对象
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(qwe(url), params, responseHandler);
}
private static String qwe(String re) {
return URL + re; //返回值
}
}
在Maintivity.java里设置监听 :
package com.example.myapplication20;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RequestParams requestParams=new RequestParams();
requestParams.put("params","{\"page\":1,\"page_count\":10}");
HttpGet.get("articles",requestParams, new TextHttpResponseHandler() { //new TextHttpResponseHandler() 自动重写两个方法
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(MainActivity.this,"访问失败",Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(MainActivity.this,responseString,Toast.LENGTH_SHORT).show();//提示这个方法,表示创建成功,否则失败
}
});
}
});
}
}
创建一个类HttpPost:
package com.example.myapplication20;
import android.content.Context;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
import java.net.URL;
/**
* Created by lenovo on 2018/4/10.
*/
public class HttpPost {
private static final String URL = "http://103.244.59.105:8014/paopaoserver/";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void post( String url, RequestParams params, AsyncHttpResponseHandler responseHandle) {
client.post( qwe(url), params, responseHandle);
}
private static String qwe(String re) {
return URL + re;
}
}
Maintivity.java中:
package com.example.myapplication20;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RequestParams requestParams=new RequestParams();
requestParams.put("params","{\"page\":1,\"page_count\":10}");
HttpPost.post("articles",requestParams, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(MainActivity.this,"访问失败",Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(MainActivity.this,responseString,Toast.LENGTH_SHORT).show();
}
});
}
});
}
}