2011-03-07 14 views
3

我正在開發一個學校項目,該項目需要JAVA向PHP Web服務前端發送一系列條形碼。我查看了幾條帖子herehere,並從他們那裏拿到了一些東西,但我的代碼似乎不起作用。在Java中通過Httppost發送JSON時出現問題,請幫助

因此,這裏是我的Java代碼:

import org.json.simple.JSONObject; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicHeader; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.HttpResponse; 
import java.io.InputStream; 


public class jsontest2 { 
    public static void main (String Args[]) { 
     JSONObject obj=new JSONObject(); 
     JSONObject codes=new JSONObject(); 
     //Forms the name value pairs for the barcodes and quantity 
     codes.put("598013", new Integer(10)); 
     codes.put("5849632927",new Integer(15)); 
     codes.put ("5849676037",new Integer(20)); 
     codes.put ("6634391931", new Integer(25)); 

     //Headers in the JSON array 
     obj.put("LoginID", new Integer(1234)); 
     obj.put("Machine_ID", new Integer(123456789)); 
     obj.put("add", codes); 
     System.out.print(obj); 

     //httppost 
     try { 
      HttpClient httpclient= new DefaultHttpClient(); 
      HttpResponse response; 
      HttpPost httppost= new HttpPost ("http://example/receive.php"); 
      StringEntity se=new StringEntity ("myjson: "+obj.toString()); 
      httppost.setEntity(se); 
      System.out.print(se); 
      httppost.setHeader("Accept", "application/json"); 
      httppost.setHeader("Content-type", "application/json"); 

      response=httpclient.execute(httppost); 

     } 
     catch (Exception e) { 
      e.printStackTrace(); 
      System.out.print("Cannot establish connection!"); 
     } 
    } 
} 

,並測試httppost要求,我做了這個PHP文件記錄每個請求。

<?php 
$input = stripslashes($_POST["myjson"]); 
logToFile("post.txt", $input); 

function logToFile($filename, $msg) 
{ 
    $fd = fopen($filename, "a"); 
    $str ="[".date("Y/m/d h:i:s")."]".$msg; 
    fwrite($fd, $str."\n"); 
    fclose($fd); 
} 

的問題是,在日誌文件log.txt中,我每次運行Java程序時,它只會增加,包括時間和日期的新生產線。對我來說,這意味着PHP正在接受有一個httppost請求,但我的json字符串在哪裏?

誰能告訴我我做錯了什麼?我在這裏呆了一段時間。謝謝!

+0

什麼是'se'的內容來了解​​JSON對象(之前發佈)和'$ _POST'('的var_dump($ _ POST)')? – 2011-03-07 04:40:11

+0

[email protected]和var_dump($ _ POST)= null。 – 2011-03-07 04:52:04

+0

非常類似於我的情況... – gumuruh 2012-04-18 09:25:53

回答

3

它看起來像你正在做一個原始的HTTP帖子,所以PHP代碼需要考慮到這一點。對於PHP代碼試試這個:

<?php 
$input = file_get_contents('php://input'); 
logToFile("post.txt",$input); 

function logToFile($filename,$msg) 
{ 
     $fd=fopen($filename,"a"); 
     $str="[".date("Y/m/d h:i:s")."]".$msg; 
     fwrite($fd,$str."\n"); 
     fclose($fd); 
} 
?> 

看到這個答案,並獲取更多信息的各個環節:

php curl post json

+0

canuckistani,非常感謝您的幫助,它的工作!你可能會簡單地解釋爲什麼我應該使用file_get_contents而不是$ _POST?再次感謝! – 2011-03-07 04:51:47

+2

這是所有在這個手冊頁:http://se2.php.net/wrappers.php。基本上,如果發佈數據不是以name = value對編碼的,PHP不會將它解析到$ _POST數組中。在這種情況下,您需要使用輸入流訪問它。 – canuckistani 2011-03-07 05:01:06

+0

你我也得到了你們所有人提到的相同結果。 我也好奇。儘管我已經把它發佈爲json。無論如何@canuckistani:非常感謝! – gumuruh 2012-04-18 09:28:49

0

在PHP模塊

  $username="demo"; 
    $action = 'error'; 
    $json =array('action'=> $action, 'username' => $username); 
    echo json_encode($json); 

在java中

String regstatus =「」;

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));  

    String line; 
    while ((line = rd.readLine()) != null) { 

     regstatus =line; 
     String json="["+regstatus+"]" ; 
     JsonArray jArray = new JsonParser().parse(json).getAsJsonArray(); 
     for (int i=0;i<jArray.size();i++) { 
      JsonObject jsonObject = jArray.get(i).getAsJsonObject(); 
      System.out.println(jsonObject.get("username")); 
      System.out.println(jsonObject.get("action")); 
      System.out.println("*********"); 
     } 


    } 

'[' 需要在Java