Java 如何将JSONObject从Android应用程序传递到PHP文件?
我想向PHP服务器发送一个简单的JSON对象,但当我试图在服务器端检索该对象时,我的$u POST变量为空是毫无意义的。服务器端是PHP5.2,我正在使用android emulator 10。。。有人能看看我的代码,告诉我出了什么问题吗? 非常感谢Java 如何将JSONObject从Android应用程序传递到PHP文件?,java,php,android,Java,Php,Android,我想向PHP服务器发送一个简单的JSON对象,但当我试图在服务器端检索该对象时,我的$u POST变量为空是毫无意义的。服务器端是PHP5.2,我正在使用android emulator 10。。。有人能看看我的代码,告诉我出了什么问题吗? 非常感谢 public void uploadJSon() throws ClientProtocolException, IOException, JSONException{ HttpClient httpclient = new Defa
public void uploadJSon() throws ClientProtocolException, IOException, JSONException{
HttpClient httpclient = new DefaultHttpClient();
String url = "http://so-dev-deb.niv2.com/suivi_activite/test.php";
HttpPost httppost = new HttpPost(url);
JSONObject json = new JSONObject();
json.put("username", "bob");
json.put("email", "test@testsite.com");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("value", json.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
URL test_url = new URL(url);
URLConnection connection = test_url.openConnection();
connection.setDoOutput(true);
HttpResponse response;
response = httpclient.execute(httppost);
Log.i("NVPS",nvps.get(0).toString());
Log.i("JSON",json.toString());
Log.i("response", response.getEntity().getContent().toString());
Log.i("response status",response.getStatusLine().toString());
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
//System.out.println(decodedString);
Log.i("info 10",decodedString);
}
in.close();
}
public void uploadJSon()抛出ClientProtocolException、IOException、JSONException{
HttpClient HttpClient=新的DefaultHttpClient();
字符串url=”http://so-dev-deb.niv2.com/suivi_activite/test.php";
HttpPost HttpPost=新的HttpPost(url);
JSONObject json=新的JSONObject();
put(“用户名”、“bob”);
json.put(“电子邮件”test@testsite.com");
List-nvps=newarraylist();
add(新的BasicNameValuePair(“value”,json.toString());
setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));
URL test_URL=新URL(URL);
URLConnection=testurl.openConnection();
connection.setDoOutput(真);
HttpResponse响应;
response=httpclient.execute(httppost);
Log.i(“NVPS”,NVPS.get(0.toString());
Log.i(“JSON”,JSON.toString());
Log.i(“response”,response.getEntity().getContent().toString());
Log.i(“响应状态”,response.getStatusLine().toString());
BufferedReader in=新的BufferedReader(
新的InputStreamReader(
getInputStream());
字符串解码字符串;
而((decodedString=in.readLine())!=null){
//System.out.println(解码字符串);
Log.i(“信息10”,解码字符串);
}
in.close();
}
服务器端test.php是:
<?php
$tmp = json_decode($_POST['value']);
var_dump($tmp);
?>
我通常采用这种方法在Java代码中生成JSON对象:
StringWriter writer = new StringWriter();
JSONWriter jsonWriter = new JSONWriter(writer);
jsonWriter.object();
jsonWriter.key("key1").value("test1");
jsonWriter.key("key2").value("test2");
jsonWriter.endObject();
String toSend = writer.toString();
我不是PHP方面的专家,然而,我参与的一个项目解码了我像这样提交的json
$jsonData = file_get_contents("php://input");
如果(isset($jsonData)和&!empty($jsonData)){
$this->data=json\u decode($jsonData,true);
}
注意,我将JSON包装在用户中,因为服务器需要我的案例 我不知道为什么它没有在我的代码中正确显示,但这一行必须添加到List nvps=new ArrayList();我在代码中使用了您的代码片段来生成JSON对象,但我的服务器端仍然没有任何内容……有什么想法吗?我建议您在客户端检查JSON代码。这是我使用的一个示例。它不允许我添加任何代码,因此我将添加另一个答案
public static String login(Context context, String email, String pwd) {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
// Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
Log.d("Debug","Login Url:" + context.getResources().getString(
R.string.url) + context.getResources().getString(
R.string.login));
HttpPost post = new HttpPost(context.getResources().getString(
R.string.url) + context.getResources().getString(
R.string.login));
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
json.put("email", email);
json.put("password", pwd);
StringEntity se = new StringEntity("{\"User\":" + json.toString()
+ "}");
post.setEntity(se);
response = client.execute(post);
if (response != null) {
json = getResult(response);
if(statusOK(json)){
return new String(json.getString("token"));
}
}
} catch (Exception e) {
Log.e("Error", "Error Login", e);
}
// printJSON(json);
return "ERROR";
}