Code Monkey home page Code Monkey logo

Comments (14)

yanzhenjie avatar yanzhenjie commented on July 22, 2024

上傳Json數據到Server端的時候,不論什麼請求方法,都是通過Stream寫Request Body到Server端。所以我們提供了幾個request.setDefineBody...的方法,多個重載放肯定可以滿足你。不過需要POST、PUT、DELETE請求方法才行。

from nohttp.

a3858293 avatar a3858293 commented on July 22, 2024
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.

yanzhenjie avatar yanzhenjie commented on July 22, 2024

是的,還有好幾個重載的方法,你可以再敲代碼提示看看。

from nohttp.

a3858293 avatar a3858293 commented on July 22, 2024
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.

yanzhenjie avatar yanzhenjie commented on July 22, 2024

我可以冒昧问下你是哪里人吗?
你可以用Fiddler或者Charles抓包看看,数据是被传输到服务器了,这里的requestBody是Stream,无法打印出来,所以看到Log中没有,如果你有更说服力的证据可以传上来我帮你判断下。

from nohttp.

a3858293 avatar a3858293 commented on July 22, 2024

開發者大大您好,小弟來自台灣,第一次使用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.

yanzhenjie avatar yanzhenjie commented on July 22, 2024

你好,原來是來自台灣的同胞。你有任何即時通訊帳號嗎?例如QQ或者微信?我QQ:757699476。
我現在不太明白你哪裡遇到了問題。希望能快速溝通。

from nohttp.

a3858293 avatar a3858293 commented on July 22, 2024

開發者大大您好,抱歉...我沒用過QQ..,沒關係,我再自行研究看看,如果找到解決方法我再張貼在上面,謝謝您。

from nohttp.

yanzhenjie avatar yanzhenjie commented on July 22, 2024

好的,有問題隨時來溝通,我的郵箱是[email protected]。對於你上面的代碼我有一點建議給你。
請求成功後拿到的是JsonObject,這裡應該把數據解析成你想要的實體再傳到適配器。最好還要做一下服務器響應碼判斷。

from nohttp.

a3858293 avatar a3858293 commented on July 22, 2024
        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-------
`

image

sss

from nohttp.

yanzhenjie avatar yanzhenjie commented on July 22, 2024

因為json包含了大括號{},大括號是特殊符號,get和post的時候都會做URLEncode。所以看起來是亂碼,但是服務器接受到會自動URLDcode的,這樣子就不會是亂碼了。所以這是正常的。

from nohttp.

a3858293 avatar a3858293 commented on July 22, 2024

瞭解了,謝謝開發者大大,成功使用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.

yanzhenjie avatar yanzhenjie commented on July 22, 2024

嗯,沒錯的。感謝你的回饋。這裡有一點需要跟你說明一下,首先請求隊列RequestQueue不需要每次請求都需要new一個出來,這裡你可以把它封裝成單例模式使用,這樣才能發揮隊列的優勢。

from nohttp.

a3858293 avatar a3858293 commented on July 22, 2024

好的,謝謝開發者大大的提醒,謝謝您不厭其煩的解決了我的問題。

感恩!!

from nohttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.