2017-04-19 19 views
-1

私は、Volleyライブラリを使って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

サーバー側のエラーではないことを確認してください(例:Postmanを使用してサービスを試してください)。

私は以前個人的に同じ問題を抱えていましたが、JsonObjectRequestをStringRequestに変更すると問題が解決しました。

は、このリンクを見てみましょう: https://stackoverflow.com/a/31613565/7871886

今、私の代わりにバレーのRetrofit2を使用して...オプションのだろう。 ハッピーコーディング

1

あなたの問題だったが、何わからない将来のGooglerのために:

私の問題は、(作業)私は、フォーム$_POST代わりの

完全なコードphp://inputを読みしようとしていたということでした。

のJava:

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は、画面上にデータを表示するために使用する2つのTextViewオブジェクトであり、queueはVolleyのリクエストキューです。

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 

あなたのAndroid監視しなければならない出力かないました。 like:

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