import android.util.Log;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class HttpUtil {
private static final String TAG = "HttpUtil";
public static String get(String httpUrl){
String result = null;
Log.e(TAG, "httpUrl:"+httpUrl);
BufferedReader br = null;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
int responseCode = connection.getResponseCode();
Log.e(TAG, "responseCode: "+responseCode );
if(responseCode == HttpURLConnection.HTTP_OK){
StringBuffer sb = new StringBuffer();
String line = null;
while( (line = br.readLine()) != null ){
sb.append(line);
}
br.close();
Log.e(TAG, "requestResult: " + sb.toString() );
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String post(String httpUrl , HashMap hashMap){
String json = new JSONObject(hashMap).toString();
Log.e(TAG, "httpUrl:"+httpUrl);
Log.e(TAG, "request param:"+json);
String result = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
bw.write(json);
bw.flush();
int responseCode = connection.getResponseCode();
Log.e(TAG, "responseCode: "+responseCode );
if(responseCode == HttpURLConnection.HTTP_OK){
br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
StringBuffer sb = new StringBuffer();
String line = null;
while( (line = br.readLine()) != null ){
sb.append(line);
}
result = sb.toString();
Log.e(TAG, "requestResult: " + result );
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bw != null)
bw.close();
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String post(String httpUrl , String json){
Log.e(TAG, "httpUrl:"+httpUrl);
Log.e(TAG, "request param:"+json);
String result = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
bw.write(json);
bw.flush();
int responseCode = connection.getResponseCode();
Log.e(TAG, "responseCode: "+responseCode );
if(responseCode == HttpURLConnection.HTTP_OK){
br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
StringBuffer sb = new StringBuffer();
String line = null;
while( (line = br.readLine()) != null ){
sb.append(line);
}
Log.e(TAG, "requestResult: " + sb.toString() );
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bw != null)
bw.close();
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String getHeadFieldsInfo(String httpUrl){
String result = null;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
Map<String,List<String>> map = connection.getHeaderFields();
Iterator<Map.Entry<String,List<String>>> iterator = map.entrySet().iterator();
StringBuffer sb = new StringBuffer();
sb.append("HeadFields\n");
while (iterator.hasNext()){
Map.Entry<String,List<String>> entry = iterator.next();
sb.append("key:"+entry.getKey());
for(String str : entry.getValue()){
sb.append("\t\tvalue:" + str);
}
sb.append("\n");
}
Log.e(TAG, sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
HttpUtil
最新推荐文章于 2018-03-29 10:50:24 发布