httpurlconnection_get_post
来源:互联网 发布:指针可以当数组用吗 编辑:程序博客网 时间:2024/05/17 06:22
//utils帮助类
package com.example.httpurlconnection_get_post;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
public class httpURlUlist {
public static String getUlits(String name,String password){
String path="http://172.19.108.2:8080/fuwuqi/Fuwuqi?name="+name+"&preass="+password;
try {
URL url = new URL(path);
try {
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
//设置读取服务器时间
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int responseCode = connection.getResponseCode();
System.out.println(connection.getResponseCode());
if (responseCode==200) {
System.out.println(connection.getResponseCode());
InputStream inputStream = connection.getInputStream();
String str="";
BufferedReader bf=new BufferedReader(new InputStreamReader(inputStream));
str=bf.readLine();
//
return str;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String postUlits(String name,String password) throws IOException{
String path="http://172.19.108.2:8080/fuwuqi/Fuwuqi";
String paths="name="+name+"preass="+password;
try {
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("post");
connection.setDoOutput(true);
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.getOutputStream().write(paths.getBytes());
if (connection.getResponseCode()==200) {
return EntityUtils.toString((HttpEntity) connection.getErrorStream());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
//main主方法
package com.example.httpurlconnection_get_post;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText editText1;
private EditText editText2;
private String name;
private String password;
private String name2;
private String password2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
}
public void getway(View view){
name = editText1.getText().toString().trim();
password = editText2.getText().toString().trim();
new Thread(new Runnable() {
@Override
public void run() {
String ulits = httpURlUlist.getUlits(name, password);
if (ulits!=null) {
Log.e("xxx", ulits);
}
else {
Log.e("xxx", "空");
}
}
}).start();
}
public void postway(View view){
name2 = editText1.getText().toString().trim();
password2 = editText2.getText().toString().trim();
new Thread(new Runnable() {
@Override
public void run() {
try {
String ulits = httpURlUlist.postUlits(name2, password2);
if (ulits!=null) {
Log.e("xxx", ulits);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
- httpurlconnection_get_post
- httpurlconnection_get_post
- Android判断机器是否联网
- #97 Maximum Depth of Binary Tree
- Swift-- 计算文字尺寸
- HashSet实现原理-源码调试
- C3P0连接池详解及配置
- httpurlconnection_get_post
- Activiti explorer demo 部署无法加载完成问题
- ubuntu 14.04 上 jvpn 使用说明
- android(2):添加控件与显示问题+功能直接的关系
- 糟糕程序员的各种迹象
- label如何对齐
- 【二分图匹配】HDU2063-过山车【模板题】
- 插入排序算法
- Dialog对话框的使用