删除 MyPost.java
parent
07673027f0
commit
226666daca
|
|
@ -1,167 +0,0 @@
|
||||||
package iku.os.net;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
|
|
||||||
public class MyPost {
|
|
||||||
|
|
||||||
private static final String TAG = "MyPost";
|
|
||||||
private static okhttp3.OkHttpClient oklient = new okhttp3.OkHttpClient();
|
|
||||||
|
|
||||||
public String PostData(Context context, byte[] byt, String url_) {
|
|
||||||
|
|
||||||
byte[] bytes = postDataBytes(context, byt, url_);
|
|
||||||
|
|
||||||
if (bytes != null && bytes.length > 0) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
return new String(bytes, "UTF-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String PostDataCommon(Context context, byte[] byt, String url_) {
|
|
||||||
byte[] bytes = postDataBytes(context, byt, url_);
|
|
||||||
|
|
||||||
if (bytes != null && bytes.length >= 0) {
|
|
||||||
if (bytes.length == 0) {
|
|
||||||
return "";
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
return new String(bytes, "UTF-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public String httpGet(String url) throws Exception {
|
|
||||||
|
|
||||||
URL obj = new URL(url);
|
|
||||||
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
||||||
|
|
||||||
//默认值为GET
|
|
||||||
con.setRequestMethod("GET");
|
|
||||||
|
|
||||||
int responseCode = con.getResponseCode();
|
|
||||||
System.out.println("\nSending 'GET' request to URL : " + url);
|
|
||||||
System.out.println("Response Code : " + responseCode);
|
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(
|
|
||||||
new InputStreamReader(con.getInputStream()));
|
|
||||||
String inputLine;
|
|
||||||
StringBuffer response = new StringBuffer();
|
|
||||||
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
|
||||||
response.append(inputLine);
|
|
||||||
}
|
|
||||||
in.close();
|
|
||||||
|
|
||||||
//打印结果
|
|
||||||
System.out.println(response.toString());
|
|
||||||
return response.toString();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] postDataBytes(Context context, byte[] byt, String url_) {
|
|
||||||
String result = null;
|
|
||||||
HttpURLConnection httpUrlConnection = null;
|
|
||||||
InputStream inStrm = null;
|
|
||||||
ByteArrayOutputStream baos = null;
|
|
||||||
BufferedInputStream bis = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
httpUrlConnection = NetWorkUtils.getHttpURLConnection(context, url_);
|
|
||||||
|
|
||||||
httpUrlConnection.setAllowUserInteraction(true);
|
|
||||||
httpUrlConnection.setDoOutput(true);
|
|
||||||
httpUrlConnection.setDoInput(true);
|
|
||||||
httpUrlConnection.setUseCaches(false);
|
|
||||||
httpUrlConnection.setRequestProperty("Connection", "close");//add 20200428
|
|
||||||
httpUrlConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
|
|
||||||
// httpUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
||||||
httpUrlConnection.setRequestMethod("POST");
|
|
||||||
httpUrlConnection.setConnectTimeout(20000);
|
|
||||||
OutputStream outStrm = null;
|
|
||||||
|
|
||||||
|
|
||||||
outStrm = httpUrlConnection.getOutputStream();
|
|
||||||
|
|
||||||
|
|
||||||
outStrm.write(byt);
|
|
||||||
outStrm.flush();
|
|
||||||
outStrm.close();
|
|
||||||
|
|
||||||
inStrm = httpUrlConnection.getInputStream();
|
|
||||||
|
|
||||||
baos = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
bis = new BufferedInputStream(inStrm);
|
|
||||||
byte[] buf = new byte[1024];
|
|
||||||
int readSize = -1;
|
|
||||||
|
|
||||||
while ((readSize = bis.read(buf)) != -1) {
|
|
||||||
baos.write(buf, 0, readSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] data = baos.toByteArray();
|
|
||||||
|
|
||||||
return data;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
result = null;
|
|
||||||
} finally {
|
|
||||||
|
|
||||||
if (baos != null) {
|
|
||||||
try {
|
|
||||||
baos.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (bis != null) {
|
|
||||||
try {
|
|
||||||
bis.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inStrm != null) {
|
|
||||||
try {
|
|
||||||
inStrm.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (httpUrlConnection != null) {
|
|
||||||
httpUrlConnection.disconnect();
|
|
||||||
httpUrlConnection = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.gc();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue