android接口请求接口返回404,android - Unexpected response code 404 for api.jsonbin POST request - Stack Overf...

林运浩
2023-12-01

I want to POST a new employee into that.

This is what I did:

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="16dp"

tools:context=".MainActivity">

android:id="@+id/text_view_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textColor="@color/colorPrimary"

android:textSize="20sp" />

android:id="@+id/button_parse"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Parse"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.976" />

android:id="@+id/button_post"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Post random data"

app:layout_constraintBottom_toTopOf="@+id/button_parse"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.859" />

MainActivity.java

package com.example.volleydemo;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.VolleyLog;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private TextView textViewResults;

private Button buttonParse;

private Button buttonPostRandomData;

private RequestQueue requestQueue;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initializeViewComponents();

initializeVolleyRequestQueue();

initializeListenersOnViewComponents();

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.button_parse:

jsonParse();

break;

case R.id.button_post:

jsonPostRequest();

break;

}

}

public void initializeViewComponents() {

textViewResults = findViewById(R.id.text_view_result);

buttonParse = findViewById(R.id.button_parse);

buttonPostRandomData = findViewById(R.id.button_post);

}

public void initializeVolleyRequestQueue() {

requestQueue = Volley.newRequestQueue(this);

}

public void initializeListenersOnViewComponents() {

buttonParse.setOnClickListener(this);

buttonPostRandomData.setOnClickListener(this);

}

private void jsonPostRequest() {

RequestQueue requestQueue = Volley.newRequestQueue(this);

String URL = "https://api.jsonbin.io/b/5f3ac924b88c04101cf5d78f";

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener() {

@Override

public void onResponse(String response) {

Log.i("VOLLEY", response);

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

Log.e("VOLLEY", error.toString());

}

}) {

@Override

protected Map getParams() {

Map params = new HashMap<>();

params.put("firstname", "Alberto");

params.put("age", String.valueOf(46));

params.put("mail", "alberto@example.com");

return params;

}

@Override

public Map getHeaders() {

Map params = new HashMap<>();

params.put("Content-Type", "application/x-www-form-urlencoded");

return params;

}

@Override

public String getBodyContentType() {

return "application/json; charset=utf-8";

}

@Override

protected Response parseNetworkResponse(NetworkResponse response) {

String responseString = "";

if (response != null) {

responseString = String.valueOf(response.statusCode);

// can get more details such as response.headers

}

assert response != null;

return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));

}

};

requestQueue.add(stringRequest);

}

}

Note that my jsonParse method works correctly, but when I press my Post Button I get:

E/Volley: [1460] BasicNetwork.performRequest: Unexpected response code 404 for https://api.jsonbin.io/b/5f3ac924b88c04101cf5d78f

How can I fix?

 类似资料: