Search in sources :

Example 36 with RequestQueue

use of com.android.volley.RequestQueue in project aplicativo by InCasa.

the class FragmentSensor method GetLuminosidade.

public void GetLuminosidade(String URLGETLUMINOSISADE) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLGETLUMINOSISADE, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                lumi.setIdLuminosidade(response.getInt("id"));
                lumi.setNome(response.getString("nome"));
                lumi.setDescricao(response.getString("descricao"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), "Configuração Luminosidade: Erro!", Toast.LENGTH_SHORT).show();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            // add headers <key,value>
            User user = User.getInstancia();
            String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
            headers.put("Authorization ", " Basic " + auth);
            return headers;
        }
    };
    // Add the request to the RequestQueue.
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(getActivity().getApplicationContext());
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : VolleyError(com.android.volley.VolleyError) User(model.User) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 37 with RequestQueue

use of com.android.volley.RequestQueue in project aplicativo by InCasa.

the class FragmentServidor method testeServer.

public void testeServer(String ipServer) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, "http://" + ipServer + "/backend/teste", null, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            Toast.makeText(getActivity(), "Servidor: OK", Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), "Erro no servidor", Toast.LENGTH_SHORT).show();
        }
    });
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(getActivity().getApplicationContext());
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 38 with RequestQueue

use of com.android.volley.RequestQueue in project V2HOT by djyde.

the class ContentActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
    }
    setContentView(R.layout.activity_content);
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
    ImageLoader.getInstance().init(config);
    Intent intent = getIntent();
    String id = intent.getStringExtra("id");
    String content = intent.getStringExtra("content");
    String username = intent.getStringExtra("username");
    title = intent.getStringExtra("title");
    url = intent.getStringExtra("url");
    final RepliesAdapter repliesAdapter = new RepliesAdapter(this, username);
    ListView repliesView = (ListView) findViewById(R.id.replies);
    View headerView = getLayoutInflater().inflate(R.layout.topic_header, repliesView, false);
    ((TextView) headerView.findViewById(R.id.header_title)).setText(title);
    ((TextView) headerView.findViewById(R.id.header_content)).setText(content);
    ((TextView) headerView.findViewById(R.id.header_username)).setText(username);
    repliesView.addHeaderView(headerView);
    repliesView.setAdapter(repliesAdapter);
    RequestQueue queue = Volley.newRequestQueue(ContentActivity.this);
    // 获取回复
    queue.add(new GsonRequest<ReplyList>(Request.Method.GET, "https://www.v2ex.com/api/replies/show.json?topic_id=" + id, ReplyList.class, new Response.Listener<ReplyList>() {

        @Override
        public void onResponse(ReplyList response) {
            repliesAdapter.addAll(response);
            repliesAdapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(ContentActivity.this, "请检查网络", Toast.LENGTH_LONG).show();
        }
    }));
}
Also used : VolleyError(com.android.volley.VolleyError) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) ListView(android.widget.ListView) RequestQueue(com.android.volley.RequestQueue) TextView(android.widget.TextView) ReplyList(com.v2ex.api.ReplyList) ImageLoaderConfiguration(com.nostra13.universalimageloader.core.ImageLoaderConfiguration) ActionBar(android.support.v7.app.ActionBar)

Example 39 with RequestQueue

use of com.android.volley.RequestQueue in project V2HOT by djyde.

the class TitleListActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_title_list);
    final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.pull);
    final ArrayAdapter<Topic> topicsAdapter = new ArrayAdapter<Topic>(this, 0) {

        private final LayoutInflater inflater = LayoutInflater.from(getContext());

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.topic_item, parent, false);
            }
            TextView title = (TextView) convertView.findViewById(R.id.title);
            title.setText(getItem(position).title);
            return convertView;
        }
    };
    final ListView topicsView = (ListView) findViewById(R.id.topics);
    topicsView.setAdapter(topicsAdapter);
    topicsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Topic topic = topicsAdapter.getItem(position);
            Intent intent = new Intent(TitleListActivity.this, ContentActivity.class);
            intent.putExtra("id", topic.id);
            intent.putExtra("title", topic.title);
            intent.putExtra("username", topic.member.username);
            intent.putExtra("url", topic.url);
            intent.putExtra("content", topic.content);
            startActivity(intent);
        }
    });
    // 创建volley请求队列
    final RequestQueue queue = Volley.newRequestQueue(this);
    // 根据API获取热议主题
    final GsonRequest<TopicList> request = new GsonRequest<TopicList>(Request.Method.GET, "https://www.v2ex.com/api/topics/hot.json", TopicList.class, new Response.Listener<TopicList>() {

        @Override
        public void onResponse(TopicList response) {
            topicsAdapter.clear();
            topicsAdapter.addAll(response);
            topicsAdapter.notifyDataSetChanged();
            swipeRefreshLayout.setRefreshing(false);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(TitleListActivity.this, "请检查网络", Toast.LENGTH_LONG).show();
        }
    });
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            queue.add(request);
        }
    });
    queue.add(request);
    swipeRefreshLayout.setRefreshing(true);
}
Also used : GsonRequest(com.v2ex.api.GsonRequest) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) ListView(android.widget.ListView) RequestQueue(com.android.volley.RequestQueue) TopicList(com.v2ex.api.TopicList) TextView(android.widget.TextView) Topic(com.v2ex.api.Topic) VolleyError(com.android.volley.VolleyError) ViewGroup(android.view.ViewGroup) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) Response(com.android.volley.Response) LayoutInflater(android.view.LayoutInflater) AdapterView(android.widget.AdapterView) ArrayAdapter(android.widget.ArrayAdapter)

Example 40 with RequestQueue

use of com.android.volley.RequestQueue in project aplicativo by InCasa.

the class AdminActivity method getUserInfo.

public void getUserInfo(JSONObject json, String URLUSERINFO) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLUSERINFO, json, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            Context context = getApplicationContext();
            CharSequence text = "Usuário: Sucesso !";
            int duration = Toast.LENGTH_SHORT;
            User user = User.getInstancia();
            try {
                user.setNome(response.getString("nome"));
                user.setId(response.getString("id"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
            Context context = getApplicationContext();
            CharSequence text = "Usuário: Falha !";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            // add headers <key,value>
            User user = User.getInstancia();
            String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
            headers.put("Authorization ", " Basic " + auth);
            return headers;
        }
    };
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(this);
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : Context(android.content.Context) VolleyError(com.android.volley.VolleyError) User(model.User) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) Toast(android.widget.Toast) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Aggregations

RequestQueue (com.android.volley.RequestQueue)72 Response (com.android.volley.Response)59 VolleyError (com.android.volley.VolleyError)58 JSONObject (org.json.JSONObject)57 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)54 HashMap (java.util.HashMap)51 User (model.User)50 JSONException (org.json.JSONException)40 Context (android.content.Context)18 Toast (android.widget.Toast)18 TextView (android.widget.TextView)11 File (java.io.File)9 Intent (android.content.Intent)7 Network (com.android.volley.Network)7 PackageInfo (android.content.pm.PackageInfo)6 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)6 Switch (android.widget.Switch)4 DiskBasedCache (com.android.volley.toolbox.DiskBasedCache)4 View (android.view.View)3 HurlStack (com.android.volley.toolbox.HurlStack)3