URLConnection格式与用法

private void getdialog() {
final EditText et = new EditText(this);
final String workid = this.workid;
new AlertDialog.Builder(this).setTitle("请输入面积").setView(et).
setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
final String size = et.getText().toString();
new Thread() {
@Override
public void run() {
httpget(workid, size);
}
}.start();
//Log.i("size",size);

}
}).show();
}

public String httpget(String workid, String size) {

String result = "";
BufferedReader in = null;
StringBuilder buf = new StringBuilder("http://www.agribiotech.cn/record/record/sizerecord");
buf.append("?");
buf.append("workid=" + workid + "&");
buf.append("size=" + size);


try {
URL url = null;
url = new URL(buf.toString());
URLConnection conn = url.openConnection();
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
Map<String, List<String>> map = conn.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}

} catch (IOException e) {
Log.i("warn", e.toString());
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;

}
posted @ 2016-07-19 10:21  Wei_java  阅读(487)  评论(0编辑  收藏  举报