【问题标题】:Android JSON CommunicationAndroid JSON 通信
【发布时间】:2013-10-10 04:50:37
【问题描述】:

我想通过我的 Android 设备在 PHP 服务器上执行此查询:

{
    "command": "REGISTER",
    "data": {
        "email": "EMAIL",
        "login": "LOGIN",
        "password": "PASSWORD",
        "language": "USER_LANGUAGE"      
    }
public class AysnchronousTaskPost extends AsyncTask<String, Long, String> {
    private static final String PATH = "http://alphabravo.com/myapp/api.php";

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        InputStream mInputStream;
        String lineString = "";
        String resultString = "";
        StringBuilder mStringBuilder = null;

        List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();


        nameValuePairList.add(new BasicNameValuePair("command","register"));
        nameValuePairList.add(new BasicNameValuePair("email","abc@gmail.com"));
        nameValuePairList.add(new BasicNameValuePair("login", "abc"));
        nameValuePairList.add(new BasicNameValuePair("password", "1205"));
        nameValuePairList.add(new BasicNameValuePair("language", "en"));

        try {
            HttpClient mHttpClient = new DefaultHttpClient();
            HttpPost mHttpPost = new HttpPost(PATH);
            mHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));

            HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
            HttpEntity mHttpEntity = mHttpResponse.getEntity();

            StatusLine mStatusLine = mHttpResponse.getStatusLine();

            int statusCodeString = mStatusLine.getStatusCode();

            if (statusCodeString == 200) {

                mInputStream = mHttpEntity.getContent();

                mStringBuilder = new StringBuilder();

                BufferedReader mBufferedReader = new BufferedReader(
                        new InputStreamReader(mInputStream, "iso-8859-1"), 8);

                while ((lineString = mBufferedReader.readLine()) != null)
                    mStringBuilder.append(lineString + "/n");

            }

            resultString = mStringBuilder.toString();

        } catch (Exception e) {
            // TODO: handle exception
        }
        return resultString;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

我得到一个空的BufferedReader.ReadLine()。我是否正确创建mHttpPost? 我也试过HTTPget 方法。 但结果是一样的。请帮帮我

我已经在浏览器上检查了我的 php 脚本。我的 PHP 脚本工作正常。

【问题讨论】:

  • 您是否进行了测试以确保 php 脚本返回正确的 json 值?
  • 是的,我的 php 脚本返回值正确,我已经检查过了
  • 尝试记录mInputStream的结果
  • 请注意,如果您尝试在服务器端获取 json 对象,则不会得到,因为 mHttpPost.setEntity() 将您的参数作为 HTTP post 参数而不是 json 表示形式发送跨度>
  • 那么我如何发送 JSON 参数并获取 JSON 响应,使用这个 'JSONObject postJsonObject = new JSONObject();'

标签: java php android json web-services


【解决方案1】:

您需要将您的键值对转换为内容类型application/json,然后将其发送到您的服务器。

在异步任务中试试这个,

JSONObject data = new JSONObject();

// Add key/value pairs
data.put("email", email);
data.put("login", login);
data.put("password", password);
data.put("language", language);

JSONObject json = new JSONObject();
json.put("command", command);
json.put("data", data);

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost("http://alphabravo.com/myapp/api.php");

StringEntity se;
se = new StringEntity(json.toString());

// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");


HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

HttpEntity entity = response.getEntity();

String receiveJson = EntityUtils.toString(entity);

//use receiveJson as new JSONObject(receiveJson) ot new JSONArray(receiveJson) as per your returned json value

【讨论】:

  • 谢谢,我已经尝试过了,但 'receiveJSON' 仍然是空的,我期待 JSON 对象作为回报
  • @kami:我怀疑您的 php 脚本没有返回任何内容,只需在 PHP 中打印传入的 json 并输入 Log.d("receiveJson", receiveJson) 并查看它是否会出现在日志中。跨度>
  • @kami:你能告诉我在 PHP 的服务器端如何获取和解析从 android 发送的 json。因为我猜现在问题出在服务器端,因为上面的代码工作正常,我在我的 2-3 个项目中使用它没有任何问题。
猜你喜欢
  • 1970-01-01
  • 2013-09-18
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多