2011-06-01 135 views
3

我想發送一個簡單的JSON對象到PHP服務器,但是當我嘗試在服務器端檢索該對象時,沒有什麼我的意思是我的$ _POST變量是空的。服務器端是PHP 5.2,我使用的是Android模擬器10 ...有人可以看看我的代碼並告訴我發生了什麼問題嗎? 非常感謝如何將Android應用程序的JSONObject傳遞給PHP文件?

public void uploadJSon() throws ClientProtocolException, IOException, JSONException{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     String url = "http://so-dev-deb.niv2.com/suivi_activite/test.php"; 
     HttpPost httppost = new HttpPost(url); 
     JSONObject json = new JSONObject(); 

     json.put("username", "bob"); 
     json.put("email", "[email protected]"); 
     List nvps = new ArrayList(); 

     nvps.add(new BasicNameValuePair("value", json.toString())); 
     httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));     

     URL test_url = new URL(url); 
     URLConnection connection = test_url.openConnection(); 
     connection.setDoOutput(true); 

     HttpResponse response; 
     response = httpclient.execute(httppost); 
     Log.i("NVPS",nvps.get(0).toString()); 
     Log.i("JSON",json.toString()); 
     Log.i("response", response.getEntity().getContent().toString()); 
     Log.i("response status",response.getStatusLine().toString()); 

     BufferedReader in = new BufferedReader(
      new InputStreamReader(
      connection.getInputStream())); 

     String decodedString; 

     while ((decodedString = in.readLine()) != null) { 
      //System.out.println(decodedString); 
      Log.i("info 10",decodedString); 
     } 
     in.close(); 

    } 

服務器端test.php的是:

<?php 
    $tmp = json_decode($_POST['value']); 
    var_dump($tmp); 
?> 
+0

我不知道爲什麼它沒有正確顯示在我的代碼中,但必須添加此行List nvps = new ArrayList ();而不是實際顯示的內容.... Thx – user683831 2011-06-01 15:39:34

回答

4

我通常採取這種做法在Java代碼生成一個JSON對象:

StringWriter writer = new StringWriter(); 
JSONWriter jsonWriter = new JSONWriter(writer); 
jsonWriter.object(); 
jsonWriter.key("key1").value("test1"); 
jsonWriter.key("key2").value("test2"); 
jsonWriter.endObject(); 
String toSend = writer.toString(); 
+0

Thx,我在你的代碼中使用了你的代碼片斷來生成JSON對象,但是我的服務器端沒有任何東西....任何想法? – user683831 2011-06-01 15:41:08

0

我沒有PHP的專家,但是,我工作的一個項目解碼了我提交的json。

$jsonData = file_get_contents("php://input"); 

如果(isset($ jsonData)& &空($ jsonData)!){$ 這 - >數據= json_decode($ jsonData,TRUE); }

+0

我剛剛試過這個,但沒有在jsonData中... – user683831 2011-06-02 10:28:31

+0

我只能建議你檢查你的客戶端上的json代碼。這是我使用的一個樣本。它不會讓我添加任何代碼,所以我會添加另一個答案 – Bear 2011-06-02 12:01:26

0
public static String login(Context context, String email, String pwd) { 

    HttpClient client = new DefaultHttpClient(); 


    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
    // Limit 
    HttpResponse response; 
    JSONObject json = new JSONObject(); 
    try { 

     Log.d("Debug","Login Url:" + context.getResources().getString(
       R.string.url) + context.getResources().getString(
         R.string.login)); 

     HttpPost post = new HttpPost(context.getResources().getString(
       R.string.url) + context.getResources().getString(
         R.string.login)); 

     post.setHeader("Accept", "application/json"); 
     post.setHeader("Content-type", "application/json"); 

     json.put("email", email); 
     json.put("password", pwd); 

     StringEntity se = new StringEntity("{\"User\":" + json.toString() 
       + "}"); 
     post.setEntity(se); 
     response = client.execute(post); 

     if (response != null) { 
      json = getResult(response); 

      if(statusOK(json)){ 
       return new String(json.getString("token")); 
      } 
     } 
    } catch (Exception e) { 
     Log.e("Error", "Error Login", e); 
    } 
    // printJSON(json); 

    return "ERROR"; 
} 

注意我把JSON包裝在用戶中,因爲服務器需要這個用於我的情況。

相關問題