1. 程式人生 > >Android用httpURLConnection傳送post網路請求並拿到資料

Android用httpURLConnection傳送post網路請求並拿到資料

package com.boyouhui.www.http;


/**
 * 本類發http請求,記著要開網路許可權
 * 
 */
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import org.json.JSONObject;


import com.boyouhui.www.util.MyParameter;


import android.hardware.Camera.Parameters;
import android.util.Log;


public class HttpRequest {
// 兩個引數1路徑2往流裡寫的資料
public static String methodPost(String path, String dataStr) {
String bStr = null;
DataOutputStream dataOutputStream = null;
ByteArrayOutputStream outputStream = null;
InputStream inputStream = null;


try {
// //生成一個json資料,準備傳到伺服器
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("name", "張三");
// jsonObject.put("age", "20");
// //拼接起來的,帶有json資料的路徑
// String jsonStr = "param=" + jsonObject.toString();


/**
* 把路徑原始路徑寫到URL中
*/


URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 資料的型別
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 向伺服器傳資料的長度
httpURLConnection.setRequestProperty("Content-Length",
String.valueOf(dataStr.length()));
// 得到輸出流並封裝
dataOutputStream = new DataOutputStream(
httpURLConnection.getOutputStream());


/**
* 把要傳的引數寫到流中

*/

//dataOutputStream.writeBytes(dataStr);
            dataOutputStream.write(dataStr.getBytes());
if (httpURLConnection.getResponseCode() == 200) {
Log.i("1", "進到200連線成功。。。");
inputStream = httpURLConnection.getInputStream();
outputStream = new ByteArrayOutputStream();
byte[] bs = new byte[10];
int len = 0;


while ((len = inputStream.read(bs)) != -1) {
outputStream.write(bs, 0, len);
}
// 把資料轉化成位元組位元組陣列
byte[] b = outputStream.toByteArray();
// 把資料轉化成字串
bStr = new String(b, "UTF-8");
// 列印一下拿到的資料
Log.i("1", "從伺服器上拿到的資料,從流裝-->位元組陣列-->字串" + bStr);
//用靜態變數把資料傳出去
MyParameter.myDataString=bStr;
}


} catch (Exception e) {
e.printStackTrace();
// 如果請求傳送失敗,則返回的字串bStr=null;
// 只要在發請求的activity中判斷bStr=null,則傳送請求失敗
return bStr;


}
finally {
//// 關流有可能出錯
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}


}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}


}


}


return bStr;


}


public static String methodGet2(String path) {
String dataStr = null;


InputStream is = null;
ByteArrayOutputStream os = null;
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("GET");
if (httpURLConnection.getResponseCode() == 200) {
Log.i("1", "網路連線成功。。。");
is = httpURLConnection.getInputStream();
os = new ByteArrayOutputStream();
byte[] b = new byte[10];
int len = 0;
if ((is.read(b)) != -1) {
os.write(b, 0, len);


}


byte[] myarray = os.toByteArray();
dataStr = new String(myarray, "UTF-8");


}


} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}


}
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}


}


}


return dataStr;


};


}