java.net.ProtocolException:方法不支持请求正文:GET

时间:2019-04-20 19:20:23

标签: java android post okhttp

我正在尝试从android studio发送POST请求,但出现类似以下错误:

  

E / ERROR ::方法不支持请求正文:GET    java.net.ProtocolException:方法不支持请求正文:GET

我不知道如何解决它。谁能帮我吗?

这是我发送端口请求的主要课程

public class Main2Activity extends AppCompatActivity {
    public TextView content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        content = (TextView) findViewById(R.id.content);
        new CheckConnectionStatus().execute("https://nonoperational-trad.000webhostapp.com/getuser.php");
    }

    class CheckConnectionStatus extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            content.setText("");
        }

        protected  String doInBackground(String...params) {
            URL url = null;
            try {
                url = new URL(params[0]);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            try {
                HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
                urlConnection.setDoInput(true);

                Uri.Builder builder = (Uri.Builder) new Uri.Builder()
                        .appendQueryParameter("username", "d")
                        .appendQueryParameter("password","d");

                OutputStream outputStream= urlConnection.getOutputStream();

                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                bufferedWriter.write(builder.build().getEncodedQuery());
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                urlConnection.connect();
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String s = bufferedReader.readLine();
                bufferedReader.close();
                return s;
            } catch (IOException e) {
                Log.e("ERROR:", e.getMessage(), e);
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String s){
            super.onPostExecute(s);
            content.setText(s);
        }
    }
}
我添加的

build.gradle-implementation 'com.squareup.okhttp3:okhttp:3.9.1'

这是发送请求时的错误消息!

E/ERROR:: method does not support a request body: GET
    java.net.ProtocolException: method does not support a request body: GET
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:26)
        at com.example.dato.maptest.Main2Activity$CheckConnectionStatus.doInBackground(Main2Activity.java:65)
        at com.example.dato.maptest.Main2Activity$CheckConnectionStatus.doInBackground(Main2Activity.java:42)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)

1 个答案:

答案 0 :(得分:1)

看起来您尝试调用的API是GET请求。因此,您需要发出GET请求(而不是POST请求)。

即使您提到您正在使用OkHttp进行API调用,我也看不到任何使用它的迹象。我看到您已经使用基本的HttpUrlConnection来发出服务器请求。

通常,我使用Volley进行API调用。您可以从提供的链接中找到如何使用Volley进行API调用。我正在尝试编写一些代码,但是,您可能必须根据需要修改代码。

首先,您需要在build.gradle文件中添加following dependency

dependencies {
    // ... Your other dependencies go here
    implementation 'com.android.volley:volley:1.1.1'
}

然后,您只需要在要调用此API的位置编写以下代码。

String username = "d";
String password = "d";

RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://nonoperational-trad.000webhostapp.com/getuser.php?username=" + username + "&password=" + password;

// Request a String response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Toast.makeText(Main2Activity.this, response, Toast.LENGTH_LONG).show();
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(Main2Activity.this, "Fail", Toast.LENGTH_LONG).show();
    }
});

// Add the request to the RequestQueue.
queue.add(stringRequest);

我尝试使用Postman调用API,并得到以下响应。

enter image description here

希望有帮助!