Reputation: 39
I'm trying to send POST request from the android studio and I get some errors like:
E/ERROR:: method does not support a request body: GET java.net.ProtocolException: method does not support a request body: GET
And I don't know how to resolve it. Can anyone help me?
This is my main class where I'm sending port request
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
that I added - implementation 'com.squareup.okhttp3:okhttp:3.9.1'
This is the error message while sending the request!
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)
Upvotes: 1
Views: 1277
Reputation: 24211
Looks like the API that you are trying to call is a GET request. Hence you need to make a GET request (not a POST request).
Even though you have mentioned that you are using OkHttp for the API call, I do not see any sign of using it. I see that you have used basic HttpUrlConnection
for making the server request.
Usually, I use Volley for making API calls. You can find how to use Volley for making an API call from the link provided. Here's I am trying to write some code, however, you may have to modify the code as per your need.
First, you need to add the following dependency in your build.gradle
file.
dependencies {
// ... Your other dependencies go here
implementation 'com.android.volley:volley:1.1.1'
}
Then you just have to write the following code where you want to call this 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);
I tried calling the API using Postman and got the following response.
Hope that helps!
Upvotes: 1