网友通过本文主要向大家介绍了android http通信,android中的http通信,android studio http,android http请求,android http等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android之利用HTTP网络通信实现与PHP的交互(三),android网络通信
Android与PHP的交互是通过Http网络编程来实现的,利用php访问数据库,并且操作数据库中的数据,利用php作为接口,使Android连接数据库。
一般情况下,我们使用Json格式进行传输,利用php将数据封装为Json的形式,然后再Android上面在对Json数据进行解析。将解析好的数据显示在手机上。
为了跟前两篇的进行对接,我这篇博文就不给大家展示Json数据的解析了,等到下篇的时候我给大家讲一下Json数据的解析,以及怎么样将数据部署在布局文件中,我这次只是将获取到的Json数据显示在TextView中。为了方便大家观察,我这里设置了两个TextView进行对比,一个部署上数据,另一个是本地的数据。并且,通过跳转的方式实现。
效果图如下:
A:
B:
C:
操作步骤:
首先打开软件,然后点击提交跳转到另一个界面中进行显示数据。显示的直接是Json数据,并没有对其进行解析。
Java代码:
MainActivity:
1 package com.example.testregister; 2 3 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.EditText; 11 12 public class MainActivity extends Activity{ 13 14 private Button btn; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 btn = (Button) findViewById(R.id.btn_tijao); 22 23 btn.setOnClickListener(new OnClickListener() { 24 25 @Override 26 public void onClick(View v) { 27 28 Intent intent = new Intent(getApplicationContext(),ShowTest.class); 29 startActivity(intent); 30 } 31 }); 32 } 33 34 35 36 }
布局文件activity_main.xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/LinearLayout1" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context=".MainActivity" > 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="@string/hello_world" /> 13 14 <Button 15 android:id="@+id/btn_tijao" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:text="提交" /> 19 20 </LinearLayout>
第二个界面ShowTest:
1 package com.example.testregister; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.TextView; 6 7 import com.example.interfaceHttp.HttpGetListener; 8 import com.example.service.HttpGetData; 9 10 public class ShowTest extends Activity implements HttpGetListener{ 11 12 private TextView textv1; 13 private TextView textv2; 14 15 //我的本地服务器的接口,如果在你自己的服务器上需要更改相应的url 16 private String url ="http://10.17.64.85:8080/testregister/JSONPars.php"; 17 18 private HttpGetData mhttpgetdata; 19 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activitytwo); 26 mhttpgetdata = (HttpGetData) new HttpGetData(url,this).execute(); 27 textv1 = (TextView) findViewById(R.id.tv1); 28 textv2 = (TextView) findViewById(R.id.tv2); 29 30 31 } 32 33 34 @Override 35 public void GetDataUrl(String data) { 36 // TODO Auto-generated method stub 37 System.out.println(data); 38 textv1.setText(data); 39 40 } 41 42 }
第二个布局 activitytwo.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tv1" 9 android:layout_width<