android-我无法使用Volley进行网络通话

android-我无法使用Volley进行网络通话,第1张

概述当我使用jsonObject请求进行网络通话时.我没有收到任何回应.我总是在URL之前收到对request=<[]的HTTP响应.我也尝试过这些链接,但没有任何帮助.AndroidVolley双发时请求缓慢:Source1、source2、Source3packagecom.example.mts3.hammadnewsapp;importandroid.content.C

当我使用JsonObject请求进行网络通话时.我没有收到任何回应.我总是在URL之前收到对request =< []的http响应.我也尝试过这些链接,但没有任何帮助.Android Volley双发时请求缓慢:

Source1、source2、Source 3

 package com.example.mts3.hammadnewsapp;import androID.content.Context;import androID.content.Intent;import androID.content.SharedPreferences;import androID.os.Bundle;import androID.preference.PreferenceManager;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.Toolbar;import androID.util.Log;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.Widget.TextVIEw;import androID.Widget.Toast;import com.androID.volley.DefaultRetryPolicy;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 org.Json.JsONObject;import java.util.ArrayList;public class NewsDetailsActivity extends AppCompatActivity {    RecyclerVIEw recyclerVIEw;    NewsAdapter adapter;    ArrayList<NewsOpen> newsList =new ArrayList<>();    Intent intent;    TextVIEw tv_newsheading;    SharedPreferences sharedPreferences;    SharedPreferences.Editor editor;    Context context;    String DATA_URL="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/API/getnewsFeeds?order=asc";    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_news_details);        Toolbar toolbar =  findVIEwByID(R.ID.toolbar);        setSupportActionbar(toolbar);        newsList = new ArrayList<>();        recyclerVIEw = findVIEwByID(R.ID.rv_newsdetails);        recyclerVIEw.setLayoutManager(new linearlayoutmanager(this));        adapter = new NewsAdapter(this, newsList);        String string = (String) getText(R.string.random_text);        newsList.add(new NewsOpen(R.drawable.hdr_bg_plain, "ChIEf Medical Officer", "10-11-2020", string));//        newsList.add(new NewsOpen("image!!", "ChIEf Medical Officer", "10-11-2020", string));        recyclerVIEw.setAdapter(adapter);        /*backgroundTask.getList(new BackgroundTask.arrayCallBack() {            @OverrIDe            public voID onSuccess(ArrayList<Contact> contacts) {                adapter = new RecyclerAdapter(contacts);                recyclerVIEw.setAdapter(adapter);            }            @OverrIDe            public voID onFail(String msg) {                // Do Stuff            }        });*/    }    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_news_List, menu);        return true;    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroIDManifest.xml.        int ID = item.getItemID();        //noinspection SimplifiableIfStatement        if (ID == R.ID.action_share) {            //Toast.makeText(NewsDetailsActivity.this, "Share menu clicked ", Toast.LENGTH_LONG).show();            callAPI();            return true;        }        if (ID == R.ID.action_logout) {            sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);            editor= sharedPreferences.edit();            editor.putBoolean("VerifIEd user",false);            editor.commit();            Toast.makeText(NewsDetailsActivity.this,"Logged out ",Toast.LENGTH_LONG).show();            Intent intent=new Intent(NewsDetailsActivity.this,MainActivity.class);            startActivity(intent);            return true;        }        return super.onoptionsItemSelected(item);        }    private voID callAPI() {       // new DefaultRetryPolicy(30000, 0, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT);           // int x=3;// retry count           // DefaultRetryPolicy defaultRetryPolicy=new DefaultRetryPolicy(DefaultRetryPolicy.DEFAulT_TIMEOUT_MS*1,x,DefaultRetryPolicy.DEFAulT_BACKOFF_MulT);        //DefaultRetryPolicy defaultRetryPolicy= new DefaultRetryPolicy(20 * 1000, 0, 1.0f);        RequestQueue requestQueue= Volley.newRequestQueue(this);        //System.setProperty("http.keepAlive", "false");        JsonObjectRequest JsonObject =new JsonObjectRequest(Request.Method.GET,DATA_URL, new Response.Listener<JsONObject>() {            @OverrIDe            public voID onResponse(JsONObject response) {                Log.e("res",response.toString());            }        }, new Response.ErrorListener() {            @OverrIDe            public voID one rrorResponse(VolleyError error) {            }        });        //int x=2;// retry count        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAulT_TIMEOUT_MS * 48, x, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAulT_TIMEOUT_MS * 48,0, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0));        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAulT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAulT_MAX_RETRIES, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(0,-1, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));        /*JsonObject.setRetryPolicy(                new DefaultRetryPolicy(                        500000,                        DefaultRetryPolicy.DEFAulT_MAX_RETRIES,                        DefaultRetryPolicy.DEFAulT_BACKOFF_MulT                )        );*/        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAulT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAulT_MAX_RETRIES, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));        //new DefaultRetryPolicy(10000, 0, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT);        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAulT_MAX_RETRIES, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));        //JsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAulT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAulT_MAX_RETRIES, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));        //int socketTimeout = 30000;//30 seconds - change to what you want       // RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAulT_MAX_RETRIES, DefaultRetryPolicy.DEFAulT_BACKOFF_MulT);       // JsonObject.setRetryPolicy(policy);        int custom_timeout_ms = 10000;        DefaultRetryPolicy policy = new DefaultRetryPolicy(custom_timeout_ms,                DefaultRetryPolicy.DEFAulT_MAX_RETRIES,                DefaultRetryPolicy.DEFAulT_BACKOFF_MulT);        JsonObject.setRetryPolicy(policy);        requestQueue.add(JsonObject);    }}

解决方法:

另一种使用StringRequest获得Json响应的解决方案

  private voID callAPI() {    RequestQueue mrequestQueue;    StringRequest stringRequest;    mrequestQueue = Volley.newRequestQueue(this);    stringRequest = new StringRequest(Request.Method.GET, DATA_URL, new Response.Listener<String>() {        @OverrIDe        public voID onResponse(String response) {            try {                JsONObject JsonObject = new JsONObject(response);                System.out.println("Json"+JsonObject);            } catch (JsONException e) {                e.printstacktrace();            }        }    }, new Response.ErrorListener() {        @OverrIDe        public voID one rrorResponse(VolleyError error) {            //Log.i(TAG, "Response: " + error.toString());        }    });    mrequestQueue.add(stringRequest);}

总结

以上是内存溢出为你收集整理的android-我无法使用Volley进行网络通话全部内容,希望文章能够帮你解决android-我无法使用Volley进行网络通话所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存