关闭

xutils请求shh框架发布的json数据(类似from表单)

标签: android框架json
588人阅读 评论(1) 收藏 举报
分类:

在Android上面测试的小demo,通过xutils这个框架,轻松发起post或get,同时又同步,异步等方式

package com.example.xutilsdemo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;


























import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ExecutorService exec;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    //  textView=(TextView) findViewById(R.id.te);
        //getString();

        //  conePostrequest();//xutlis3
        //breenPaotrequest();//xutlis
        breenPaotrequestAnsy();
    }



    /**
     * 异步请求
     */
    void breenPaotrequest(){
        String url="http://10.188.20.128:8080/tj/getUserByUId.action?work=河北大学&UId=1"; 
        String uel="http://10.188.20.128:8080/tj/getUserByUId.action?";
        RequestParams params=new RequestParams();
        params.addBodyParameter("work", "河北");
        params.addBodyParameter("UId", "1");
        HttpUtils http = new HttpUtils();
        http.send(HttpMethod.POST, uel, params, 
                new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }
            @Override
            public void onStart() {
                super.onStart();
                Toast.makeText(getApplicationContext(), "paost开始", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                JSON.parse(arg0.result);
                // List<User>list=JSON.parseArray(arg0.result,User.class);
                textView.setText(arg0.result);
            }
        });
        //
        http.send(HttpMethod.GET,
                url,
                new RequestCallBack<String>(){
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
            }
            @Override
            public void onSuccess(ResponseInfo<String> jsonObject) {

                System.out.println("是数据"+jsonObject.result);
                Toast.makeText(getApplicationContext(), jsonObject.result, Toast.LENGTH_LONG).show();
            }
            @Override
            public void onStart() {
                Toast.makeText(getApplicationContext(), "开始....", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
            }
        });

    }
    /**
     * 同步步请求
     */
ExecutorService executorService=Executors.newFixedThreadPool(2);

void breenPaotrequestAnsy(){
    final String url="http://10.188.20.128:8080/tj/getUserByUId.action?work=河北大学&UId=1"; 
    final String uel="http://10.188.20.128:8080/tj/getUserByUId.action?";
    final HttpUtils http = new HttpUtils();
    executorService.execute(new Runnable() {
        @Override
        public void run() {

            RequestParams params=new RequestParams();
            params.addBodyParameter("work", "河北");
            params.addBodyParameter("UId", "1");
             http.configCurrentHttpCacheExpiry(1000 * 10); //设置超时时间   10s
            try {
             ResponseStream responstream=http.sendSync(HttpMethod.POST,uel,params);
             String json=responstream.readString();
             User user=JSON.parseObject(json, User.class);
            System.out.println(user.getUName());
            JSONObject jsonObject=new JSONObject(json);
            System.out.println("POSTjsonjson->"+jsonObject.getString("UName"));
            } catch (Exception e) {
                e.printStackTrace();
            }           
        //--------------
            try {
                ResponseStream responseStreamget=http.sendSync(HttpMethod.GET, url);
                String json=responseStreamget.readString();
                 User user=JSON.parseObject(json, User.class);
                System.out.println(user.getUName());
                JSONObject jsonObject=new JSONObject(json);
                System.out.println("GETjsonjson->"+jsonObject.getString("UName"));
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   
    });
}
}
3
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    小鼠
    求投食~(点图即可)
    个人资料
    • 访问:115027次
    • 积分:2676
    • 等级:
    • 排名:第13633名
    • 原创:109篇
    • 转载:32篇
    • 译文:0篇
    • 评论:348条
    最新评论
    github