2016-11-18 40 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流中从请求正文中读取原始数据。