通过Post将JSON从Java发送到PHP
我正在做一个Android程序,该程序将数据从平板电脑发送到PHP Web服务。用于发送JSON的代码:通过Post将JSON从Java发送到PHP,java,php,android,Java,Php,Android,我正在做一个Android程序,该程序将数据从平板电脑发送到PHP Web服务。用于发送JSON的代码: package com.example.shvalidation; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpResponse; imp
package com.example.shvalidation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainMenuScreen extends Activity {
//JSON Variables
JSONParser jsonParser = new JSONParser();
String pid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu_layout);
new TestThread().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu_layout, menu);
return true;
}
public void PlantToDome(View view) {
Intent intent = new Intent(this, SelectLocationScreen.class);
startActivity(intent);
}
//Código del Web Service
public class TestThread extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(MainMenuScreen.this, "Loading", "Loading data, please wait..");
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
protected Void doInBackground(Void...args0) {
try {
HttpClient client = new DefaultHttpClient();
HttpResponse response;
HttpPost post = new HttpPost("http://192.168.1.101:8080/GetBook.php");
JSONObject holder = new JSONObject();
JSONObject euid = new JSONObject();
euid.put("euid", 1);
holder.accumulate("euids", euid);
euid.put("euid", 2);
holder.accumulate("euids", euid);
post.setHeader("json", holder.toString());
StringEntity se = new StringEntity(holder.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
if (response != null) {
InputStream in = response.getEntity().getContent();
String a = convertStreamToString(in);
Log.i("Read from Server", a);
}
} catch (Exception e) {
Log.d("error", e.toString());
}
return null;
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
}
package com.example.shvalidation;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicHeader;
导入org.apache.http.protocol.http;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
公共类MainMenuScreen扩展活动{
//JSON变量
JSONParser JSONParser=新的JSONParser();
串pid;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_菜单_布局);
新建TestThread().execute();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单.主菜单布局,菜单);
返回true;
}
公共无效PlantToDome(视图){
意向意向=新意向(选择LocationScreen.class);
星触觉(意向);
}
//Código del Web服务
公共类TestThread扩展异步任务{
进程对话;
受保护的void onPreExecute(){
dialog=ProgressDialog.show(MainMenuScreen.this,“正在加载”,“正在加载数据,请稍候”);
}
私有字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}
受保护的空位背景(空位…args0){
试一试{
HttpClient=new DefaultHttpClient();
HttpResponse响应;
HttpPost=新的HttpPost(“http://192.168.1.101:8080/GetBook.php");
JSONObject holder=新的JSONObject();
JSONObject euid=新的JSONObject();
euid.put(“euid”,1);
持有人。累积(“euid”,euid);
euid.put(“euid”,2);
持有人。累积(“euid”,euid);
setHeader(“json”,holder.toString());
StringEntity se=新的StringEntity(holder.toString());
setContentEncoding(新的BasicHeader(HTTP.CONTENT_TYPE,“application/json”);
邮政实体(se);
响应=client.execute(post);
if(响应!=null){
InputStream in=response.getEntity().getContent();
字符串a=convertStreamToString(in);
Log.i(“从服务器读取”,a);
}
}捕获(例外e){
Log.d(“错误”,例如toString());
}
返回null;
}
受保护的void onPostExecute(未使用的void){
dialog.dismise();
}
}
}
PHP Web服务:
<?php
ob_start();
var_dump(json_decode(file_get_contents('php://input')));
$out = ob_get_contents();
ob_end_clean();
$f = fopen('out.txt', 'w+');
fwrite($f, html_entity_decode($out));
fclose($f);
?>
我尝试了不同的方法来获取JSON,但没有一种对我有效。也许StackOverflow的优秀员工可以帮我解决这个问题,就像他们一直在解决我遇到的所有其他问题一样 在评论部分,您似乎只希望将JSON发送到PHP脚本。通常,您会将其发布到PHP,并将其解压缩:
<?php
print_r($_POST);
$json_string = $_POST['message'];
$json = json_decode($json_string);
print_r($json);
?>
其输出为:
Array
(
[message] => {"message":"This is a message"}
)
stdClass Object
(
[message] => This is a message
)
为什么要将json放在HTTP头中?我刚刚在互联网上学习了一些教程,因为这是第一次做Web服务。这应该是一篇以json为主题的文章。。。发布的内容:然后在PHP端解析帖子:我现在正确地获得了数据。它返回以下内容:
string'{“euid”:[{“euid”:1},{“euid”:2}]}(length=33)
但是我只想要JSON,而不是像开头的字符串或结尾的长度这样的附加数据。
Array
(
[message] => {"message":"This is a message"}
)
stdClass Object
(
[message] => This is a message
)