Comments (14)
上傳Json數據到Server端的時候,不論什麼請求方法,都是通過Stream寫Request Body到Server端。所以我們提供了幾個request.setDefineBody...的方法,多個重載放肯定可以滿足你。不過需要POST、PUT、DELETE請求方法才行。
from nohttp.
requestQueue = NoHttp.newRequestQueue();
Request<JSONArray> request = NoHttp.createJsonArrayRequest("http://192.168.1.40/test/call_android.php", RequestMethod.POST);
JSONObject object = new JSONObject();
try {
object.put("member_account","test");
object.put("member_password","test");
object.put("member_email","[email protected]");
} catch (JSONException e) {
e.printStackTrace();
}
request.setDefineRequestBodyForJson(object);// 開發者大大,是這樣嗎?
request.addHeader("Author", "nohttp_sample");
// request.setTag(object);
requestQueue.add(NOHTTP_WHAT_TEST, request, onResponseListener);
from nohttp.
是的,還有好幾個重載的方法,你可以再敲代碼提示看看。
from nohttp.
08-21 16:45:19.841 9788-9835/com.example.ren.httpsample I/NoHttpSample: -------Send request data start-------
08-21 16:45:19.841 9788-9835/com.example.ren.httpsample I/NoHttpSample: -------Send request data end-------
JSONObject object = new JSONObject();
try {
object.put("member_account","test");
object.put("member_password","test");
array.put(object);
} catch (JSONException e) {
e.printStackTrace();
}
// 添加请求参数
request.setDefineRequestBodyForJson(object);
但是他好像沒有把資料塞入進去?
from nohttp.
我可以冒昧问下你是哪里人吗?
你可以用Fiddler或者Charles抓包看看,数据是被传输到服务器了,这里的requestBody是Stream,无法打印出来,所以看到Log中没有,如果你有更说服力的证据可以传上来我帮你判断下。
from nohttp.
開發者大大您好,小弟來自台灣,第一次使用nohttp,所以算是新手,真不好意思。
public class MainActivity extends AppCompatActivity {
ListView content = null;
ListAdapter adapter = null;
/**
* 用来标志请求的what, 类似handler的what一样,这里用来区分请求
*/
private static final int NOHTTP_WHAT_TEST = 0x001;
/**
* 请求队列
*/
private RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = (ListView)findViewById(R.id.content);
// 创建请求队列, 默认并发3个请求, 传入数字改变并发数量: NoHttp.newRequestQueue(1);
requestQueue = NoHttp.newRequestQueue();
// 创建请求对象
Request<JSONArray> request = NoHttp.createJsonArrayRequest("http://192.168.1.40/test/call_android.php", RequestMethod.POST);
JSONArray array = new JSONArray();
JSONObject object = new JSONObject();
try {
object.put("member_account","test");
object.put("member_password","test");
array.put(object);
} catch (JSONException e) {
e.printStackTrace();
}
// 添加请求参数
request.setDefineRequestBodyForJson(object);
// 添加请求头
request.addHeader("Author", "nohttp_sample");
// 设置一个tag, 在请求完(失败/成功)时原封不动返回; 多数情况下不需要
// request.setTag(object);
/*
* what: 当多个请求同时使用同一个OnResponseListener时用来区分请求, 类似handler的what一样
* request: 请求对象
* onResponseListener 回调对象,接受请求结果
*/
requestQueue.add(NOHTTP_WHAT_TEST, request, onResponseListener);
}
/**
* 回调对象,接受请求结果
*/
private OnResponseListener<JSONArray> onResponseListener = new OnResponseListener<JSONArray>() {
@SuppressWarnings("unused")
@Override
public void onSucceed(int what, Response<JSONArray> response) {
if (what == NOHTTP_WHAT_TEST) {// 判断what是否是刚才指定的请求
// 请求成功
JSONArray result = response.get();// 响应结果
// 响应头
Headers headers = response.getHeaders();
headers.getResponseCode();// 响应码
response.getNetworkMillis();// 请求花费的时间
Log.e("aaaaaa", String.valueOf(result));
adapter = new ListAdapter(MainActivity.this,result);
content.setAdapter(adapter);
}
}
@Override
public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
}
@Override
public void onStart(int what) {
// 请求开始,显示dialog
}
@Override
public void onFinish(int what) {
// 请求结束,关闭dialog
}
};
@Override
protected void onDestroy() {
super.onDestroy();
requestQueue.cancelAll();// 退出APP时停止所有请求
requestQueue.stop();// 退出APP时停止队列
}
}
public class ListAdapter extends BaseAdapter {
JSONArray data = null;
LayoutInflater inflater = null;
Context context = null;
public ListAdapter(Context _c,JSONArray _data)
{
data = _data;
context = _c;
inflater = LayoutInflater.from(_c);
}
@Override
public int getCount()
{
if(data.length()!=0)
{
return data.length();
}
else
{
return 0;
}
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = inflater.inflate(R.layout.list_item,null);
}
TextView content = (TextView)convertView.findViewById(R.id.content);
try
{
JSONObject item = data.getJSONObject(position);
content.setText(item.getString("account")+"\n"+item.getString("email")+"\n"+item.getString("phone"));
}
catch (JSONException e)
{
e.printStackTrace();
}
return convertView;
}
}
from nohttp.
你好,原來是來自台灣的同胞。你有任何即時通訊帳號嗎?例如QQ或者微信?我QQ:757699476。
我現在不太明白你哪裡遇到了問題。希望能快速溝通。
from nohttp.
開發者大大您好,抱歉...我沒用過QQ..,沒關係,我再自行研究看看,如果找到解決方法我再張貼在上面,謝謝您。
from nohttp.
好的,有問題隨時來溝通,我的郵箱是[email protected]。對於你上面的代碼我有一點建議給你。
請求成功後拿到的是JsonObject,這裡應該把數據解析成你想要的實體再傳到適配器。最好還要做一下服務器響應碼判斷。
from nohttp.
JSONObject object = new JSONObject();
try
{
object.put("member_account","test");
object.put("member_password","test");
array.put(object);
}
catch (JSONException e)
{
e.printStackTrace();
}
request.add("jsonObject",String.valueOf(object));
Log.e("sss", String.valueOf(object));
開發者大大您好,我想到一個解決辦法,就是把 jsonobject 塞入自己想要的資料,之後轉成String
然後...
`Log.e("sss", String.valueOf(object));這行
我在Logcat看,他是顯示正確的jsonobject
E/sss: {"member_account":"test","member_password":"test"}
可是在傳資料的時候,他好像會亂碼?
I/NoHttpSample: -------Send request data start-------
I/NoHttpSample: Push RequestBody: jsonObject=%7B%22member_account%22%3A%22test%22%2C%22member_password%22%3A%22test%22%7D
I/NoHttpSample: -------Send request data end-------
D/NoHttpSample: -------Response start-------
`
from nohttp.
因為json包含了大括號{},大括號是特殊符號,get和post的時候都會做URLEncode。所以看起來是亂碼,但是服務器接受到會自動URLDcode的,這樣子就不會是亂碼了。所以這是正常的。
from nohttp.
瞭解了,謝謝開發者大大,成功使用JsonObject上傳到後台資料庫了,參考代碼如下:
requestQueue = NoHttp.newRequestQueue();
Request<JSONArray> request = NoHttp.createJsonArrayRequest("http://192.168.1.40/test/call_android.php", RequestMethod.POST);
JSONObject object = new JSONObject();
try
{
object.put("member_account","測試");
object.put("member_password","test");
} catch (JSONException e) {
e.printStackTrace();
}
request.setDefineRequestBodyForJson(object);
request.addHeader("Author", "nohttp_sample");
requestQueue.add(NOHTTP_WHAT_TEST, request, onResponseListener);
from nohttp.
嗯,沒錯的。感謝你的回饋。這裡有一點需要跟你說明一下,首先請求隊列RequestQueue不需要每次請求都需要new一個出來,這裡你可以把它封裝成單例模式使用,這樣才能發揮隊列的優勢。
from nohttp.
好的,謝謝開發者大大的提醒,謝謝您不厭其煩的解決了我的問題。
感恩!!
from nohttp.
Related Issues (20)
- RequestQueue多并发实现问题
- 请求头部的添加,自动把键值的首字母改成大写 HOT 1
- 上传进度条问题new FileInputStream(file), "application/octet-stream" 如何设置上传进度条 HOT 1
- 上传请求的数据中包含list list里面是map数据,请问如何上传 HOT 5
- 你好问下表单提交file能拿到上传进度的回调吗
- 报错 v1.1.11 HOT 1
- 使用studio3.2版本在安卓9.0机型请求失败
- 多文件上传 bug HOT 1
- Ftp下载 HOT 1
- 不回调success,Debug发现报 java.util.concurrent.CancellationException
- 设置返回编码无效
- NoHttpSample: java.net.SocketException: socket failed: ENONET (Machine is not on the network)
- 支持https不
- 同步请求数据返回为 “”, 同样的url 异步请求没问题
- post请求中文乱码
- The network is not available, please check the network
- target 29 下载报权限异常 HOT 1
- NoHttpSample: okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR HOT 1
- 为什么下载后文件正常但是重启设备后文件的后缀多了.nohttp 导致文件异常
- 请求头部的添加,自动把键值的首字母改成大写 ,这个有方法避免吗
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nohttp.