英文:
Getting "Could not execute method for android:onClick" exception while making a GET request using OKHttp
问题
我正在尝试在按钮点击时使用OKHttp创建GET请求。在按钮点击时,会发出一个请求到特定的URL,以获取一些JSON数据。以下是代码片段:
public static String sendGet(final String url, final String token) throws IOException, JSONException {
    Request request = new Request.Builder()
            .url(url)
            //这将token添加到头部。
            .addHeader("Authorization", "Bearer " + token)
            .addHeader("Content-Type","application/json")
            .build();
    try{
        Response response = client.newCall(request).execute();
        System.out.println(response);
        if (!response.isSuccessful()){
            throw new IOException("Unexpected Code " + response);
        }
        System.out.println(response.body().string());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
以下是异常信息:
FATAL EXCEPTION: main
Process: com.collins.ped.bw, PID: 15532
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6308)
    at android.widget.TextView.performClick(TextView.java:11202)
    at android.view.View$PerformClick.run(View.java:23969)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
希望这些信息有所帮助。如果您需要进一步的解释或帮助,请随时告诉我。
英文:
I am trying to create a GET request using OKHttp on a button click. On the button click, a request is made to a particular url that fetches some JSON data. Following is the code snippet:
public static String sendGet(final String url, final String token) throws IOException, JSONException {
            Request request = new Request.Builder()
                    .url(url)
                    //This adds the token to the header.
                    .addHeader("Authorization", "Bearer " + token)
                    .addHeader("Content-Type","application/json")
                    .build();
            try{
                Response response = client.newCall(request).execute();
                System.out.println(response);
                if (!response.isSuccessful()){
                    throw new IOException("Unexpected Code " + response);
                }
                System.out.println(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
}
Following is the exception:
FATAL EXCEPTION: main
    Process: com.collins.ped.bw, PID: 15532
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:6308)
        at android.widget.TextView.performClick(TextView.java:11202)
        at android.view.View$PerformClick.run(View.java:23969)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
专注分享java语言的经验与见解,让所有开发者获益!

评论