[英]JSON sending to server
我必须从我的Android应用程序向服务器发送一些JSON数据。
这是我编写的代码。
public static String POST(String url, String id, int status, String cardno,String orderno){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("order_number", orderno);
jsonObject.put("id", id);
jsonObject.put("status", status);
jsonObject.put("card_no", cardno);
JSONArray postjson=new JSONArray();
postjson.put(jsonObject);
// 4. convert JSONObject to JSON to String
// json = jsonObject.toString();
// httpPost.setParams("json", json.toString());
System.out.println("json is"+jsonObject.toString());
httpPost.getParams().setParameter("jsonpost", postjson);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httpPost.setEntity(se);
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
System.out.println("result is"+result);
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
而在文本文件中接收此内容的php脚本为:
<?php
error_reporting(E_ALL & ~E_NOTICE);
file_put_contents("postData.txt",$_REQUEST);
if(isset($_REQUEST['jsonpost']))
{
echo 'passed';
}else{
echo "NOT RUNNING";
}
exit;
?>
当我从android应用程序执行代码时,我的请求已完成以连接到服务器,但无法在文本文件中获取任何数据。
我想念什么? 我应该如何将数据发送到服务器? 错误是在我的android代码中发送数据还是在php脚本中接收了数据?
我将修改后的代码如下:
public static String POST(String url, String id, int status, String cardno,String orderno){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.put("order_number", orderno);
jsonObject.put("id", id);
jsonObject.put("status", status);
jsonObject.put("card_no", cardno);
JSONArray postjson=new JSONArray();
postjson.put(jsonObject);
StringEntity se = new StringEntity(jsonObject.toString());
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
System.out.println("result is"+result);
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
但这仍然行不通。 在我的php脚本中要进行任何更改吗?
尝试将您的帖子数据设置为这样的请求:
//passes the results to a string builder/entity
StringEntity se = new StringEntity(jsonObject.toString());
//sets the post request as the resulting string
httpPost.setEntity(se);
//sets a request header so the page receving the request
//will know what to do with it
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
//Handles what is returned from the page
ResponseHandler responseHandler = new BasicResponseHandler();
HttpResponse httpResponse = httpclient.execute(httpPost, responseHandler);
请注意,这不是完整的代码,只是补充代码的部分,因此请在您的示例中使用它(所有var的名称都与您命名的名称相同,因此应该很容易:))。
另外,还要确保您的JSONObject已经jsonpost
关键,因为当你将它张贴这样, key->values
从JSONObject的将成为key->values
在$_POST
变量。
$_REQUEST
是一个类似于$_POST & $_GET
的数组,因此假设您只是想知道那里有什么,而无需经过任何形式的优雅显示print_r()
file_put_contents("postData.txt",print_r($_REQUEST, true));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.