Android中使用网络和服务器端程序进行数据交换
2016-06-30 23:19
696 查看
通过Get和Post方式从服务器读取数据和发送数据
XML文件:
MainActivity
GetPostUtil
XML文件:
<?xml version="1.0" encoding="utf-8"?> <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="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="top.helloworldes.getpost.MainActivity"> <Button android:id="@+id/get" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get" /> <Button android:id="@+id/post" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="post" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/username" android:layout_gravity="center_horizontal" android:text="admin" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/password" android:layout_gravity="center_horizontal" android:text="admin" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scrollView" android:layout_gravity="center_horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="HelloWood" android:id="@+id/textView" /> </ScrollView> </LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity { Button get, post; TextView show; String response; EditText name,pswd; String username,password,params; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x123) { show.setText(response); } if (msg.what == 0x124) { System.out.println(params); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); get = (Button) findViewById(R.id.get); post = (Button) findViewById(R.id.post); show = (TextView) findViewById(R.id.textView); name = (EditText) findViewById(R.id.username); pswd = (EditText) findViewById(R.id.password); get.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread() { @Override public void run() { response = GetPostUtil.sendGet("http://www.helloworlds.top/Test/index.jsp", null); handler.sendEmptyMessage(0x123); } }.start(); } }); post.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handler.sendEmptyMessage(0x124); postMsg(); } }); } public void postMsg() { new Thread() { @Override public void run() { username = name.getText().toString(); password = pswd.getText().toString(); params = "username=" + username + "&" + "password=" + password; response = GetPostUtil.sendPost("http://www.helloworlds.top/Test/login.jsp",params); handler.sendEmptyMessage(0x123); } }.start(); } }
GetPostUtil
public class GetPostUtil { public static String sendGet(String url, String params) { String result = ""; BufferedReader in = null; String urlName = url + "?" + params; try { URL realUrl = new URL(urlName); URLConnection conn = realUrl.openConnection(); conn.connect(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += "\n" + line; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } public static String sendPost(String url, String params) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(params); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += "\n" + line; Log.i("GetPost", line); } } catch (MalformedURLException e) { Log.e("GetPost:", "Post请求异常"); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } }
相关文章推荐
- BP神经网络学习过程
- 网络编程小结
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- HTTP Session原理
- 自己动手写HTTP框架:ThreadPool+Runnable+handler篇
- http://www.tuicool.com/articles/B3qeUrB
- 波尔远程控制软件与网络人远程控制软件大比拼
- 波尔远程控制软件与网络人远程控制软件大比拼
- Qt---基于TCP聊天室
- 劫持网络--运营商投放广告
- ubuntu开机无网络-解决方法记录
- HTTP和HTTPS详解
- 编写C语言版本的卷积神经网络CNN之四:CNN的学习及测试结果的比较
- 伪造IP包,禁止TCP连接
- HTTP Live Streaming直播(iOS直播)技术分析与实现
- 网络流
- labview之tcp通信
- iOS判断当前是否有网络
- 安卓学习之-ConnectivityManager(判断网络连接)
- 使用httpClient请求的网络数据