- 浏览: 129352 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
memoryisking:
可以看看这篇文章,构建一个简单的线程池,这个是struts教程 ...
java 线程池示例(自己实现的参考别人的代码) -
zwq194:
坑爹啊,误导人,代码有问题
java 线程池示例(自己实现的参考别人的代码) -
zhouming4455:
貌似你传错了哦
java 线程池示例(自己实现的参考别人的代码)
HttpURLConnection
Java代码
URL sourceUrl;
String fileName ="";
try {
sourceUrl = new URL("网址");
fileName = sourceUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
fileName = "/sdcard/"+(new Date()).getTime()+fileName;
/*创建临时文件
File myTempFile = File.createTempFile("temfile", "."+"mp3");//文件扩展名
记录临时文件名
currentTempFilePath = myTempFile.getAbsolutePath();
*/
FileOutputStream fos = new FileOutputStream(fileName);
int read = 0;
byte[] buffer = new byte[512];
HttpURLConnection conn = (HttpURLConnection) sourceUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
do{
read = is.read(buffer);
if(read > 0){
fos.write(buffer, 0, read);
}
}while(read != -1);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
if(fileName !=""){
File file = new File(fileName);
if (file.exists()){
//根据filename,操作这个文件
}
}
URL sourceUrl;
String fileName ="";
try {
sourceUrl = new URL("网址");
fileName = sourceUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
fileName = "/sdcard/"+(new Date()).getTime()+fileName;
/*创建临时文件
File myTempFile = File.createTempFile("temfile", "."+"mp3");//文件扩展名
记录临时文件名
currentTempFilePath = myTempFile.getAbsolutePath();
*/
FileOutputStream fos = new FileOutputStream(fileName);
int read = 0;
byte[] buffer = new byte[512];
HttpURLConnection conn = (HttpURLConnection) sourceUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
do{
read = is.read(buffer);
if(read > 0){
fos.write(buffer, 0, read);
}
}while(read != -1);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
if(fileName !=""){
File file = new File(fileName);
if (file.exists()){
//根据filename,操作这个文件
}
}
Java代码
URL imageUrl = null;
Bitmap bitmap = null;
try
{
/* new URL对象将网址传入 */
imageUrl = new URL(uriPic);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
/* 取得连接 */
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
/* 取得返回的InputStream */
InputStream is = conn.getInputStream();
mTextView1.setText(conn.getResponseCode()+"="+conn.getResponseMessage());
/* 将InputStream变成Bitmap */
bitmap = BitmapFactory.decodeStream(is);
/* 关闭InputStream */
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
URL imageUrl = null;
Bitmap bitmap = null;
try
{
/* new URL对象将网址传入 */
imageUrl = new URL(uriPic);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
/* 取得连接 */
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
/* 取得返回的InputStream */
InputStream is = conn.getInputStream();
mTextView1.setText(conn.getResponseCode()+"="+conn.getResponseMessage());
/* 将InputStream变成Bitmap */
bitmap = BitmapFactory.decodeStream(is);
/* 关闭InputStream */
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
处理文字数据
Java代码
/* 将InputStream转成Reader */
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
/* 图文件路径 */
String uriPic = "";
/* 一行一行读取 */
while ((inputLine = in.readLine()) != null)
{
uriPic += inputLine;
}
/* 将InputStream转成Reader */
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
/* 图文件路径 */
String uriPic = "";
/* 一行一行读取 */
while ((inputLine = in.readLine()) != null)
{
uriPic += inputLine;
}
URLConnection 获取图片
Java代码
URL aryURI = new URL(myImageURL[position]);
URLConnection conn = aryURI.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
is.close();
imageView.setImageBitmap(bm);
URL aryURI = new URL(myImageURL[position]);
URLConnection conn = aryURI.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
is.close();
imageView.setImageBitmap(bm);
注:URL可以直接InputStream is = URL.openStream();
XML 的应用
Java代码
URL url = new URL(ConstData.queryString+cityParamString);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
myHandler gwh = new myHandler();
xr.setContentHandler(gwh);
InputStreamReader isr =new InputStreamReader(url.openStream(),"GBK");
InputSource is=new InputSource(isr);
xr.parse(is);
URL url = new URL(ConstData.queryString+cityParamString);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
myHandler gwh = new myHandler();
xr.setContentHandler(gwh);
InputStreamReader isr =new InputStreamReader(url.openStream(),"GBK");
InputSource is=new InputSource(isr);
xr.parse(is);
向服务器上传文件
Java代码
private void uploadFile()
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设置DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}
private void uploadFile()
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设置DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}
DefaultHttpClient
用户登录验证
Java代码
/* 账号:david */
/* 密码:1234 */
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/TestLogin/index.php";
String strRet = "";
try
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httpost = new HttpPost(uriAPI);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("uid", strUID));
nvps.add(new BasicNameValuePair("upw", strUPW));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
//entity = response.getEntity();
Log.d(TAG, "HTTP POST getStatusLine: " + response.getStatusLine());
/* HTML POST response BODY */
strRet = EntityUtils.toString(entity);
Log.i(TAG, strRet);
strRet = strRet.trim().toLowerCase();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (entity != null)
{
entity.consumeContent();
}
Log.d(TAG, "HTTP POST Initialize of cookies.");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty())
{
Log.d(TAG, "HTTP POST Cookie not found.");
Log.i(TAG, entity.toString());
}
else
{
for (int i = 0; i < cookies.size(); i++)
{
Log.d(TAG, "HTTP POST Found Cookie: " + cookies.get(i).toString());
}
}
if(strRet.equals("y"))
{
Log.i("TEST", "YES");
return true;
}
else
{
Log.i("TEST", "NO");
return false;
}
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
/* 账号:david */
/* 密码:1234 */
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/TestLogin/index.php";
String strRet = "";
try
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httpost = new HttpPost(uriAPI);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("uid", strUID));
nvps.add(new BasicNameValuePair("upw", strUPW));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
//entity = response.getEntity();
Log.d(TAG, "HTTP POST getStatusLine: " + response.getStatusLine());
/* HTML POST response BODY */
strRet = EntityUtils.toString(entity);
Log.i(TAG, strRet);
strRet = strRet.trim().toLowerCase();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (entity != null)
{
entity.consumeContent();
}
Log.d(TAG, "HTTP POST Initialize of cookies.");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty())
{
Log.d(TAG, "HTTP POST Cookie not found.");
Log.i(TAG, entity.toString());
}
else
{
for (int i = 0; i < cookies.size(); i++)
{
Log.d(TAG, "HTTP POST Found Cookie: " + cookies.get(i).toString());
}
}
if(strRet.equals("y"))
{
Log.i("TEST", "YES");
return true;
}
else
{
Log.i("TEST", "NO");
return false;
}
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
未完待续
Java代码
URL sourceUrl;
String fileName ="";
try {
sourceUrl = new URL("网址");
fileName = sourceUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
fileName = "/sdcard/"+(new Date()).getTime()+fileName;
/*创建临时文件
File myTempFile = File.createTempFile("temfile", "."+"mp3");//文件扩展名
记录临时文件名
currentTempFilePath = myTempFile.getAbsolutePath();
*/
FileOutputStream fos = new FileOutputStream(fileName);
int read = 0;
byte[] buffer = new byte[512];
HttpURLConnection conn = (HttpURLConnection) sourceUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
do{
read = is.read(buffer);
if(read > 0){
fos.write(buffer, 0, read);
}
}while(read != -1);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
if(fileName !=""){
File file = new File(fileName);
if (file.exists()){
//根据filename,操作这个文件
}
}
URL sourceUrl;
String fileName ="";
try {
sourceUrl = new URL("网址");
fileName = sourceUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
fileName = "/sdcard/"+(new Date()).getTime()+fileName;
/*创建临时文件
File myTempFile = File.createTempFile("temfile", "."+"mp3");//文件扩展名
记录临时文件名
currentTempFilePath = myTempFile.getAbsolutePath();
*/
FileOutputStream fos = new FileOutputStream(fileName);
int read = 0;
byte[] buffer = new byte[512];
HttpURLConnection conn = (HttpURLConnection) sourceUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
do{
read = is.read(buffer);
if(read > 0){
fos.write(buffer, 0, read);
}
}while(read != -1);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
if(fileName !=""){
File file = new File(fileName);
if (file.exists()){
//根据filename,操作这个文件
}
}
Java代码
URL imageUrl = null;
Bitmap bitmap = null;
try
{
/* new URL对象将网址传入 */
imageUrl = new URL(uriPic);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
/* 取得连接 */
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
/* 取得返回的InputStream */
InputStream is = conn.getInputStream();
mTextView1.setText(conn.getResponseCode()+"="+conn.getResponseMessage());
/* 将InputStream变成Bitmap */
bitmap = BitmapFactory.decodeStream(is);
/* 关闭InputStream */
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
URL imageUrl = null;
Bitmap bitmap = null;
try
{
/* new URL对象将网址传入 */
imageUrl = new URL(uriPic);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
/* 取得连接 */
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
/* 取得返回的InputStream */
InputStream is = conn.getInputStream();
mTextView1.setText(conn.getResponseCode()+"="+conn.getResponseMessage());
/* 将InputStream变成Bitmap */
bitmap = BitmapFactory.decodeStream(is);
/* 关闭InputStream */
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
处理文字数据
Java代码
/* 将InputStream转成Reader */
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
/* 图文件路径 */
String uriPic = "";
/* 一行一行读取 */
while ((inputLine = in.readLine()) != null)
{
uriPic += inputLine;
}
/* 将InputStream转成Reader */
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
/* 图文件路径 */
String uriPic = "";
/* 一行一行读取 */
while ((inputLine = in.readLine()) != null)
{
uriPic += inputLine;
}
URLConnection 获取图片
Java代码
URL aryURI = new URL(myImageURL[position]);
URLConnection conn = aryURI.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
is.close();
imageView.setImageBitmap(bm);
URL aryURI = new URL(myImageURL[position]);
URLConnection conn = aryURI.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
is.close();
imageView.setImageBitmap(bm);
注:URL可以直接InputStream is = URL.openStream();
XML 的应用
Java代码
URL url = new URL(ConstData.queryString+cityParamString);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
myHandler gwh = new myHandler();
xr.setContentHandler(gwh);
InputStreamReader isr =new InputStreamReader(url.openStream(),"GBK");
InputSource is=new InputSource(isr);
xr.parse(is);
URL url = new URL(ConstData.queryString+cityParamString);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
myHandler gwh = new myHandler();
xr.setContentHandler(gwh);
InputStreamReader isr =new InputStreamReader(url.openStream(),"GBK");
InputSource is=new InputSource(isr);
xr.parse(is);
向服务器上传文件
Java代码
private void uploadFile()
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设置DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}
private void uploadFile()
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设置DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}
DefaultHttpClient
用户登录验证
Java代码
/* 账号:david */
/* 密码:1234 */
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/TestLogin/index.php";
String strRet = "";
try
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httpost = new HttpPost(uriAPI);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("uid", strUID));
nvps.add(new BasicNameValuePair("upw", strUPW));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
//entity = response.getEntity();
Log.d(TAG, "HTTP POST getStatusLine: " + response.getStatusLine());
/* HTML POST response BODY */
strRet = EntityUtils.toString(entity);
Log.i(TAG, strRet);
strRet = strRet.trim().toLowerCase();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (entity != null)
{
entity.consumeContent();
}
Log.d(TAG, "HTTP POST Initialize of cookies.");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty())
{
Log.d(TAG, "HTTP POST Cookie not found.");
Log.i(TAG, entity.toString());
}
else
{
for (int i = 0; i < cookies.size(); i++)
{
Log.d(TAG, "HTTP POST Found Cookie: " + cookies.get(i).toString());
}
}
if(strRet.equals("y"))
{
Log.i("TEST", "YES");
return true;
}
else
{
Log.i("TEST", "NO");
return false;
}
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
/* 账号:david */
/* 密码:1234 */
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/TestLogin/index.php";
String strRet = "";
try
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httpost = new HttpPost(uriAPI);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("uid", strUID));
nvps.add(new BasicNameValuePair("upw", strUPW));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
//entity = response.getEntity();
Log.d(TAG, "HTTP POST getStatusLine: " + response.getStatusLine());
/* HTML POST response BODY */
strRet = EntityUtils.toString(entity);
Log.i(TAG, strRet);
strRet = strRet.trim().toLowerCase();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (entity != null)
{
entity.consumeContent();
}
Log.d(TAG, "HTTP POST Initialize of cookies.");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty())
{
Log.d(TAG, "HTTP POST Cookie not found.");
Log.i(TAG, entity.toString());
}
else
{
for (int i = 0; i < cookies.size(); i++)
{
Log.d(TAG, "HTTP POST Found Cookie: " + cookies.get(i).toString());
}
}
if(strRet.equals("y"))
{
Log.i("TEST", "YES");
return true;
}
else
{
Log.i("TEST", "NO");
return false;
}
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
未完待续
相关推荐
import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import java.util.Map; public class HttpTest { private HttpURLConnection hc = ...
HttpURLConnection文件下载\httpURLConnection文件下载
正文:我在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,responseCode = -1。一直想不明白,同一个程序我调用了两次,结果有一个链接一直OK,另一个却一直报这个错误。后来发现两个...
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();// 取得连接 conn.connect(); InputStream is = conn.getInputStream();//取得返回的InputStream bitmap = BitmapFactory....
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
自己写了一个基于httpurlconnection和Asynctask的例子
调用URL对象的openConnection( )来获取HttpURLConnection对象实例: HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 设置HTTP请求使用的方法:conn.setRequestMethod("GET"); 设置连接超时,...
HttpURLConnection实现三方接口HTTP调用调用,带有FILE文件参数
使用HttpURLConnection或HttpClient方式传json值到spring中
主要介绍了java后台调用HttpURLConnection类模拟浏览器请求实例,该实例可用于接口调用,具有一定的实用价值,需要的朋友可以参考下
实例Demo程序来示范使用HttpURLConnection实现多线程下载。 使用多线程下载文件可以更快完成文件的下载,因为客户端启动多条线程进行下载就意味着服务器也需要为该客户端提供响应的服务。假设服务器同时最多服务100...
代码包含了httpurlconnection基本操作,一些正常的操作。
HttpURLConnection使用总结示例源码
本示例使用HttpUrlConnection实现上传文件,可以将手机上的文件上传到服务器 必须与my_upload工程配合运行,先启动my_upload工程
httpurlconnection, http post, http get
java网络请求工具类 HttpURLConnection post请求工具类HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式
一个简单的例子,使用HttpURLConnection下载图片
HttpURLConnection调用三方HTTP接口,带有FILE文件参数
Android HttpUrlConnection使用方法 post and get json.