0

我正在尝试将 JSON 格式的数据发布到我在我的网络服务器上运行 PHP 的脚本。我找到了这篇文章:如何使用 httpPost 将数据发送到网站,应用程序崩溃

使用他编写的代码(首先将其放在单独的线程上),我可以将数据发布到 PHP 脚本,该脚本通过 $_POST 变量访问它。但是,我希望以 JSON 格式发布我的数据。我猜这需要我将原始数据流发布到服务器。有哪些功能可以实现这一点?我还需要将图像作为数据流发布到 PHP 脚本,所以我认为这个解决方案也将在该领域为我提供帮助。

另外,将JSON发布到服务器而不是使用他使用的方法有什么好处?

我正在结合 Android SDK 用 Ja​​va 对客户端进行编程。

任何帮助,将不胜感激。

4

1 回答 1

0

我有一个发布 json data 的示例。

看看这个:

    public class LoginActivity extends Activity {

        private static final String TAG = "LoginActivity";

        private Context mContext;
        private Intent mIntent;
        private ProgressDialog pdLoading;

        private class LoginTask extends AsyncTask<Void, Void, String>
        {

                private ArrayList<NameValuePair> mParams = new ArrayList<NameValuePair>();
                private JSONArray mJArray = new JSONArray();
                private JSONObject mJobject = new JSONObject();
                private String jsonString = new String();

                @Override
                protected void onPreExecute() {
                        super.onPreExecute();
                        pdLoading.show();
                }

                @Override
                protected String doInBackground(Void... params) {

                        try {
                                mJobject.put("userName", "test");
                                mJobject.put("password", "test");
                                mJArray.put(mJobject);
                                mParams.add(new BasicNameValuePair("message", mJArray.toString()));

                                jsonString = WebAPIRequest.postJsonData("http://putyoururlhere.com/login.php?", mParams);

                        } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                return null;
                        }
                        return jsonString;
                }

                @Override
                protected void onPostExecute(String result) {
                        super.onPostExecute(result);
                        pdLoading.dismiss();

                        if(result!=null)
                        {

/*                              try {
                                        mJobject = new JSONObject(jsonString);
                                        if(mJobject.getString("Success").equals("True"))
                                        {
                                                mJArray = mJobject.getJSONArray("user");
                                                JSONObject mUser = mJArray.getJSONObject(0);

                                        }
                                } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }*/

                                Log.e(TAG, jsonString);

                        }
                }

        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                initialization();

                new LoginTask().execute();
        }

        private void initialization() {
                mContext = this;
                mIntent = new Intent();
                pdLoading = new ProgressDialog(mContext);
                pdLoading.setMessage("loading...");

        }


}

public class WebAPIRequest {


    public static String convertStreamToString(InputStream is)
                    throws IOException {
            if (is != null) {
                    StringBuilder sb = new StringBuilder();
                    String line;
                    try {
                            BufferedReader reader = new BufferedReader(
                                            new InputStreamReader(is, "UTF-8"));
                            while ((line = reader.readLine()) != null) {
                                    sb.append(line).append("\n");
                            }
                    } finally {
                            is.close();
                    }
                    return sb.toString();
            } else {
                    return "";
            }
    }


    public static String postJsonData(String url, List<NameValuePair> params) {
            String response_string = new String();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            try {
                    httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));


                    String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);
                    String sampleurl = url + "" + paramString;
                    Log.e("Request_Url", "" + sampleurl);

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                    if (response != null) {
                            InputStream in = response.getEntity().getContent();
                            response_string = WebAPIRequest.convertStreamToString(in);

                    }
            } catch (Exception e) {
                    e.printStackTrace();
            }

            return response_string;
    }

}

编辑 :

尝试,

print_r(json_decode($_POST['message'], true);  

或者

$data = file_get_contents('php://input');
$json = json_decode($data,true);

我希望它会有所帮助!

于 2013-10-28T05:39:51.537 回答