我从API23改为22,因为他们说httpclient不可用。当我切换到API22时,我遇到了HttpClient、HttpPost和NameValuePair的问题。我找到了使用HttpURLConnectionHandler的解决方案。但是我不知道如何将它用于下面的方法。
public void send(View v)
{
HttpClient httpclient = new DefaultHttpClient();
// put the address to your server and receiver file here
HttpPost httppost = new HttpPost("http://yoursite/yourPHPScript.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// we wont be receiving the parameter ID in your server, but it is here to show you how you can send more data
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
// message is the parameter we are receiving, it has the value of 1 which is the value that will be sent from your server to your Arduino board
nameValuePairs.add(new BasicNameValuePair("message", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost); // send the parameter to the server
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
有人好心帮我一把
您可以这样做:
public boolean sendPost(MessageSenderContent content) {
HttpURLConnection connection;
try {
URL gcmAPI = new URL("your_url");
connection = (HttpURLConnection) gcmAPI.openConnection();
connection.setRequestMethod("POST");// type of request
connection.setRequestProperty("Content-Type", "application/json");//some header you want to add
connection.setRequestProperty("Authorization", "key=" + AppConfig.API_KEY);//some header you want to add
connection.setDoOutput(true);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
//content is the object you want to send, use instead of NameValuesPair
mapper.writeValue(dataOutputStream, content);
dataOutputStream.flush();
dataOutputStream.close();
responseCode = connection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
if (responseCode == 200) {
Log.i("Request Status", "This is success response status from server: " + responseCode);
return true;
} else {
Log.i("Request Status", "This is failure response status from server: " + responseCode);
return false;
}
}
例如,对于MessageSenderContent,您可以为“message”和“id”创建自己的示例:
public class MessageSenderContent implements Serializable {
private List<String> registration_ids;
private Map<String, String> data;
public void addRegId(String regId){
if (registration_ids==null){
registration_ids = new LinkedList<>();
registration_ids.add(regId);
}
}
public void createData(String title,String message) {
if (data == null)
data = new HashMap<>();
data.put("title", title);
data.put("message", message);
}
public Map<String, String> getData() {
return data;
}
public void setData(Map<String, String> data) {
this.data = data;
}
public List<String> getRegIds() {
return registration_ids;
}
public void setRegIds(List<String> regIds) {
this.registration_ids = regIds;
}
@Override
public String toString() {
return "MessageSenderContent{" +
"registration_ids=" + registration_ids +
", data=" + data +
'}';
}
更新:
您可以使用HttpUrlConnection后导入此在您的build.gradle文件
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
useLibrary 'org.apache.http.legacy' // this one will let you use HttpUrlConnection
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
...
}
应用程序(Android): 在我运行这个应用程序的手机中,我嗅出了数据,我看到应用程序向服务器发送了一个请求,但数据包不包含json
问题内容: 如何使用HttpURLConnection发布JSON数据?我正在尝试: 我在第14行中收到编译错误。 cURL请求为: 这是处理cURL请求的方法吗?任何信息对我都会非常有帮助。 谢谢。 问题答案: OutputStream希望使用字节,并且您要向其传递字符。试试这个:
我开发了一个应用程序。在我的应用程序中,我从相机或画廊拍摄一张图像。我想使用多部分发布图像到服务器,但图像不发布它。我的帖子数据如下
问题内容: 我正在向本地部署并使用JAVA Spark创建的本地服务发出POST请求。 我想使用进行POST调用时在请求正文中发送一些数据,但是每次JAVA Spark中的请求正文为null时。 下面是我为此使用的代码 Java Spark POST服务处理程序 HTTPClass进行POST调用 问题答案: 您应该仅在将参数写入主体后才调用,而不是之前。您的代码应如下所示:
问题内容: 我对extJS和json有点陌生。使用extJS来发布json数据的最轻松的方法是什么?我对任何GUI功能都不是很感兴趣,只是使用框架发送一些示例数据。 问题答案: