Volley 라이브러리를 사용하여 PHP 서버에 json 요청을 시도하고 있지만 어떤 이유로 서버가 내가 보내는 json 객체를 수신하지 못하고 빈 문자열로 응답합니다. 내 코드는 다음과 같습니다.
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MyVolley implements Listener, ErrorListener {
private static Context appContext;
public MyVolley(Context context) {
appContext = context;
}
public void stuff() throws JSONException {
RequestQueue queue = Volley.newRequestQueue(appContext);
JSONObject obj = new JSONObject();
obj.put("param1", "assda");
obj.put("param2", "fassfafsa");
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, "some url here", obj, this, this);
queue.add(stringRequest);
}
@Override
public void onResponse(Object response) {
System.out.println(response);
}
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
}
그리고 그것이 실행될 때, 이것은 서버가받는 것입니다
array (
'Content-Type' => 'application/json; charset=utf-8',
'User-Agent' => 'pointless info here',
'Host' => 'some host here',
'Connection' => 'Keep-Alive',
'Accept-Encoding' => 'gzip',
'Content-Length' => '107',
) array (
) array (
)
왜 그런 일이 일어날 수 있습니까?
서버 측 오류가 아닌지 확인하십시오 (예를 들어 Postman을 사용하여 서비스를 시도하십시오).
나는 개인적으로 얼마 전에 같은 문제에 직면하여 JsonObjectRequest를 StringRequest로 변경하여 문제를 해결했습니다.
이 링크를보십시오 : https://stackoverflow.com/a/31613565/7871886
지금은 Volley 대신 Retrofit2를 사용합니다. 옵션이 될 수 있습니다. 즐거운 코딩
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다