當前位置:首頁 » 文件管理 » volley上傳文件

volley上傳文件

發布時間: 2022-06-18 03:42:13

1. android studio怎麼導入volleyjar包

java目錄的同級目錄新建一個文件夾,命名為「JniLibs」,然後把要導入的jar包復制到這個文件,然後在AS環境中右鍵這個jar包,選擇「Add As library」。這樣您的jar包就成功導入項目了,希望可以幫到您。


2. Volley有提供文件上傳的方法嗎還是要自己去實現

沒有,你可以自己實現

使用Post方式上傳

3. android volley stringrequest post中的getparams怎麼把text數據提交上去

JSONObject jsonObject = new JSONObject(params);
JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST,httpurl, jsonObject,new Response.Listener<JSONObject>(){});

如果是上傳json數據格式的話,直接使用JsonObjectRequest可能比較好一點,對Json數據的格式支持也比較到位。客戶端以Json串的post請求方式進行提交,服務端返回Json串

4. 我用sitemapx生成的sitemap文件從哪裡上傳從織夢後台可以嗎不懂啊

織夢有個sitemap.xml插件,dede沒有自帶附件的插件,把插件上傳到ftp,在後台就會看到一個生成按鈕,點擊生成xml插件,希望對你有幫助,我的土撥培訓也是這樣弄的。
我用volley中的jsonrequest訪問這個網址獲取json數據後,key = "obj"的value是一個String[], JSONObject本身好像並沒有String[] getStringArray(String key)這樣的方法,請問有什麼方便的方法可以直接獲取那個String[]中的每一項。
JSONArray ja=json.getJSONArray("obj");
volley想用的爽需要重新封裝下,json的處理建議用google 的gson

5. Android客戶端訪問網路時,volley與okHttp,哪一個更好

OkHttp
物理質量
使用OkHttp需要 okio.jar (80k), okhttp.jar(330k)這2個jar包,總大小差不多400k,加上自己的封裝,差不多得410k。
功能介紹
Square 公司開源的 OkHttp 是一個專注於連接效率的 HTTP 客戶端。OkHttp 提供了對 HTTP/2 和 SPDY 的支持,並提供了連接池,GZIP 壓縮和 HTTP 響應緩存功能。
優點
支持http請求,https請求。
支持文件下載。
使用的是HttpURLConnection,不要擔心android版本的變換。(至少目前是都支持的)。
支持get,post請求。
基於Http的文件上傳。
載入圖片。
缺點
比如callback回來是在線程裡面, 不能刷新UI,需要我們手動處理。
封裝比較麻煩。
Volley
物理質量
使用Volley 需要Volley.jar(120k),加上自己的封裝最多140k。
功能簡述
Volley是Goole在2013年Google I/O大會上推出了一個新的網路通信框架,它是開源的。Volley 的特點:特別適合數據量小,通信頻繁的網路操作。
優點
非常適合進行數據量不大,但通信頻繁的網路操作。
內部分裝了非同步線程。
支持get,post網路請求。
圖片下載。
可直接在主線程調用服務端並處理返回結果。
可以取消請求,容易擴展,面向介面編程
缺點
對大文件下載 Volley的表現非常糟糕。
只支持http請求。
在BasicNetwork中判斷了statusCode(statusCode < 200 || statusCode > 299),如果合條件直
圖片載入性能一般。
使用的是httpclient,HttpURLConnection。不過在android 6.0不支持httpclient了,如果想支持得添加org.apache.http.legacy.jar。
總結
在我們當前的項目 xxxSDK,xxx商城裡面,使用volley就可以了,畢竟經過了日活幾十萬的測試,至少穩定性是沒有問題的。okhttp暫時還用不上。後續如果要使用okhttp,可以再深入okhttp,給項目做重構。
不過既然轉戰Android studio,網路請求還是推薦使用Retrofit2
Retrofit 是在OkHttp上封裝的,可以參考下 Volley vs Retrofit
http://blog.csdn.net/hwz2311245/article/details/46845271

6. android volley stringrequest post中的getparams怎麼把json數據提交上去

1.客戶端以普通的post方式進行提交,服務端返回字元串
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST,httpurl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "response -> " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.getMessage(), error);
}
}) {
@Override
protected Map<String, String> getParams() {
//在這里設置需要post的參數
Map<String, String> map = new HashMap<String, String>();
map.put("name1", "value1");
map.put("name2", "value2");
return params;
}
};
requestQueue.add(stringRequest);

2.客戶端以json串的post請求方式進行提交,服務端返回json串
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
Map<String, String> map = new HashMap<String, String>();
map.put("name1", "value1");
map.put("name2", "value2");
JSONObject jsonObject = new JSONObject(params);
JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST,httpurl, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "response -> " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.getMessage(), error);
}
})
{
//注意此處override的getParams()方法,在此處設置post需要提交的參數根本不起作用
//必須象上面那樣,構成JSONObject當做實參傳入JsonObjectRequest對象里
//所以這個方法在此處是不需要的
// @Override
// protected Map<String, String> getParams() {
// Map<String, String> map = new HashMap<String, String>();
// map.put("name1", "value1");
// map.put("name2", "value2");

// return params;
// }

7. okhttp,retrofit,android-async-http,volley應該選擇哪一個

個人認為okhttp是android平台最好的網路庫。

volley是一個簡單的非同步http庫,僅此而已。缺點是不支持同步,這點會限制開發模式;不能post大數據,所以不適合用來上傳文件。

android-async-http,與volley一樣是非同步網路庫。但volley是封裝的httpUrlConnection,它是封裝的httpClient,而android平台不推薦用HttpClient了,所以這個庫已經不適合android平台了。

okhttp是高性能的http庫,支持同步、非同步,而且實現了spdy、http2、websocket協議,api很簡潔易用,和volley一樣實現了http協議的緩存。picasso就是利用okhttp的緩存機制實現其文件緩存,實現的很優雅,很正確,反例就是UIL(universal image loader),自己做的文件緩存,而且不遵守http緩存機制。

retrofit與picasso一樣都是在okhttp基礎之上做的封裝,項目中可以直接用了。

8. NoHttp和Volley哪個好用

我推薦NoHttp. 原因是NoHttp封裝了: 文件下載, 斷點續傳, 304緩存, 302/303傳參數, 傳文件, 請求頭, 多文件上傳, 大文件上傳, Cookie自動管理等多種功能, 這些是Volley而沒有, 使用Volley時這些功能要我們去寫蠻多代碼來再次封裝.
而且Volley用的HttpClient來解析的, Android6.0刪除了HttpClient後, 我們在6.0下也不能使用Volley的源碼了, 所以還是用NoHttp吧, NoHttp兼容2.0-6.0以上

9. android用volley怎麼給伺服器發送json

1.下載官網的android SDK(本人用的是eclipse)

2.新建一個android項目:

File->new->andriod Application project

7、下面就是具體的使用post和get請求的代碼:

A:發送get請求如下:

package com.example.xiaoyuantong;

import java.util.HashMap;

import java.util.Iterator;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;

/**

* Demo

*/

public class MainActivity extends Activity {

private RequestQueue requestQueue ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

private void init() {

TextView textView = (TextView)findViewById(R.id.textView);

requestQueue = Volley.newRequestQueue(this);

getJson();

textView.setText("hello");

}

private void getJson(){

String url = "http://192.168.20.1:8080/xiaoyuantong/userAction!register.action?pwd='測試'";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(

Request.Method.GET, url, null,

new Response.Listener<JSONObject>() {

@Override

public void onResponse(JSONObject response) {

//這里可以列印出接受到返回的json

Log.e("bbb", response.toString());

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError arg0) {

// System.out.println("sorry,Error");

Log.e("aaa", arg0.toString());

}

});

requestQueue.add(jsonObjectRequest);

}

}

B:發送post請求如下:

package com.example.xiaoyuantong;

import java.util.HashMap;

import java.util.Map;

import org.json.JSONException;

import org.json.JSONObject;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;

import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import android.view.Menu;

import android.widget.TextView;

public class PostActivity extends Activity {

private RequestQueue requestQueue ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_post);

init();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.post, menu);

return true;

}

private void init() {

TextView textView = (TextView)findViewById(R.id.postView);

requestQueue = Volley.newRequestQueue(this);

getJson();

textView.setText("hellopost");

}

private void getJson(){

String url = "http://192.168.20.1:8080/xiaoyuantong/userAction!reg.action";

JsonObjectRequest jsonObjectRequest ;

JSONObject jsonObject=new JSONObject() ;

try {

jsonObject.put("name", "張三");

jsonObject.put("sex", "女");

} catch (JSONException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

//列印前台向後台要提交的post數據

Log.e("post",jsonObject.toString());

//發送post請求

try{

jsonObjectRequest = new JsonObjectRequest(

Request.Method.POST, url, jsonObject,

new Response.Listener<JSONObject>() {

@Override

public void onResponse(JSONObject response) {

//列印請求後獲取的json數據

Log.e("bbb", response.toString());

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError arg0) {

// System.out.println("sorry,Error");

Log.e("aaa", arg0.toString());

}

});

requestQueue.add(jsonObjectRequest);

} catch (Exception e) {

e.printStackTrace();

System.out.println(e + "");

}

requestQueue.start();

}

}

8、在android的logcat裡面能查看到列印的請求

(紅色的顯示的是我在後台請求到數據)

有時候logcat顯示不出數據,可能是消息被過濾了,可以在左邊點擊「減號」刪除過濾

在server端,也就是在myeclipse的建立的另一個後台工程裡面能獲取到請求:


9、後續會補充json數據的解析部分,以及過度到移動雲的部分,上面只是c/s模式下的一個簡單的基於http的請求應答例子。

10. android Volley上傳圖片到伺服器 華為手機上傳出錯

final Map<String, String> params = new HashMap<String, String>();
params.put("send_userId", String.valueOf(id));
params.put("send_email", address);
params.put("send_name", name);
params.put("receive_email", emails);

final Map<String, File> files = new HashMap<String, File>();
files.put("uploadfile", file);

final String request = UploadUtil.post(requestURL, params, files);

熱點內容
qj壓縮機 發布:2024-05-08 08:10:13 瀏覽:840
dhcp伺服器可以分配什麼參數 發布:2024-05-08 08:07:36 瀏覽:958
跨象限編程 發布:2024-05-08 07:58:37 瀏覽:61
java多對一 發布:2024-05-08 07:58:33 瀏覽:641
蘋果怎麼創建文件夾 發布:2024-05-08 07:53:34 瀏覽:916
html連接sql資料庫 發布:2024-05-08 07:53:28 瀏覽:736
網易雲盤無法上傳 發布:2024-05-08 07:48:42 瀏覽:597
python浮點數取整 發布:2024-05-08 07:15:07 瀏覽:137
二手汽車解壓程序 發布:2024-05-08 07:14:13 瀏覽:514
android和php最佳實踐 發布:2024-05-08 07:10:07 瀏覽:852