2017-04-19 3 views
-1

PHP 서버로 발리 라이브러리를 사용하여 json 요청을하려고하는데, 서버가 json 객체를받지 못하는 이유는 무엇입니까? , 빈 문자열로 응답합니다. 여기에 내 코드Android Volley - POST로 비어있는 응답을받을 수없는 POST로 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 (
) 

수신 무엇인가?

답변

1

서버 측 오류가 아닌지 확인하십시오. 예를 들어 우편 배달부를 사용하여 서비스를 시도하십시오.

필자는 개인적으로 같은 문제에 직면 해 있습니다. JsonObjectRequest를 StringRequest로 변경하면 문제가 해결되었습니다.

이 링크에서보세요 : https://stackoverflow.com/a/31613565/7871886

은 이제 옵션이 될 수 대신 발리의 Retrofit2을 ... 사용합니다. 해피 코딩

1

문제가 있었다,하지만 확실하지 미래 Google 직원 :

내 문제는 (작업) I 양식 $_POST 대신

전체 코드 php://input를 읽으려고 한 것이 었습니다 :

자바 :

JSONObject jsonobj; // declared locally so that it destroys after serving its purpose 
jsonobj = new JSONObject(); 
try { 
    // adding some keys 
    jsonobj.put("new key", Math.random()); 
    jsonobj.put("weburl", "hashincludetechnology.com"); 
    // lets add some headers (nested JSON object) 
    JSONObject header = new JSONObject(); 
    header.put("devicemodel", android.os.Build.MODEL); // Device model 
    header.put("deviceVersion", android.os.Build.VERSION.RELEASE); // Device OS version 
    header.put("language", Locale.getDefault().getISO3Language()); // Language 
    jsonobj.put("header", header); 
    // Display the contents of the JSON objects 
    display.setText(jsonobj.toString(2)); 
} catch (JSONException ex) { 
    display.setText("Error Occurred while building JSON"); 
    ex.printStackTrace(); 
} 

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonobj, new Response.Listener<JSONObject>() { 


    @Override 
    public void onResponse(JSONObject response) { 
     System.out.println("onResponse()"); 

     try { 
      result.setText("Response: " + response.toString(2)) 

      System.out.println("Response: " + response.toString(2)); 
     } catch (JSONException e) { 
      display.setText("Error Occurred while building JSON"); 
      e.printStackTrace(); 
     } 
     //to make sure it works backwards as well 

    } 
}, new Response.ErrorListener() { 

    @Override 
    public void onErrorResponse(VolleyError error) { 
     System.out.println("onErrorResponse()"); 
     System.out.println(error.toString()); 


    } 
}); 


System.out.println("After the request is made"); 
// Add the request to the RequestQueue. 
queue.add(jsObjRequest); 

대한 설명 : displayresult은 화면에 데이터를 표시하는 데 사용하는 두 개의 TextView 개체이며 Volley의 요청 대기열은 queue입니다.

PHP :

$inp = json_decode(file_get_contents('php://input')); //$input now contains the jsonobj 
echo json_encode(["foo"=>"bar","input"=>$inp]); //to make sure we received the json and to test the response handling 

귀하의 안드로이드 모니터해야 출력 STH. like :

{ 
    "foo":"bar", 
    "input":{ 
     "new key":0.8523024722406781, 
     "weburl":"hashincludetechnology.com", 
     "header": { 
      "devicemodel":"Android SDK built for x86", 
      "deviceVersion":"7.1", 
      "language":"eng" 
     } 
    } 
}