项目需要,服务器端接口已经写好,现在需要在手机app端进行对该接口的访问,并得到接口返回的数据,搞了一周,终于搞定了,写篇博客,归纳总结一下,同大家互相交流学习一下,现将代码贴出,完整代码,并附有详细注释。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<TextView
android:id="@+id/mytex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取数据"
/>
</LinearLayout>
上面是xml布局代码,下面进入正菜,也是最为关键的部分,
public class MainActivity extends Activity implements OnClickListener {
HttpURLConnection con = null;
InputStream iss = null;
String resultDat = "";
String resultData = "";
TextView mytex = null;
Button mybut = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mybut = (Button)findViewById(R.id.mybut);
this.mytex = (TextView)findViewById(R.id.mytex);
this.mybut.setOnClickListener(this);
}
//get_data函数完成了对数据库的访问,并得到服务器接口返回的数据
private String get_data(String httpUrl){
try{
//通过URL类指定一个要连接的地址(或者ip)
URL url = new URL(httpUrl);
//利用URL类的openConnection()方法取得一个
//URLConnection类的对象,而HttpURLConnection
//类是URLConnection类的子类
con = (HttpURLConnection)url.openConnection();
iss = con.getInputStream();//此时才是真正意思上的与服务器建立了连接
InputStreamReader reader = new InputStreamReader(iss);
BufferedReader buf = new BufferedReader(reader);
String result = "";
while((result = buf.readLine())!=null){
resultDat += result+"\n";
}
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(iss != null){
try {
iss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null){
con.disconnect();
}
}
return resultDat;
}
//httptest类完成子线程的建立
class httptest implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
String url = "http://122.13.138.146/interface_aplist.php";
resultData = get_data(url);
}
}
//get_data函数完成了对数据库的访问,并得到服务器接口返回的数据
private String get_data(String httpUrl){
try{
//通过URL类指定一个要连接的地址(或者ip)
URL url = new URL(httpUrl);
//利用URL类的openConnection()方法取得一个
//URLConnection类的对象,而HttpURLConnection
//类是URLConnection类的子类
con = (HttpURLConnection)url.openConnection();
iss = con.getInputStream();//此时才是真正意思上的与服务器建立了连接
InputStreamReader reader = new InputStreamReader(iss);
BufferedReader buf = new BufferedReader(reader);
String result = "";
while((result = buf.readLine())!=null){
resultDat += result+"\n";
}
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(iss != null){
try {
iss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null){
con.disconnect();
}
}
return resultDat;
}
@Override
public void run() {
// TODO Auto-generated method stub
String url = "http:// + ip + /文件名称";
resultData = get_data(url);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread http = new Thread(new httptest());
http.start();
try {
http.join();
if(resultData != null){
mytex.setText(resultData);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上完成了代码最为核心的部分,利用子线程的方式对服务器接口进行了访问。但是需要注意一点的是:别忘了在AndroidManifest.xml中进行权限声明
<uses-permission android:name="android.permission.INTERNET"/>
全部的代码已经贴出,这样便会得到接口返回的数据,数据保存在resultData中,当然单单得到数据工作还没有完全结束,还要对接受到的数据进行解析,但是,与服务器的通信我们已经完成。