2016-11-18 42 views
0

我無法使用php post獲取值。我的代碼是:使用Java HttpURLConnection和php

JSONObject obj = new JSONObject(); 
obj.put("id", requestModel.Id); 
obj.put("name", !requestModel.Name.equals("") ? requestModel.Name : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
obj.put(ManufacturesTableAdapter.KEY_MANUFACTURES_ADDRESS, !requestModel.Address.equals("") ? requestModel.Address : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
obj.put("city", !requestModel.City.equals("") ? requestModel.City : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
obj.put("state", !requestModel.State.equals("") ? requestModel.State : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
obj.put("zip", !requestModel.Zip.equals("") ? requestModel.Zip : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
obj.put(ManufacturesTableAdapter.KEY_MANUFACTURES_PHONE, !requestModel.Phone.equals("") ? requestModel.Phone : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
obj.put(ManufacturesTableAdapter.KEY_MANUFACTURES_EMAIL, !requestModel.Email.equals("") ? requestModel.Email : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
switch (apiName) { 
    case JsonWriteContext.STATUS_OK_AFTER_COLON /*2*/: 
     obj.put("questions", !requestModel.Questions.equals("") ? requestModel.Questions : MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR); 
     break; 
} 
return obj.toString(); 

我節省$ _ SERVER,$ _GET,$ _ POST細節,但我不能讓像GET,POST和服務器數據進行任何

InputStream is; 
URL url = new URL(targetURL); 
String request = "request=" + parameter; 
Log.d("Light", "Url data:" + request); 
connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("POST"); 
connection.setUseCaches(false); 
connection.setDoInput(true); 
connection.setDoOutput(true); 
DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
wr.writeBytes(request); 
wr.flush(); 
wr.close(); 

參數賦值。

回答

1

PHP填充$_POST陣列當請求使用某些Content-Type報頭(諸如application/x-www-form-urlencodedmultipart/form-data或),並請求體是urlencoded進行查詢字符串(examples)。

此外,您還可以從php://input流中從請求正文中讀取原始數據。