Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在我的示例中,有人能一步一步地解释我的android客户端和运行PHP的服务器之间的交互吗? 公共字符串postJSON()抛出JSONException{ HttpClient HttpClient=新的DefaultHttpClient(); HttpPost-HttpPost=newhttppost(ec2Url+“testjson.php”); JSONObject json=新的JSONObject(); 试一试{ put(“用户名”、“迈克尔”); JSONArray postjson=新的JSONArray(); postjson.put(json); setHeader(“json”,json.toString()); Log.d(DEBUG_标记,“toString()”+json.toString()); httppost.getParams().setParameter(“jsonpost”,postjson); HttpResponse response=httpclient.execute(httppost); if(响应!=null){ InputStream is=response.getEntity().getContent(); BufferedReader reader=新的BufferedReader(新的InputStreamReader(is)); StringBuilder sb=新的StringBuilder(); 字符串行=null; 试一试{ 而((line=reader.readLine())!=null){ sb.追加(第+行“\n”); } }捕获(IOE异常){ } text=sb.toString(); } 返回文本;} 捕获(客户端协议例外e){ }捕获(IOE异常){ } 返回“断开”; }_Php_Android_Json - Fatal编程技术网

在我的示例中,有人能一步一步地解释我的android客户端和运行PHP的服务器之间的交互吗? 公共字符串postJSON()抛出JSONException{ HttpClient HttpClient=新的DefaultHttpClient(); HttpPost-HttpPost=newhttppost(ec2Url+“testjson.php”); JSONObject json=新的JSONObject(); 试一试{ put(“用户名”、“迈克尔”); JSONArray postjson=新的JSONArray(); postjson.put(json); setHeader(“json”,json.toString()); Log.d(DEBUG_标记,“toString()”+json.toString()); httppost.getParams().setParameter(“jsonpost”,postjson); HttpResponse response=httpclient.execute(httppost); if(响应!=null){ InputStream is=response.getEntity().getContent(); BufferedReader reader=新的BufferedReader(新的InputStreamReader(is)); StringBuilder sb=新的StringBuilder(); 字符串行=null; 试一试{ 而((line=reader.readLine())!=null){ sb.追加(第+行“\n”); } }捕获(IOE异常){ } text=sb.toString(); } 返回文本;} 捕获(客户端协议例外e){ }捕获(IOE异常){ } 返回“断开”; }

在我的示例中,有人能一步一步地解释我的android客户端和运行PHP的服务器之间的交互吗? 公共字符串postJSON()抛出JSONException{ HttpClient HttpClient=新的DefaultHttpClient(); HttpPost-HttpPost=newhttppost(ec2Url+“testjson.php”); JSONObject json=新的JSONObject(); 试一试{ put(“用户名”、“迈克尔”); JSONArray postjson=新的JSONArray(); postjson.put(json); setHeader(“json”,json.toString()); Log.d(DEBUG_标记,“toString()”+json.toString()); httppost.getParams().setParameter(“jsonpost”,postjson); HttpResponse response=httpclient.execute(httppost); if(响应!=null){ InputStream is=response.getEntity().getContent(); BufferedReader reader=新的BufferedReader(新的InputStreamReader(is)); StringBuilder sb=新的StringBuilder(); 字符串行=null; 试一试{ 而((line=reader.readLine())!=null){ sb.追加(第+行“\n”); } }捕获(IOE异常){ } text=sb.toString(); } 返回文本;} 捕获(客户端协议例外e){ }捕获(IOE异常){ } 返回“断开”; },php,android,json,Php,Android,Json,代码是有效的,但我并不完全理解它。有人能解释一下httppost.setHeader(“json”,json.toString())在做什么,以及$\u服务器['HTTP\u json']如何设置为等于json.toString()?还有,httppost.getParams().setParameter(“jsonpost”,postjson)做什么?它似乎将“jsonpost”和JSON数组本身配对在一起,但在PHP中,它在哪里被接收,在哪里被解释 我过去问过一些不好的问题,所以如果这不被认

代码是有效的,但我并不完全理解它。有人能解释一下httppost.setHeader(“json”,json.toString())在做什么,以及$\u服务器['HTTP\u json']如何设置为等于json.toString()?还有,httppost.getParams().setParameter(“jsonpost”,postjson)做什么?它似乎将“jsonpost”和JSON数组本身配对在一起,但在PHP中,它在哪里被接收,在哪里被解释


我过去问过一些不好的问题,所以如果这不被认为是“关于主题”或不清楚,请毫不犹豫地告诉我,我会编辑它。

实际上,这段代码有点奇怪,它不是一个良好实践的例子,但幸运的是,考虑一下所涉及的概念是很好的

要理解它,需要引入HTTP。但为了让思考不那么痛苦,我会尽量简化。客户端(Android应用程序)和服务器(HTTP服务器上运行的PHP)一次一个地相互发送字符串(更准确地说,是文本数据流)。这些字符串称为HTTP消息

HTTP消息总是有一个初始部分,称为HTTP头。在头之后,可以(而不是必须)包含HTTP主体。这些部分用两条连续的新线彼此分开

Android代码正在发送一种特定类型的HTTP消息:POST(HTTP消息实际上并不存在,它是一个“抽象概念”)。一个帖子可以包含一个主体,通常是它

您的第一部分问题是关于帖子准备,所以让我们从最终结果开始(这里是您的Android代码发送到服务器的帖子的近似值):

<?php
$json = $_SERVER['HTTP_JSON'];
echo "JSON: \n";
echo "---------------\n";
var_dump($json);
echo "\n\n";

$data = json_decode($json);
echo "Array: \n";
var_dump($data);
echo "\n\n";

$username = $data->username;

echo "Result: \n";
echo "---------------\n";
echo "username : ", $username, "\n";

?>





public String postJSON() throws JSONException{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(ec2Url + "testjson.php");
    JSONObject json = new JSONObject();

    try {
        json.put("username", "Michael");
        JSONArray postjson = new JSONArray();
        postjson.put(json);

        httppost.setHeader("json", json.toString());
        Log.d(DEBUG_TAG, "toString() " + json.toString());
        httppost.getParams().setParameter("jsonpost", postjson);

        HttpResponse response = httpclient.execute(httppost);

        if(response != null) {
            InputStream is = response.getEntity().getContent();
            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){

            }
            text = sb.toString();
        }
    return text;} 

    catch (ClientProtocolException e) {

    } catch(IOException e){

    }
    return "BROKE";
}
POST /testjson.php HTTP/1.1
Host: content.of.your.ec2Url.string
User-Agent: Android Http Client User-Agent
Content-Length: 0
json: {"username": "Michael"}
                                     <- new line here, end of headers, no body