androidpost圖片
A. [Android]HTTPPOST請求參數和效果
一、HTTPPOST請求
1、定義請求的相關參數:
定義請求路徑:
2、String url=」http:本地Ip地址:8080/Web工程名」;
3、連接請求
4、設置需要傳遞的參數:
List<NameValuePair> params=new ArrayList<NameValuePair>;
params.add(new BasicNameValuePair(「後台需要傳遞的值[鍵]」 , 」Android中傳遞的值[值]」));
5、設置字元集(防止亂碼):
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
6、客戶端發送請求
Response=client.exequte(post);
7、判斷請求是否成功:
If(response..getStatusLine().getStatusCode()==200){
}
二、Android自定義動畫效果
1、定義某個類繼續SurfaceViewHolder 並實現CallBack介面
註:CallBack介面是import android.view.SurfaceHolder.Callback;
2、定義SurfaceviewHolder holder對象
Holder=this.getHolder();
Holder.addCallBack(this);
3、在surfaceChanged中處理線程問題:
}catch(Exceptione(){
}
).start();
三、Android游戲中的四種基本動畫效果
1、漸變(alpha)
Animation anmation=new AnimationUtils().loadAnimation(MainActivity.this, R.anim.alpha);
<set>
<alphaandroid:fromAlpha="0.1" android:toAlpha="1.0"android:ration="2000"/>
</set>
此漸變效果由模糊變得清晰
2、縮放(scale)
Animation scale=new AnimationUtils().loadAnimation(MainActivity.this,R.anim.scale);
p_w_picpath.startAnimation(scale);
<set>
<scale
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"android:ration="2000"/>
</set>
定義圖片從某個X點到某個X點開始動畫等一系列信息動畫
3、旋轉(rotate)
Animation rotate=new AnimationUtils().loadAnimation(MainActivity.this,R.anim.rotate);
p_w_picpath.startAnimation(rotate);
<set>
<rotate
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:ration="2000"/>
</set>
圖片以0度角開始以順時針旋轉,並且以圖片的中間點開始動畫,所用時間為2s
4、平移(translate)
Animationtranslate=new AnimationUtils().loadAnimation(MainActivity.this,R.anim.translate);
p_w_picpath.startAnimation(translate);
<set>
<translate
android:fromXDelta="10"
android:toXDelta="100"
android:fromYDelta="10"
android:toYDelta="100"
android:ration="2000"/>
</set>
效果和縮放差不多類似
5、Frame動畫
註:frame動畫當點擊暫停後,所有效果從最開始重新播放,所以對於動畫來說,最好使用SurfaceViewHolder來播放動畫效果
實現步驟:
1)在res目錄下的anim中定義一個xml文件 eg:frame.xml
<animation-list><itemandroid:drawable="@drawable/img1"android:ration="300">
</item></animation-list>
在此xml文件中的item裡面找到要播放的圖片以及每張圖片播放的時間效果
2)在res的layout目錄下定義一個xml,這裡面加入一個ImageView控制項,並定義相關的id屬性和Width、height等
3)在Activity中獲取ImageView控制項
Eg:ImageViewp_w_picpath=(ImageView)R.layout.p_w_picpath;
A)為p_w_picpath載入動畫
p_w_picpaths.setBackgroundResource(R.anim.frame);
B)定義AnimationDrawable 為每個p_w_picpath設置背景
AnimationDrawable frameAnimation;
frameAnimation=(AnimationDrawable) p_w_picpaths.getBackground();
註:文章出處來自51CTO博客作者EverythingTK
Android
https://blog.51cto.com/cheerupfortk/1219830
B. android 上傳圖片或文件都是怎麼弄的
一:可以使用httppost上傳文件
二:或者socket寫入文件
上面兩種都可以獲得二進制流,然後把文件寫入流,這一類網路操作最好使用非同步任務模型
C. Android用post方式上傳到伺服器的問題
在HTTP通信中使用最多的就是GET和POST了,GET請求可以獲取靜態頁面,也可以把參數放在URL字元串的後面,傳遞給伺服器。POST與GET的不同之處在於POST的參數不是放在URL字元串裡面,而是放在HTTP請求數據中。
android 用post方式上傳圖片到伺服器的示例代碼如下:
java">/**
*上傳文件到伺服器類
*/
publicclassUploadUtil{
privatestaticfinalStringTAG="uploadFile";
privatestaticfinalintTIME_OUT=10*1000;//超時時間
="utf-8";//設置編碼
/**
*Android上傳文件到服務端
*
*@paramfile需要上傳的文件
*@paramRequestURL請求的rul
*@return返回響應的內容
*/
publicstaticStringuploadFile(Filefile,StringRequestURL){
Stringresult=null;
StringBOUNDARY=UUID.randomUUID().toString();//邊界標識隨機生成
StringPREFIX="--",LINE_END=" ";
StringCONTENT_TYPE="multipart/form-data";//內容類型
try{
URLurl=newURL(RequestURL);
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(TIME_OUT);
conn.setConnectTimeout(TIME_OUT);
conn.setDoInput(true);//允許輸入流
conn.setDoOutput(true);//允許輸出流
conn.setUseCaches(false);//不允許使用緩存
conn.setRequestMethod("POST");//請求方式
conn.setRequestProperty("Charset",CHARSET);//設置編碼
conn.setRequestProperty("connection","keep-alive");
conn.setRequestProperty("Content-Type",CONTENT_TYPE+";boundary="+BOUNDARY);
if(file!=null){
/**
*當文件不為空,把文件包裝並且上傳
*/
DataOutputStreamdos=newDataOutputStream(conn.getOutputStream());
StringBuffersb=newStringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
/**
*這里重點注意:name裡面的值為服務端需要key只有這個key才可以得到對應的文件
*filename是文件的名字,包含後綴名的比如:abc.png
*/
sb.append("Content-Disposition:form-data;name="uploadfile";filename=""
+file.getName()+"""+LINE_END);
sb.append("Content-Type:application/octet-stream;charset="+CHARSET+LINE_END);
sb.append(LINE_END);
dos.write(sb.toString().getBytes());
InputStreamis=newFileInputStream(file);
byte[]bytes=newbyte[1024];
intlen=0;
while((len=is.read(bytes))!=-1){
dos.write(bytes,0,len);
}
is.close();
dos.write(LINE_END.getBytes());
byte[]end_data=(PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
dos.write(end_data);
dos.flush();
/**
*獲取響應碼200=成功當響應成功,獲取響應的流
*/
intres=conn.getResponseCode();
Log.e(TAG,"responsecode:"+res);
//if(res==200)
//{
Log.e(TAG,"requestsuccess");
InputStreaminput=conn.getInputStream();
StringBuffersb1=newStringBuffer();
intss;
while((ss=input.read())!=-1){
sb1.append((char)ss);
}
result=sb1.toString();
Log.e(TAG,"result:"+result);
//}
//else{
//Log.e(TAG,"requesterror");
//}
}
}catch(MalformedURLExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
returnresult;
}
/**
*通過拼接的方式構造請求內容,實現參數傳輸以及文件傳輸
*
*@paramurlServicenetaddress
*@paramparamstextcontent
*@paramfilespictures
*@
*@throwsIOException
*/
publicstaticStringpost(Stringurl,Map<String,String>params,Map<String,File>files)
throwsIOException{
StringBOUNDARY=java.util.UUID.randomUUID().toString();
StringPREFIX="--",LINEND=" ";
StringMULTIPART_FROM_DATA="multipart/form-data";
StringCHARSET="UTF-8";
URLuri=newURL(url);
HttpURLConnectionconn=(HttpURLConnection)uri.openConnection();
conn.setReadTimeout(10*1000);//緩存的最長時間
conn.setDoInput(true);//允許輸入
conn.setDoOutput(true);//允許輸出
conn.setUseCaches(false);//不允許使用緩存
conn.setRequestMethod("POST");
conn.setRequestProperty("connection","keep-alive");
conn.setRequestProperty("Charsert","UTF-8");
conn.setRequestProperty("Content-Type",MULTIPART_FROM_DATA+";boundary="+BOUNDARY);
//首先組拼文本類型的參數
StringBuildersb=newStringBuilder();
for(Map.Entry<String,String>entry:params.entrySet()){
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition:form-data;name=""+entry.getKey()+"""+LINEND);
sb.append("Content-Type:text/plain;charset="+CHARSET+LINEND);
sb.append("Content-Transfer-Encoding:8bit"+LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}
DataOutputStreamoutStream=newDataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());
//發送文件數據
if(files!=null)
for(Map.Entry<String,File>file:files.entrySet()){
StringBuildersb1=newStringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition:form-data;name="uploadfile";filename=""
+file.getValue().getName()+"""+LINEND);
sb1.append("Content-Type:application/octet-stream;charset="+CHARSET+LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStreamis=newFileInputStream(file.getValue());
byte[]buffer=newbyte[1024];
intlen=0;
while((len=is.read(buffer))!=-1){
outStream.write(buffer,0,len);
}
is.close();
outStream.write(LINEND.getBytes());
}
//請求結束標志
byte[]end_data=(PREFIX+BOUNDARY+PREFIX+LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
//得到響應碼
intres=conn.getResponseCode();
InputStreamin=conn.getInputStream();
StringBuildersb2=newStringBuilder();
if(res==200){
intch;
while((ch=in.read())!=-1){
sb2.append((char)ch);
}
}
outStream.close();
conn.disconnect();
returnsb2.toString();
}
}
D. Android 圖片上傳(同時支持拍照上傳和從相冊上傳)以二進制流的方式post上去,欄位「img
我幫你 記得給我好評哦 聯系我吧 騙子不要來
E. android 文件流的方式多張圖片上傳,並多個參數
android 開發中圖片上傳是很正常的,有兩種可用的方式:
下面我們就說明一下以文件流上傳圖片的方式, 實現網路框架是Retrofit
測試上傳3張手機sd卡中的圖片,並傳人了參數EquipmentCode, Description, ReportUserCode等
其中的思路是: Post的方式,Content-Type:multipart/form-data的類型進行上傳文件的。
其中MultipartBody是RequestBody的擴展,
看看請求頭的信息, 請求中攜帶了所有信(如果介面開發人員說不能收到, 叫他自己想想,截圖給他,哈哈哈:)
上面的是上傳了3張圖片,如果一張,只要傳一個就行!
就這樣,圖片上傳的兩種方式ok拉,測試通過的,保證正確!
參考: https://www.jianshu.com/p/acfefb0a204f
F. Android如何獲取網路圖片
android中獲取網路圖片是一件耗時的操作,如果直接獲取有可能會出現應用程序無響應(ANR:Application Not Responding)對話框的情況。對於這種情況,一般的方法就是耗時操作用線程來實現。下面列三種獲取url圖片的方法:
直接獲取:(容易:ANR,不建議)
mImageView=(ImageView)this.findViewById(R.id.imageThreadConcept);
Drawabledrawable=loadImageFromNetwork(IMAGE_URL);
mImageView.setImageDrawable(drawable);
2. 後台線程獲取url圖片:
mImageView=(ImageView)this.findViewById(R.id.imageThreadConcept);
newThread(newRunnable(){
Drawabledrawable=loadImageFromNetwork(IMAGE_URL);
@Override
publicvoidrun(){
//post()特別關鍵,就是到UI主線程去更新圖片
mImageView.post(newRunnable(){
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
mImageView.setImageDrawable(drawable);
}});
}
}).start();
3.AsyncTask獲取url圖片
mImageView=(ImageView)this.findViewById(R.id.imageThreadConcept);
newDownloadImageTask().execute(IMAGE_URL);
<String,Void,Drawable>
{
(String...urls){
returnloadImageFromNetwork(urls[0]);
}
protectedvoidonPostExecute(Drawableresult){
mImageView.setImageDrawable(result);
}
}
G. android中Handler的post方法的作用是什麼
在工作線程中執行耗時任務,當任務完成時,會返回UI線程,一般是更新UI。這時有兩種方法可以達到目的。
一種是handler.sendMessage。發一個消息,再根據消息,執行相關任務代碼。
另一種是handler.post(r)。r是要執行的任務代碼。意思就是說r的代碼實際是在UI線程執行的。可以寫更新UI的代碼。
(7)androidpost圖片擴展閱讀
一個APK文件結構為:
1、META-INF (註:Jar文件中常可以看到);
2、res (註:存放資源文件的目錄) ;
3、AndroidManifest.xml (註:程序全局配置文件) ;
4、classes.dex (註:Dalvik位元組碼);
5、resources.arsc (註:編譯後的二進制資源文件)。
Android在運行一個程序時首先需要UnZip,然後類似Symbian那樣直接執行安裝,和Windows Mobile中的PE文件有區別,這樣做對於程序的保密性和可靠性不是很高,通過dexmp命令可以反編譯,但這樣做符合發展規律,微軟的 Windows Gadgets或者說WPF也採用了這種構架方式。
在Android平台中dalvik vm的執行文件被打包為apk格式,最終運行時載入器會解壓然後獲取編譯後androidmanifest.xml文件中的permission分支相關的安全訪問,但仍然存在很多安全限制,如果你將apk文件傳到/system/app文件夾下會發現執行是不受限制的。
參考資料來源:網路-Android
H. Android提交post請求上傳圖片,Java如何獲取HttpEntity裡面的東西
你好!
那個是客戶端,伺服器端,寫servlet等服務程序去接收、或者使用smartupload接收,,,,,可以使用apache的upload組件、struts的上傳功能、等,很多方法
僅代表個人觀點,不喜勿噴,謝謝。
I. 安卓(android)開發,使用post方法向伺服器提交數據,得不到返回值,具體見截圖
出錯了 但是錯誤信息被try包起來了 你可以在catch里邊列印錯誤信息
J. android 怎麼發送post請求並接收二進制數據
可使用android自帶的httpclient框架實現向伺服器發起get或post請求,以下為完整的示例代碼:
1. GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}