【问题标题】:Calling api when button is clicked单击按钮时调用api
【发布时间】:2021-08-06 17:42:50
【问题描述】:

大家好,我正在尝试制作一个应用程序,该应用程序将 api 的结果返回到我现在在我的活动中的文本视图中,我只需单击按钮,onclick 侦听器应该开始工作,但问题是它是此处不返回任何内容是代码:- 包 com.example.youtubehub;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

public class Download_Page extends AppCompatActivity {
    Button download_button;
    EditText editText;
    String full_url, receive_url,title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_page);
        download_button = findViewById(R.id.download_button_1);
        TextView textView = findViewById(R.id.textView4);
        download_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "working", Toast.LENGTH_LONG).show();
                load_page();
            }
        });
//
    }
        public void load_page()  {
            String url = "https://meme-api.herokuapp.com/gimme";
            TextView textView = findViewById(R.id.textView4);

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                    (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                String answer = response.getString("title");
                                textView.setText(answer);
                            } catch (JSONException e) {
                                Toast.makeText(getApplicationContext(), "something went wrong", Toast.LENGTH_LONG).show();
                                Log.d("scuess", "onResponse: ");

                            }
                        }
                    }, new Response.ErrorListener() {



                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // TODO: Handle error

                        }
                    });





    }}

【问题讨论】:

  • 你做了什么调试?您是否收到来自服务器的响应?响应的格式是否符合预期?您是否遇到网络错误?您需要使用调试器并查看发生了什么。
  • 据我所知,调试器没有返回任何内容。

标签: android api android-studio


【解决方案1】:

您需要做的第一件事是检查您是否在您的 Android Manifest 文件中添加了 Internet 权限,如下所示

<uses-permission android:name="android.permission.INTERNET" />

设置完成后,我注意到您正确使用了 volley,但问题是您没有按照 android 文档 here 创建 RequestQueue 并将其传递给 jsonObjectRequest

向您展示它将如何工作,如下所示改进您的 load_page() 方法,它将起作用

 public void load_page() {
    String url = "https://meme-api.herokuapp.com/gimme";
    TextView textView = findViewById(R.id.textView4);//remove this. already defined on onCreate

    RequestQueue requestQueue = Volley.newRequestQueue(this);//create a request Queue and pass the context of the activity

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        String answer = response.getString("title");
                        textView.setText(answer);
                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(), "something went wrong", Toast.LENGTH_LONG).show();
                        Log.d("scuess", "onResponse: ");

                    }
                }
            }, new Response.ErrorListener() {


                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error

                }
            });


    requestQueue.add(jsonObjectRequest); //pass the jsonObjectRequest

}

阅读更多关于documentation

【讨论】:

  • 谢谢伙计,当我使用你的方式时它工作得非常好,但仍然存在问题,我在这个问题中使用的 api 不是我想在我的应用程序中使用的,这只是一个示例一个只是测试它,现在当我尝试使用我原来的一个时,它没有显示任何结果,可能是因为即使在浏览器上也需要 2-3 分钟才能显示结果,所以我不确定如何在 android 中克服这个问题。这是原始 api :- anroidyt.herokuapp.com/url/?url=https://youtu.be/t_xrBpg-1Zs
  • 你好,上面的链接失效了,请重新发一下
  • @Spidy 这个概念在使用 Volley 时仍然适用,这是最好的方法,但是如果您收到缓慢的请求,那么您应该处理您的 API 并尝试对其进行一些优化
  • 没有办法让凌空等待吗?
  • 不幸的是,我不能真正帮助你,因为我不熟悉如何做到这一点。我知道的最好方法是优化 API
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-08
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多