Android Studioアクセスネットワーク-Post方式
3559 ワード
ネットワーク権限を追加 Android Manifest.xml
public bootlean deallogin(){ bollan bLogin=false;
//
String StrResponseBody=null;
String strul = 「http://www.appio.cn/WebTest/LoginCheck";
String username="admin"
String password="1234"
String stredata=「username=」+username+「&password=」+password;
try{
byte[]request Body=stredata.getByttes(「UTF-8」)
URL url=new URL(strul);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection()
httpURLConnect.set Connect Timeout(3000) //接続タイムアウト時間を設定します。
httpURLConnect.set DonInput(true); //サーバからデータを取得するために、入力ストリームを開きます。
httpURLConnection.set DoOutput(true) //サーバにデータを送信するために出力ストリームを開きます。
httpURLConnect.set Request Method(「POST」); //Post形式でデータを提出する設定
httpURLConnection.setsUseCaches(false); //Post方式ではキャッシュは使えません。
httpURLConnect.set Request Property(「Content-Type」、「appication/x-wn-form-urlencoded」)、/設定要求体のタイプはテキストタイプです。
httpURLConnect.set Request Property(「Content-Length」、String.valueOf(request Body.length);/要求体の長さを設定する。
OutputStream out putStream=httpURLConnect.get Output Stream();
out putStream.write(request Body);
intレスポンス=httpURLConnection.get ResonseCode; //サーバの応答コードを取得します。
if(レスポンス=HttpURLConnection.HTTPOOK){
InputStream inptStream=httpURLConnection.get InputStream()
//応答体のバイト配列を取得します。
StrResonseBody=dealResonseResoult(inptStream);
if(StrResonseBody==OK)
{
bLogin=true;
}
//応答ヘッダを取得
//レススタティック・ヘッド=get ResonseHeader(conn)
}//if
}catch(IOException e){
e.print StockTrace();
//return"err:"+e.get Message().toString();
}
//
return b Login;
}
public bootlean deallogin(){ bollan bLogin=false;
//
String StrResponseBody=null;
String strul = 「http://www.appio.cn/WebTest/LoginCheck";
String username="admin"
String password="1234"
String stredata=「username=」+username+「&password=」+password;
try{
byte[]request Body=stredata.getByttes(「UTF-8」)
URL url=new URL(strul);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection()
httpURLConnect.set Connect Timeout(3000) //接続タイムアウト時間を設定します。
httpURLConnect.set DonInput(true); //サーバからデータを取得するために、入力ストリームを開きます。
httpURLConnection.set DoOutput(true) //サーバにデータを送信するために出力ストリームを開きます。
httpURLConnect.set Request Method(「POST」); //Post形式でデータを提出する設定
httpURLConnection.setsUseCaches(false); //Post方式ではキャッシュは使えません。
httpURLConnect.set Request Property(「Content-Type」、「appication/x-wn-form-urlencoded」)、/設定要求体のタイプはテキストタイプです。
httpURLConnect.set Request Property(「Content-Length」、String.valueOf(request Body.length);/要求体の長さを設定する。
OutputStream out putStream=httpURLConnect.get Output Stream();
out putStream.write(request Body);
intレスポンス=httpURLConnection.get ResonseCode; //サーバの応答コードを取得します。
if(レスポンス=HttpURLConnection.HTTPOOK){
InputStream inptStream=httpURLConnection.get InputStream()
//応答体のバイト配列を取得します。
StrResonseBody=dealResonseResoult(inptStream);
if(StrResonseBody==OK)
{
bLogin=true;
}
//応答ヘッダを取得
//レススタティック・ヘッド=get ResonseHeader(conn)
}//if
}catch(IOException e){
e.print StockTrace();
//return"err:"+e.get Message().toString();
}
//
return b Login;
}
public String dealResponseResult(InputStream inputStream) {
String resultData = null; //
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
while((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
resultData = new String(byteArrayOutputStream.toByteArray());
return resultData;
}